实例了一个SerialPort spprivate void AutoRead(object sender, EventArgs e)//自动接收事件响应
{
if ((line = sp.ReadLine()) != null)//到这里就不行了
{
byte[] readdata = Convert.FromBase64String(line);
Received_Msg = Encoding.Unicode.GetString(readdata);
。}C#串口通信SerialPort
{
if ((line = sp.ReadLine()) != null)//到这里就不行了
{
byte[] readdata = Convert.FromBase64String(line);
Received_Msg = Encoding.Unicode.GetString(readdata);
。}C#串口通信SerialPort
10年就有一模一样的问题解决过了
sp.ReadLine()是线程阻塞的,机器向串口发送数据然后 串口有数据接收时,sp.ReadLine()才会继续向下执行。
因为下位机没发\n,所以一直卡在这里等待。
sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);