Writing to serial port problem. [modified]
-
Hi, I had got an error as "Invalid handle specified" while trying write to a serial port(COM1)using WriteFile(). But the handle to serial port is seems to be valid. One by one step I had followed is below. Step 1: Create a connection HANDLE hSerial = CreateFile( "COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL); Step 2: Configure the settings DCB db; memset( &db,0,sizeof(db)); db.DCBlength = sizeof(db); if( ! GetCommState( hSerial, &db )) { return; } if( ! SetCommState( hSerial, &db ) ) { DWORD dw = GetLastError(); return; } Step 3: DWORD dwWritten; OVERLAPPED or; if( WriteFile( hSerial, "Write", 6, &dwWritten, &or )) { AfxMessageBox( "Write Success!!!"); } Step1 and Step2 are successfully executed. But step3 returns an "Invalid handle specified" error. What may be prob? Thanx in Advance. -- modified at 0:33 Friday 11th August, 2006
-
Hi, I had got an error as "Invalid handle specified" while trying write to a serial port(COM1)using WriteFile(). But the handle to serial port is seems to be valid. One by one step I had followed is below. Step 1: Create a connection HANDLE hSerial = CreateFile( "COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL); Step 2: Configure the settings DCB db; memset( &db,0,sizeof(db)); db.DCBlength = sizeof(db); if( ! GetCommState( hSerial, &db )) { return; } if( ! SetCommState( hSerial, &db ) ) { DWORD dw = GetLastError(); return; } Step 3: DWORD dwWritten; OVERLAPPED or; if( WriteFile( hSerial, "Write", 6, &dwWritten, &or )) { AfxMessageBox( "Write Success!!!"); } Step1 and Step2 are successfully executed. But step3 returns an "Invalid handle specified" error. What may be prob? Thanx in Advance. -- modified at 0:33 Friday 11th August, 2006
What is the value of hSerial at this point (check with your debugger) ? It should be something else than 0xFFFFFFFF
Cédric Moonen Software developer
Charting control -
Hi, I had got an error as "Invalid handle specified" while trying write to a serial port(COM1)using WriteFile(). But the handle to serial port is seems to be valid. One by one step I had followed is below. Step 1: Create a connection HANDLE hSerial = CreateFile( "COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL); Step 2: Configure the settings DCB db; memset( &db,0,sizeof(db)); db.DCBlength = sizeof(db); if( ! GetCommState( hSerial, &db )) { return; } if( ! SetCommState( hSerial, &db ) ) { DWORD dw = GetLastError(); return; } Step 3: DWORD dwWritten; OVERLAPPED or; if( WriteFile( hSerial, "Write", 6, &dwWritten, &or )) { AfxMessageBox( "Write Success!!!"); } Step1 and Step2 are successfully executed. But step3 returns an "Invalid handle specified" error. What may be prob? Thanx in Advance. -- modified at 0:33 Friday 11th August, 2006
Try to zero out or before using it. ZeroMemory(&or, sizeof(OVERLAPPED)); Then set the hEvent member to your handle (hSerial). Or an easier way: Use a ready made serial communication class. There are quite a few here at CP. -- modified at 3:15 Friday 11th August, 2006
-
Try to zero out or before using it. ZeroMemory(&or, sizeof(OVERLAPPED)); Then set the hEvent member to your handle (hSerial). Or an easier way: Use a ready made serial communication class. There are quite a few here at CP. -- modified at 3:15 Friday 11th August, 2006
oops! That was the problem. Thanx you very much.
-
oops! That was the problem. Thanx you very much.
I'm glad to help you. But i would suggest you to use a ready-made serial comms class. It's not easy to handle two different overlapped structures (one for read, one for write). And if you don't use overlapped, then your reads will hang until there is data to read. A serial comms class will take care of that problem for you.
-
I'm glad to help you. But i would suggest you to use a ready-made serial comms class. It's not easy to handle two different overlapped structures (one for read, one for write). And if you don't use overlapped, then your reads will hang until there is data to read. A serial comms class will take care of that problem for you.
I decided to use non-overlapped communication. I had made a worker thread for reading the serial port hoping that the blocking issue get solved. But from the worker thread if I call ReadFile() using the same handle, it do not wait, even if there is no data in the serial port. why this happens? Plz help.. -- modified at 7:04 Friday 11th August, 2006
-
I decided to use non-overlapped communication. I had made a worker thread for reading the serial port hoping that the blocking issue get solved. But from the worker thread if I call ReadFile() using the same handle, it do not wait, even if there is no data in the serial port. why this happens? Plz help.. -- modified at 7:04 Friday 11th August, 2006
Well, I'm not sure what happens. My suggestion is to read about ReadFile at MSDN. Or, as I said before... Use a ready-made serial comms class... That way, you won't have to reinvent the wheel. Serial comms ain't easy to handle. I'm leaving for the weekend now.