110,538
社区成员
发帖
与我相关
我的任务
分享
comm.ReceivedBytesThreshold = 5;// 触发条件,
//添加事件注册
comm.DataReceived += comm_DataReceived;
int n = comm.BytesToRead;
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
string str = "";
received_count += n;//增加接收计数
comm.Read(buf, 0, n);//读取缓冲数据
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler)(delegate
{
for (int i = 0; i < n; i++)
{
str += buf[i].ToString("X2");
}
this.txGet.AppendText(str);
//修改接收计数
labelGetCount.Text = "Get:" + received_count.ToString();
}));
int n = comm.BytesToRead;
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
string str = "";
received_count += n;//增加接收计数
comm.Read(buf, 0, n);//读取缓冲数据
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler)(delegate
{
for (int i = 0; i < n; i++)
{
str += buf[i].ToString("X2");
}
this.txGet.AppendText(str);
//修改接收计数
labelGetCount.Text = "Get:" + received_count.ToString();
}));
int n = comm.BytesToRead;//
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
bool data_1_catched = false;
string str = "";
received_count += n;//增加接收计数
temp_Counter += n;
comm.Read(buf, 0, n);//读取缓冲数据
if (n > 0)
{
Array.ConstrainedCopy(buf, 0, binary_data_1, temp_Counter - n, n);
if (temp_Counter - n > 0)
data_1_catched = true;
//正确分析一条数据,从缓存中移除数据。
Array.Clear(buf, 0, buf.Length);
}
if (data_1_catched)
{
this.Invoke((EventHandler)(delegate
{
for (int i = 0; i < temp_Counter; i++)
{
str += binary_data_1[i].ToString("X2");
}
this.txGet.AppendText(str);
temp_Counter = 0;
Array.Clear(binary_data_1, 0, binary_data_1.Length);
//修改接收计数
labelGetCount.Text = "Get:" + received_count.ToString();
}));