serialPort.Open()后一切正常,然后:
string tempData = "";
if (serialPort.BytesToRead > 0)
{
tempData = serialPort.ReadLine();//本句一执行就死机!
tempData = "";
}
请问这是何原因?谢谢!
string tempData = "";
if (serialPort.BytesToRead > 0)
{
tempData = serialPort.ReadLine();//本句一执行就死机!
tempData = "";
}
请问这是何原因?谢谢!
(换行符设置方法:serial.NewLine="\r\n")
2.一读就死机,你试试不要换行,直接ReadExisting或是Read出一个数组试试。
如果真的读了就死机,试试串口工具sscomm32.exe,如果还是死机,可能硬件问题,驱动问题,或是终端资源设置错误,查询IRQ是否冲突。
int bytecount = serialPort1.BytesToRead;
byte[] byteBar = new byte[bytecount];
serialPort1.Read(byteBar, 0, bytecount);
for (int i = 0; i < byteBar.Length; i++)
{
ls_Rev += (char)byteBar[i];
}
if (bytesCanRead > 0)
{
byte[] bzData = new byte[bytesCanRead];
((System.IO.Ports.SerialPort)sender).Read(bzData, 0, bytesCanRead);
ReceiveData(Encoding.ASCII.GetString(bzData));
}