G
G
German2019-04-13 16:12:21
Arduino
German, 2019-04-13 16:12:21

Does the COM port take a long time to return a response from the Arduino?

I am trying to get a response from Arduino on request.
In this example, I send the letter H and receive Hello , everything works.
But the problem is that the reception works sooo long (you have to wait 2-3 seconds).
Sending works instantly, but the answer takes a long time, what should I do?
My code:

#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <string> //for wstring

using namespace std;

int main(int argc, char* argv[])
{
  int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  wchar_t sPortName[8] = L"COM";
  wchar_t sPortNumberStr[8] = L"";
  int sPortNumber = 0;
  wchar_t buffer[256] = L"";
  cout << "Enter COM port number: ";
  cin >> sPortNumber;
  swprintf_s(sPortNumberStr, L"%d", sPortNumber);
  wcscat_s(sPortName, sizeof(sPortNumberStr) / sizeof(wchar_t), sPortNumberStr);
  HANDLE hSerial = ::CreateFile((LPCWSTR)sPortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (hSerial == INVALID_HANDLE_VALUE)
  {
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
    {
      wcout << sPortName << " does not exist\n";
      return -1;
    }
    cout << "Error in file\n";
    return -1;
  }
  else
  {
    //настройка параметров соединения
    DCB dcbSerialParams = { 0 };
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    if (!GetCommState(hSerial, &dcbSerialParams))
    {
      cout << "Getting state error\n";
      return -1;
    }
    dcbSerialParams.BaudRate = CBR_9600;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;
    if (!SetCommState(hSerial, &dcbSerialParams))
    {
      cout << "Error setting serial port state\n";
      return -1;
    }
  }
  //Sleep();
  while (true)
  {
    char data[] = "H";  // строка для передачи
    DWORD dwSize = sizeof(data);   // размер этой строки
    DWORD dwBytesWritten;    // тут будет количество собственно переданных байт
    DWORD dwBytesReaded;
    BOOL iRet = WriteFile(hSerial, data, dwSize, &dwBytesWritten, NULL);
    if (!iRet)
    {
      cout << "Write error\n";
      return -1;
    }
    else
    {
      cout << dwSize << " Bytes in string. " << dwBytesWritten << " bytes sended\n";
      iRet = ReadFile(hSerial, data, 256, &dwBytesReaded, NULL);
      if (!iRet)
      {
        cout << "Read error\n";
        return -1;
      }
      else
      {
        cout << data << " " << dwBytesReaded << " bytes readed\n";
      }
    }
  }
  cout << "Press Enter to exit";
  while (_getch() != 13);
  return 0;
}

Arduino code:
const int ledPin = 13;
int inputSymbol = 0;

void setup()
{
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
}

void loop() 
{
    if(Serial.available())
    {
        inputSymbol = Serial.read();
        if(inputSymbol == 'L')
        {
            digitalWrite(ledPin, HIGH);
            delay(3000);
            digitalWrite(ledPin, LOW);
            inputSymbol = NULL;
        }
        if(inputSymbol == 'H')
        {
            Serial.print("Hello!");
            inputSymbol = NULL;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vanyamba-electronics, 2019-04-13
@vanyamba-electronics

The file driver simply waits for the program to send it an end-of-line character.
Add a call to FlashFileBuffers to transfer the data right away:

BOOL FlushFileBuffers(
  HANDLE hFile
);

More

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question