用VS2005 的SerialPort 做了一个读卡的程序,
通过Write方法向串口发送代码,用serialPort.ReadByte()
方法接收串口返回的代码,
问题是:发送接收一次后,大约要等15秒后,再发送,才能接收到数据,能不能让这时间间隔短一点??
这个时间是与哪些设置有关?
通过Write方法向串口发送代码,用serialPort.ReadByte()
方法接收串口返回的代码,
问题是:发送接收一次后,大约要等15秒后,再发送,才能接收到数据,能不能让这时间间隔短一点??
这个时间是与哪些设置有关?
另外,使用DataReceived事件来处理读取数据,更好一些。
// spcom 是SerialPort 控件, tmpb 是要发的byte[] 数组
读取:
private void spcom_DataReceived(object sender, SerialDataReceivedEventArgs e)
{ try
{
int bytesRead = spcom.BytesToRead;
byte[] bytesData = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{ bytesData[i] = Convert.ToByte(spcom.ReadByte()); }
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 4; i < bytesRead-1; i++)
sb.Append(bytesData[i].ToString("x2")).Append(" ");
string code = sb.ToString().ToUpper();
MessageBox.Show(code);
spcom.DiscardInBuffer();
spcom.DiscardOutBuffer();
}
catch
{
MessageBox.Show("Error");
}
}