In the debugging of the ICU communication equipment, because the serial communication of the old fault, so I suspect CF implementation of the SerialPort class whether there is a problem, so finally decided to use a pure API function to achieve serial reading. First from the Internet search related code (keywords: C # API serial port), found that the relevant information on the Internet from a version, that is the so-called MSDN Sample code (the specific source of MSDN, I did not research), the other code is mostly its variants. In fact, this sample code is problematic, that is, the DCB structure is declared problematic, although the code can communicate normally, but if you set the parity, you will find that the parity is not valid. VC in the DCB structure statement as follows: typedef struct _DCB { DWORD dcblength; /* sizeof (DCB) & nbsp; * DWORD baudrate; /* baudrate at which running * DWORD fbinary:1; /* Binary Mode ( Skip EOF Check) */ DWORD fparity:1; /* Enable parity Checki ng */ DWORD foutxctsflow:1; /* CTS handshaking on output */ DWORD foutxdsrflow:1; /* DSR handshaking on output */ DWORD fdtrcontrol:2; /* DTR Flow control * * DWORD fdsrsensitivity:1; /* DSR sensitivity * * DWORD ftxcontinueonxoff:1; /* Continue TX when Xoff sent/ DWORD foutx:1; /* Enable output X -on/x-off * DWORD finx:1; /* Enable input x-on/x-off */ DWORD ferrorchar:1; /* Enable ERR replacement * * DWORD fnull:1; /* Enable Null Stripping * DWORD frtscontrol:2; /* Rts Flow control * * DWORD fabortonerror:1; /* Abort all reads and writes on Error */ DWORD fdummy2:17; /* reserved &n bsp; * WORD wreserved; /* not currently used * WORD xonlim; /* Transmit x-on threshold / WORD xofflim; /* Transmit X-off THRESHOLD&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; * BYTE bytesize; /* number of bits/byte, 4-8& nbsp; */ BYTE parity; /* 0-4=none,odd,even,mark,space * BYTE stopbits; /* 0,1,2 = 1, 1.5, 2 * Char xonchar; /* Tx and Rx x-on character */ char xoffchar; & nbsp; /* Tx and Rx x-off character * * Char errorchar; /* Error replacement char * Char eofchar; /* End of Input character */ Char evtchar; /* Received Event character * WORD wreserved1; /* Fill for now. & nbsp; */} DCB, * LPDCB; Problematic code DCB structure declaration is as follows: [StructLayout (layoutkind.sequential)] public struct DCB { public int dcblength; public int baudrate; public int fbinary; public int fparity; public int foutxctsflow; public int foutxdsrflow; public int Fdtrcontrol; public int fdsrsensitivity; public int Ftxcontinueonxoff; public int foutx; public int Finx; public int Ferrorchar; public int fnull; public int Frtscontrol; public int fabortonerror; public int fDummy2; public UINT Flags; public ushort wreserved; public ushort Xonlim; public ushort Xofflim; public byte ByteSize; public byte Parity; public byte StopBits; public byte XonChar; public byte XoffChar; public byte ErrorChar; public byte EofChar; public byte Evtchar; public ushort WReserved1; } familiar with C + + users should know that the structure of the format of the declaration, such as a DWORD fbinary:1, the variable is set in bits, A total of 4 bytes of related bits in DCB, which is equivalent to the space occupied by an int variable in C #. Obviously, the above DCB structure will have problems, in fact, you set the serial port parameters, such as parity due to the migration problem, although you set up, in fact, there is no set success. In fact, it is not that I said that people's DCB statement is wrong, in the SerialPort class you can find Microsoft's official own DCB statement (need to Decompile SerialPort class), declared as follows: [StructLayout ( layoutkind.sequential)] public struct DCB { public int dcblength public int baudrate; public UINT Flags; Public ushort Wreserved; public ushort Xonlim; public ushort Xofflim; public byte ByteSize; public byte Parity; public byte StopBits; public byte XonChar; public byte XoffChar; public byte ErrorChar; public byte EofChar; public byte Evtchar; public ushort WReserved1; } and has a function that sets the bit flags specifically, as follows: internal void Setdcbflag (int whichflag, int setting) & nbsp; { uint num; setting = setting << Whichflag; if ((Whichflag = 4) | | (Whichflag = 12)) { num = 3; } else if (Whichflag = =) { num = 0x1ffff; } Else { num = 1; } dcb.flags &= ~ (num << whichflag); dcb.flags |= (UINT) setting; } The API code that has been modified to run correctly is as follows (note that since I am running on the WinCE platform, the DLL's path is "//windows// Coredll.dll ", you modify the" kernel32 "can be used in PC:///<summary> ///API serial type Ye Fan modified http:// Blog.csdn.net/yefanqiu ///</summary> public class Commport { ///<summary> / Port name (Com1,com2 ...) COM4 ...) ///</summary> Public String Port = "COM1:"; ///<summary> /// Baud rate 9600 ///</summary> public int baudrate = 9600; ///<summary> /// Data bits 4-8 ///</summary> public byte bytesize = 8; 4-8 ///<summary> /// Parity 0-4=no,odd,even,mark,space ///</summary> public byte Parity = 0; //0-4=no,odd,even,mark,space ///<summary> ///Stop bit /// </summary> public byte stopbits = 0; //0,1,2 = 1, 1.5, 2 &nbs p; ///<summary> ///Timeout long ///</summary> public int ReadTimeout = 200; ///<summary> /// Whether the serial port has been opened ///</summary> public bool opened = FALSE; ///<summary> ///COM Mouth handle ///</summary> private int hcomm =-1; &NBSP;&NBSP;&Nbsp; #region "API correlation definition" Private const string DllPath = "//windows//coredll.dll"; "Kernel32"; ///<summary> // /WINAPI Constants, write logos ///</summary> Private Const UINT GENERIC_READ = 0x80000000; ///<summary> /// WINAPI constants, Reading flags &n
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.