Hope you are all well. I am experiencing a weird trouble while trying to make a serial communication with RPi-Pico -> PC. From Pico I am sending 9 Bytes packet (8 Byte Data from ADC + \n) once a second through Serial communication using USB BUS.
While reading it from windows, my program at the first hand reading nothing, only staying at the if (!WaitCommEvent(hComm, &dwEventMask, NULL))
stage.
But if I run putty with any configuration, then data is shown in putty. After closing putty if I run my program, surprisingly it's also reading data without any fail!!!!
Again after restarting the Pico, same situation comes back. That's so far very much unknown feature for me. Could any of you help me to solve that situation? I am sending the code snippet for analysis. Thank you so much in advance. Have a great day / evening!
hComm = CreateFileA(commPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hComm == INVALID_HANDLE_VALUE)
cout << "\nError Port " << commPortName << " can't be opened!!!\n";
else
cout << "\nPort " << commPortName << " is opened!!\n";
dcbParams.DCBlength = sizeof(dcbParams);
if (!GetCommState(hComm, &dcbParams))
cout << "\nError in GetCommState()\n";
dcbParams.BaudRate = CBR_115200; dcbParams.ByteSize = 8;
dcbParams.StopBits = ONESTOPBIT; dcbParams.Parity = NOPARITY;
if (!SetCommState(hComm, &dcbParams))
cout << "\nError in setting Device-Control Block(DCB)\n";
else
{
cout << "\n\n Setting DCB Structure Successfull"
<< "\n Baudrate --> " << dcbParams.BaudRate
<< "\n ByteSize --> " << (unsigned int)dcbParams.ByteSize
<< "\n StopBits --> " << (unsigned int)dcbParams.StopBits
<< "\n Parity --> " << (unsigned int)dcbParams.Parity;
}
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hComm, &timeouts))
cout << "\n\n Error! in Setting Time Outs\n";
else
cout << "\n\n Setting Serial Port Timeouts Successfull\n";
if (!SetCommMask(hComm, EV_RXCHAR))
cout << "\n\n Error! in Setting Communication Mask\n";
else
cout << "\n\n Setting Communication Mask successfull\n";
cout << "\n\n Waiting for Data Reception....\n";
for (int i = 0; i <= 99; i+=1)
{
if (!WaitCommEvent(hComm, &dwEventMask, NULL))
cout << "\n Error! in Setting Wait for Communication Event...!\n";
else
{
ReadFile(hComm, &readBuffer, sizeof(readBuffer), &noBytesRead, NULL);
cout << " " << readBuffer;
}
}
CloseHandle(hComm);