串口读数据 [英] Serial port reading data

查看:98
本文介绍了串口读数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个应用程序以进行数据交换,我在C#中开发了一个应用程序,我也可以发送和接收数据,但问题是我将数据作为字符ASCII。我希望逐字节地接收HEX中的数据,我在网上搜索但是我还没有找到解决方案,所以我想如果有人帮助以及如何接收数据IN六进制字节。



这是我接收数据的代码



  String 数据; 
private void serialPort1_DataReceived( object sender,SerialDataReceivedEventArgs e)
{
data + = serialPort1.ReadExisting();
this .Invoke( new EventHandler(display_data_event));
}

private void display_data_event( object sender,EventArgs e)
{
textBox2.Text = data;
}

解决方案

然后停止以字符串形式读取数据,并将其作为字节读取: SerialPort.Read方法(Byte [],Int32,Int32)(System.IO.Ports) [ ^

Hi, I designed an application in order to make data exchange , I developed an application in C # , I can sent and receive data as well, but the problem is that I get the data as character ASCII . I would like to receive the data in HEX byte by byte , I searched the in the net but I have not found a solution , so I would like if some one help and how to receive the data IN hexa byte .

Here is my piece of code to receive the data

String data;
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        data += serialPort1.ReadExisting();
        this.Invoke(new EventHandler(display_data_event));
    }
 
    private void display_data_event(object sender, EventArgs e)
    {
        textBox2.Text = data;
    }

解决方案

Then stop reading the data as a string, and read it as bytes: SerialPort.Read Method (Byte[], Int32, Int32) (System.IO.Ports)[^]


这篇关于串口读数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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