发送&从serialport1行接收数据 [英] send & receive data from serialport1 line

查看:266
本文介绍了发送&从serialport1行接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我需要通过串口连接微控制器。如果我点击一个按钮,它会向微控制器发送一个数据包,如果它收到这个,则发回特殊数据。

例如,我按下按钮发送:AA BB 80 01 FF 01 80

AA BB是帧的开始,01 80CRC,其他是数据

来自微控制器的此消息的回复是: BB AA 80 32(部分数据)(2字节CRC)

表示BB AA是帧的开始,2字节CRCCRC,其他是数据

所有这些数据都是十六进制代码!



问题1:当我按下按钮并将数据发送到微控制器时,我写了button_click事件

hi i need to connect via serial port to a micro controller. if i click on a button, it sends a packet of data to micro controller and if it receives this, sends back special data.
for ex., i send by press on button: "AA BB 80 01 FF 01 80"
that "AA BB" is start of frame, "01 80" CRC, and others are data
reply of this message from micro controller is: "BB AA 80 32 (some of data) (2 bytes CRC)"
that "BB AA" is start of frame, "2 bytes CRC" CRC, and others are data
all of these data are as hex code!

question1: when i press on button and send data to micro controller, and i wrote on button_click event

serialPort1.WriteLine("AA BB 01 FF 01 80");

。该数据(AA BB 01 FF 01 80)是否变为十六进制?因为微控制器只是读取十六进制!



问题2:如果这个数据由微控制器接收并且它回复数据为BB AA 80 32(部分数据)(2字节) CRC)。但现在,我需要使用十六进制代码中的一些数据,并在文本框中显示这些数据。我该怎么办?

. shall this data("AA BB 01 FF 01 80") change to hex? because micro controller just read hex!

question2: if this data received by micro controller and it replies data as "BB AA 80 32 (some of data) (2 bytes CRC)". but now, i need to use "some of data" that are in hex code, and display these on text box. what shall i do?

推荐答案

您可能应该使用 SerialPort.Write [ ^ ]和< a href =http://msdn.microsoft.com/en-us/library/ms143549(v=vs.110).aspx> SerialPort.Read [ ^ ]方法,用于处理纯十六进制数据。然后,您可以将输入字节转换为字符串数据以显示在文本框中。
You probably should be using the SerialPort.Write[^] and SerialPort.Read[^] methods, to handle pure hex data. You can then convert the input bytes to string data to display in your text box.


Quote:

该数据(AA BB 01 FF 01 80)是否变为十六进制?因为微控制器只是读取十六进制!

shall this data("AA BB 01 FF 01 80") change to hex? because micro controller just read hex!





可能(非常可能,确实)微控制器'理解'二进制数据 hex adecimal只是数字的文本表示,即二进制数据。)

正如 Richard 所建议的那样你必须写(和读)字节缓冲区,例如



Probably (very probably, indeed) the microcontroller 'understands' binary data (hexadecimal is just a textual representation of numbers, that is, binary data).
As already suggested by Richard you have to write (and read) byte buffers, e.g.

byte[] buf = { 0xAA, 0xBB, 0x80, 0x01, 0xFF, 0x01, 0x80 };
serialPort1.Write(buf, 0, buf.Length);


这篇关于发送&amp;从serialport1行接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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