an event from comm-port
-
Please, help me! I have external device, which connects with comm-port. I send to device the number, after it signaled it send to computer some signal; how can i receive this signal? in case, if device is nonsignaled, how can i wait it for five sec, and break the waiting operation? What do i need to write to this code, in the end of it? I know all abot WaitCommEvent and WaitForSingleObject, but i don't know how to use it. WaitForSingleObject doesn't work with EVENT, or i don't know how to use it:( HANDLE port; DWORD bc; DCB dcb; char buffer[100]; char *buf_out="50"; DWORD dwBlockSize=512; void *szBuffer; int send=255; ZeroMemory(&dcb,sizeof(DCB)); strcpy(buffer,"baud=1200 parity=N data=8 stop=1"); BuildCommDCB((char*)&buffer,&dcb); dcb.fRtsControl = RTS_CONTROL_ENABLE; port=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); GetCommState(port,&dcb); SetCommState(port,&dcb); HeapFree(GetProcessHeap(),0,&dcb); szBuffer = malloc(dwBlockSize); memset(szBuffer, send, dwBlockSize); // Sending number for device WriteFile(port,szBuffer,sizeof(szBuffer),&bc,NULL); free(szBuffer); // Wait for signal for five sec CloseHandle(port);
-
Please, help me! I have external device, which connects with comm-port. I send to device the number, after it signaled it send to computer some signal; how can i receive this signal? in case, if device is nonsignaled, how can i wait it for five sec, and break the waiting operation? What do i need to write to this code, in the end of it? I know all abot WaitCommEvent and WaitForSingleObject, but i don't know how to use it. WaitForSingleObject doesn't work with EVENT, or i don't know how to use it:( HANDLE port; DWORD bc; DCB dcb; char buffer[100]; char *buf_out="50"; DWORD dwBlockSize=512; void *szBuffer; int send=255; ZeroMemory(&dcb,sizeof(DCB)); strcpy(buffer,"baud=1200 parity=N data=8 stop=1"); BuildCommDCB((char*)&buffer,&dcb); dcb.fRtsControl = RTS_CONTROL_ENABLE; port=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); GetCommState(port,&dcb); SetCommState(port,&dcb); HeapFree(GetProcessHeap(),0,&dcb); szBuffer = malloc(dwBlockSize); memset(szBuffer, send, dwBlockSize); // Sending number for device WriteFile(port,szBuffer,sizeof(szBuffer),&bc,NULL); free(szBuffer); // Wait for signal for five sec CloseHandle(port);
(1) Init an event by using CreateEvent() (2) Add in OVERLAPPED struct to your code (3) Insert the event (1) into (2) (4) CreateFile() with FILE_FLAG_OVERLAPPED Now call WaitCommEvent() with LPOVERLAPPED = (2) whereever you want. Wenn the specified com-event occurs the event (1) will be set to signaled state. You should wait for it with WaitForSingleObject() in a seperate thread. Stay heavy ... RockNix/// ------------------------------- Look out for free Win32 Serial Communication Module for VC++ or Borland C++ Builder on http://www.klangwerker.de -------------------------------