用于实时接收数据字节的代码。 [英] Code for receiving bytes of data in real time.

查看:51
本文介绍了用于实时接收数据字节的代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好



我是巴西人,所以我为我的英语道歉。



我有一个新的挑战,获得一组10个字节,来自模拟器传感器Carr创建显示一些信息,如速度,消耗,温度,速度......等......



我实时收到这些字节有些困难...到目前为止,我发现这样做的方式,代码如下所示,但不确定它是否适合此角色。任何人都有任何想法?



此代码只是一个接收数据的测试,在标签中显示更好的可视化,在点击之后它将以逻辑方式处理这组字节。



---代码---



<前lang =cs> 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.IO.Ports;

命名空间 Teste
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false ;
}
私有 void button1_Click( object sender,EventArgs e)
{
serialPort1.PortName = comboBox1.Text;
serialPort1.Open(); // abre porta serial selecionada
}

private void serialPort1_DataReceived( object sender,System.IO。 Ports.SerialDataReceivedEventArgs e)
{
int i = serialPort1.BytesToRead;

if (i > = 10
{
byte [] dados = new byte [i];
serialPort1.Read(dados, 0 ,i);
label1.Text = + dados [ 0 ];
label2.Text = + dados [ 1 ];
label3.Text = + dados [ 2 ];
label4.Text = + dados [ 3 ];
label5.Text = + dados [ 4 ];
label6.Text = + dados [ 5 ];
label7.Text = + dados [ 6 ];
label8.Text = + dados [ 7 ];
label9.Text = + dados [ 8 ];
label10.Text = + dados [ 9 ];

}

}
}
}





感谢您的帮助。



问候!



Christiano De Carli

控制工程专业学生自动化

Caxias do Sul - RS - BRASIL

解决方案

您可以在最近的一系列文章中找到有用的东西:

实时分析大数据 [ ^ ]


早上好(在你所在的时区?)



似乎有什么问题?我认为Wes关于CheckForIllegalCrossThreadCalls的评论是有效的,但除此之外,它有什么问题?



我唯一看到的是你读取BytesToRead的数量,但只能在你的标签中显示10个。因此,有更多的数据会丢失,并使数据无序。意思是,你得到12个字节,读取所有12个字节,只显示10.其他2个字节丢失。现在,以下8个(或更多)字节无序。你需要调查一下我猜。



祝你好运!


我想你必须缓冲收到的数据并处理只有当你确定你有一个完整的数据包时才能使用它们。


Good Morning

I am Brazilian, so I apologize for my English.

I have a new challenge, get a set of 10 bytes, coming from a simulator Sensors Carr to create a display showing some information, such as speed, consumption, temperature, speed ... etc ...

I'm having some difficulties to receive these bytes in real time ... so far the way I found to do this, the code is shown below, but not sure if it's right for this role. Anyone have any idea?

This code is just a test for receiving data, showing in label to better visualize, after hitting it will treat this set of bytes otherwise logically.

---CODE---

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace Teste
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.Text;
serialPort1.Open(); //abre porta serial selecionada
}

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int i = serialPort1.BytesToRead;

if (i >= 10)
{
byte[] dados = new byte[ i ];
serialPort1.Read(dados, 0, i);
label1.Text = "" + dados[0];
label2.Text = "" + dados[1];
label3.Text = "" + dados[2];
label4.Text = "" + dados[3];
label5.Text = "" + dados[4];
label6.Text = "" + dados[5];
label7.Text = "" + dados[6];
label8.Text = "" + dados[7];
label9.Text = "" + dados[8];
label10.Text = "" + dados[9];

}

}
}
}



Thanks for the help.

Greetings!

Christiano De Carli
Student of Control Engineering and Automation
Caxias do Sul - RS - BRASIL

解决方案

You may find something useful in this recent serie of articles:
Analysing Big Data in Real Time[^]


Good morning (in your time zone?)

What seems to be the problem? I think the remark of Wes about CheckForIllegalCrossThreadCalls is valid, but other than that, what is wrong with it?

The only thing I see is that you read the number of BytesToRead but only can display 10 of them in your labels. So, it there is more data you would loose that and get data out of order. Meaning, you get 12 bytes, read all 12 and display only 10. The other 2 bytes are lost. The following 8 (or more) bytes are now out of order. You need to look into that I guess.

Good luck!


I suppose you have to bufferize the data received and process them only when you are sure you got a full packet.


这篇关于用于实时接收数据字节的代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆