日期:2014-05-20  浏览次数:21158 次

用API与串口通信,如何处理接收到串口发送数据到达信息?
现在是接收端串口无法接收数据,发送方串口在发送时一直阻塞,但当将该接收端串口关闭时,发送方可以发送出去?

------解决方案--------------------
用.Net 2.0里的SerialPort类吧
------解决方案--------------------
我用过 SerialPort,有例子
但是还没有用过api来实现
如果需要 我可以给你发过去
------解决方案--------------------
public byte[] Read(int NumBytes) {
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes];
if (hComm!=INVALID_HANDLE_VALUE) {
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead=0;
ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes,OutBytes,BytesRead);
}
else {
throw(new ApplicationException( "Comm Port Not Open "));
}
return OutBytes;
}
public void Write(byte[] WriteBytes) {
if (hComm!=INVALID_HANDLE_VALUE) {
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);
}
else {
throw(new ApplicationException( "Comm Port Not Open "));
}
}
}
}
}

------解决方案--------------------
上面那个程序有点小问题
dcbCommPort.Parity=Parity;
dcbCommPort.ByteSize=ByteSize;
dcbCommPort.StopBits=StopBits;
if (!SetCommState(hComm, ref dcbCommPort))
这样写,并不能给dcbCommPort付值,还有一个api ,BuildCommDCB,专门用来初始化参数的