//定义串口
private SerialPort comm;
private long received_count = 0;//接收计数
private long send_count = 0;//发送计数
byte c = 51;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comm = new SerialPort("COM4", 1200);
//添加事件注册
comm.DataReceived += comm_DataReceived;
comm.ReceivedBytesThreshold = 1;
}
//数据接收
void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
received_count += n;//增加接收计数
comm.Read(buf, 0, n);//读取缓冲数据
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler)(delegate
{
//依次的拼接出16进制字符串
string ReceivedStr = GetHexStrFromByte(buf);
this.label1.Text = GetAm(ReceivedStr);
}));
}
详细解决方案
SerialPort串口通讯中DataReceived事件不能触发
热度:4617 发布时间:2013-02-25 00:00:00.0
相关解决方案
- C# serialport+mschart读取下位机数据绘制实时曲线之进制转换,与mschart数据源绑定
- C# SerialPort mobile 蓝牙 系统慢的有关问题
- 网站发布之后,为什么收不到串口的数据呢?(IIS,c#SerialPort),该怎么解决
- Android串口操作,简单化android-serialport-api的demo
- 帮忙看看 android-serialport-api 要如何下载源码下来
- 网站发布之后,为什么收不到串口的数据呢?(IIS,c#SerialPort),该怎么解决
- .Net串口开发(SerialPort)发数据时惟独关闭串口对象时另一台设备才能得到数据
- Unknown module(s) in QT: serialport,error: ‘QtSerialPort‘ file not found,两种解决方法
- android-serialport-api源码(c代码部分)
- Android串口开发(android-serialport-api)
- C# SerialPort 使用DataReceived接口利用空闲中断原理接收完整一帧数据
- dot Net SerialPort 组件关闭时卡住