I am using NETDCU8. To communicate between two prozessors (this from NETDCU8 and an ARM 7) I use the RS232 Port. Everything work well except the order WriteFile (...). Using this the Code runs until this point, a breakpoint in the next line is never reached. Then the whole code is that corrupted, that even a "stop debugging" does not stop the software on the NETDCU8. A whole power off / power on reset has to be done to reanimate the system.
Using instead of WriteFile (..) TransmitCommChar(..) with a loop works well. ReadFile (..) also works great for reading data.
Some help would be convenient...
WriteFile (..) does not work in RS232 Communication
-
-
Until now I tried everything what came into my mind, but WriteFile (...) does not work with serial Communication. If anybody has an idea, i would appreciate it...
-
Can you create a small code sample that shows this error?
Regards,
H. Keller
-
The initialization of the COM Port looks like this:
CodeThe settings look like this:
Code- bool CSerial::RS232Setting(DWORD dwBaudrate)
- {
- DCB PortDCB;
- //Initialize the DCBlength member.
- PortDCB.DCBlength = sizeof (DCB);
- //Get the default port setting information.
- GetCommState (m_hSPort, &PortDCB);
- //Change the DCB structure settings.
- PortDCB.BaudRate = dwBaudrate; //Current baud
- PortDCB.fBinary = TRUE; //Binary mode; no EOF check
- PortDCB.fParity = FALSE; //Enable parity checking
- PortDCB.fOutxCtsFlow = TRUE; //CTS output flow control
- PortDCB.fOutxDsrFlow = TRUE; //DSR output flow control
- PortDCB.fDtrControl = DTR_CONTROL_ENABLE; //DTR flow control type
- PortDCB.fDsrSensitivity = FALSE; //DSR sensitivity
- PortDCB.fTXContinueOnXoff = FALSE; //XOFF continues Tx
- PortDCB.fOutX = FALSE; //No XON/XOFF out flow control
- PortDCB.fInX = FALSE; //No XON/XOFF in flow control
- PortDCB.fErrorChar = FALSE; //Disable error replacement
- PortDCB.fNull = FALSE; //Disable null stripping
- PortDCB.fRtsControl = RTS_CONTROL_ENABLE;//RTS flow control
- PortDCB.fAbortOnError = FALSE; //Do not abort reads/writes on error
- PortDCB.ByteSize = 8; //Number of bits/byte, 4-8
- PortDCB.Parity = NOPARITY; //0-4=no,odd,even,mark,space
- PortDCB.StopBits = ONESTOPBIT; //0,1,2 = 1, 1.5, 2
- //Configure the port according to the specifications of the DCB structure.
- if (!SetCommState(m_hSPort, &PortDCB)) //Could not configure the serial port.
- {
- return FALSE;
- }
- return TRUE;
- }
This is more or less a copy of example code f&s delivers. The writing to the port looks like this:
Code- int CSerial::WriteFresh(BYTE bStream[], DWORD dwBytesToWrite)
- {
- //DWORD dwBytesTransferred;
- DWORD i;
- BYTE bDaten[510];
- for (i=0; i<dwBytesToWrite; i++)
- {
- bDaten[i] = *(bStream + i); //Übergabe der Daten an den Stream
- //Sleep(1);
- TransmitCommChar(m_hSPort, bDaten[i]);
- }
- //SetCommMask (m_hSPort, EV_RXCHAR); //Hier ein stopp gesetzt wird noch erreicht...
- //Sleep(1);
- //WriteFile (m_hSPort, &bDaten[0], 1, &dwBytesTransferred, NULL);
- return 1;//Dieser Punkt wird nie erreicht.
- }
Like already described ReadFile (..) works well, TransmitCommChar (..) like in the Code works well, too. Using the WriteFile (..) (like commented out) does not work. The line SetCommMask was just for some tryout, it does not work with or without that line. The program never leaves the WriteFile Routine.
The whole stuff works in an own thread.
-
Quote from "YoMas"
When using FILE_FLAG_OVERLAPPED, you have to give the OVERLAPPED structure to the WriteFile() function. However overlapped access is not supported in WinCE anyway, so maybe removing the FILE_FLAG_OVERLAPPED is already the solution.In addition the serial port is a hardware device that can only be read and written sequentially. There is no way of using random access (seek, etc). So you have to remove the FILE_FLAG_RANDOM_ACCESS also. And you can not change the attribute of the COM-port file, therefore also leave out FILE_ATTRIBUTE_NORMAL.
Now try again...
Regards,
H. Keller
-
Thank you for your hints, BUT:
No combination of dwFlagsAndAttributes, nor leaving out all of them works. Still the program stops at WriteFile(..) and never comes out of this routine again.
Please HELP!!!
-
Hallo,
have you already tried the "Quick RS232 test" sample from our "Windows CE Samples" forum? I think there is a simple mistake hidden in your code.
-
Just to give a final answer:
The mistake lies in fOutxCtsFlow = TRUE and in fOutxDsrFlow = TRUE. Changing both parameters to FALSE solves the probblem.Generally the RS232 is extremly bad documented in WCE, also the microsoft webpage is no help at all. So for a lot of settings, which could be made it is not obvious what hardware settings correspond to them.