应该怎么把接收到的串口数据显示在,TextBox上.接收到的串口数据是一组16进制的byte[]
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.IO.Ports; namespace _2 { public partial class Form1 : Form { SerialPort spReceive = new SerialPort (); delegate void UpdateTextEventHandler(string text);//声明一个委托 UpdateTextEventHandler updateText;//创建一个委托实例 string str; public Form1() { InitializeComponent(); updateText = new UpdateTextEventHandler(UpdateTextBox); spReceive.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(spReceive_DataReceived ); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { spReceive.PortName = "COM5"; spReceive.BaudRate = 2400; spReceive.DataBits = 8; spReceive.StopBits = StopBits.One; spReceive.Parity = Parity.Even; string str = "FEFEFE68AAAAAAAAAAAA681300DF16"; spReceive.Open(); spReceive.Write(HexToByte (str ),0,HexToByte (str).Length ); public void spReceive_DataReceived(object sender,System .IO .Ports .SerialDataReceivedEventArgs e) { string readString = spReceive.ReadExisting(); this.Invoke(updateText ,new string []{readString }); } private void UpdateTextBox(string text) { textBox1.Text =str ; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { spReceive.Close(); } private static byte[] HexToByte(string hexString)//字串符转换成16进制byte[] { byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; }上面代码已经实现向串口发送数据,串口也正确的返回数据了(用串口监控软件已经监控到数据的正常发送和返回)怎么把串口返回的数据显示在textBox1上?返回数据是一组16进制的byte[]例:返回的是68 11 11 11 11 11 11 68 00 在 textBox1上也显示68 11 11 11 11 11 11 68 00
那你把byte【】 數組,轉換成字符串 。
求代码.我不是要byte[]转换成字符串的代码.我是求把返回数据显示在textbox的代码
string m = "";
byte[] bytes = { 0, 1, 14, 168, 255 };
foreach (byte byteValue in bytes)
m += byteValue.ToString();
stringBulider 使用这个进行字符串追加就ok
显示为16进制数或者10进制数吧。