<< Program to communicate with the com >>

This is the essence of the problem. When I launch the program and execute it step by step (f10 in the Visual Studio), the port opens perfectly and the recording takes place. when I just run the program, it is not executed. The WriteFile (...) function does not execute. And it does not give any error, just the program stops doing something.

I tried to insert any pauses (Sleep, getch (), cin, etc.) before this function, between opening the port and WriteFile (), but in this case the program does not work even when stepping.

Tried to make a window application - one button opens the port, another writes. The port opens, but when I click write, the program hangs.

It is required that the program be executed NOT only with step-by-step execution! please help with the solution of the problem.

But the program code:

int main() { /* .... */ char sPortName[] = "COM5"; open_port(sPortName);// сперва открываем порт write_scc((void*) TextOut,a,b);// запись в порт /*....*/ } void open_port(char * name) { hCom = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);//(имя порта, права доступа,0,0) -открытие порта, с указанием прав if (hCom == INVALID_HANDLE_VALUE)// если порт 

open with error (port is not open) {cout << "*** Error opening port! \ n"; getch (); return; }

else {cout << "*** Port succesfully opened! \ n";

  GetCommState(hCom, &dcb);//Функция GetCommState извлекает // данные о текущих настройках // управляющих сигналов для указанного // коммуникационного устройства. dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; if (SetCommState(hCom, &dcb)) cout << " *** Configuring OK.\n\n"; else cout << " *** Configuring error.\n\n"; hThread = CreateThread(0, 0, ReadThread, (LPVOID)hwnd, 0, 0); } } bool write_scc(LPCVOID outputData, const unsigned int& sizeBuffer, unsigned long& length) { if (&length) { if (WriteFile(hCom, // handle to file to write to outputData, // pointer to data to write to file sizeBuffer, // number of bytes to write &length,NULL) == 0) // pointer to number of bytes written { printf("Reading of serial communication has problem."); return FALSE; } return TRUE; } 

return FALSE; }

  • who knows, maybe your ReadThread blocks the whole record ... - KoVadim

1 answer 1

And is there anything connected to the com-port? If not, you will always wait for ReadFile or WriteFile. It is necessary at least to plug the cap, so that the TX falls on the RX of the same port. And configure the port to half dupleh. And open the port with the FILE_FLAG_OVERLAPPED flag. And use the OVERLAPPED structure in ReadFile and WriteFile. At least, I managed to listen to the microcontroller hanging on a com-port in this way. Quite a strange way to exchange data between threads.