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; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值