C#下调用JustinIO的串口通信问题

运用JustinIO做和串口通信的程序过程中遇到两大问题:

1)SetCommState  一直不成功;

          if (!SetCommState(hComm, ref dcbCommPort))
           {
               uint ErrorNum=GetLastError();
               throw (new ApplicationException("Comm Port Can Not Be Opened2"));
           }

2)在打开串口后关闭串口,下一次再打开串口出现错误。

      两个问题一一得到解决。
    一、  针对第1)个问题,是因为结构体public struct DCB 中的一部分被注释了,需要去除注释得到。

public struct DCB { //taken from c struct in platform sdk public int DCBlength; // sizeof(DCB) public int BaudRate; // current baud rate /* these are the c struct bit fields, bit twiddle flag to set */ public int fBinary; // binary mode, no EOF check public int fParity; // enable parity checking public int fOutxCtsFlow; // CTS output flow control public int fOutxDsrFlow; // DSR output flow control public int fDtrControl; // DTR flow control type public int fDsrSensitivity; // DSR sensitivity public int fTXContinueOnXoff; // XOFF continues Tx public int fOutX; // XON/XOFF out flow control public int fInX; // XON/XOFF in flow control public int fErrorChar; // enable error replacement public int fNull; // enable null stripping public int fRtsControl; // RTS flow control public int fAbortOnError; // abort on error public int fDummy2; // reserved public uint flags; public ushort wReserved; // not currently used public ushort XonLim; // transmit XON threshold public ushort XoffLim; // transmit XOFF threshold public byte ByteSize; // number of bits/byte, 4-8 public byte Parity; // 0-4=no,odd,even,mark,space public byte StopBits; // 0,1,2 = 1, 1.5, 2 public char XonChar; // Tx and Rx XON character public char XoffChar; // Tx and Rx XOFF character public char ErrorChar; // error replacement character public char EofChar; // end of input character public char EvtChar; // received event character public ushort wReserved1; // reserved; do not use }  

    二 、  针对2)个问题因为JustinIO的 close函数不够完善,在关闭后没有将 opened 从TRUE 变为FALSE。

public void Close() { if (hComm != INVALID_HANDLE_VALUE) { CloseHandle(hComm); Opened = false; } }

你可能感兴趣的:(C#下调用JustinIO的串口通信问题)