c#中,接收串口传送过来的数据,DataReceived就是不能触发Sp_DataReceived函数,下面是源码
//绑定事件处理函数privatestaticvoidSp_DataReceived(objectsender,SerialDataReceivedEventArgse)...
//绑定事件处理函数
private static void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//接收串口数据
SerialPort sp = sender as SerialPort;
if (sp == null)
return;
//readBuffer就是接受到的数据
byte[] readBuffer = new byte[sp.ReadBufferSize];
//从串口输入缓冲区一些字节并将那些字节写入字节数组中指定的偏移位处
sp.Read(readBuffer, 0, readBuffer.Length);
//byte[]转字符串
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
String StringMessage = ASCII.GetString(readBuffer);
Console.Write(StringMessage); //表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入
} 展开
private static void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//接收串口数据
SerialPort sp = sender as SerialPort;
if (sp == null)
return;
//readBuffer就是接受到的数据
byte[] readBuffer = new byte[sp.ReadBufferSize];
//从串口输入缓冲区一些字节并将那些字节写入字节数组中指定的偏移位处
sp.Read(readBuffer, 0, readBuffer.Length);
//byte[]转字符串
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
String StringMessage = ASCII.GetString(readBuffer);
Console.Write(StringMessage); //表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询