首页 新闻 赞助 找找看

接收串口数据显示

0
悬赏园豆:5 [已关闭问题] 关闭于 2012-05-23 11:16

应该怎么把接收到的串口数据显示在,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
C#
一坨黄色的主页 一坨黄色 | 初学一级 | 园豆:13
提问于:2012-05-18 11:27
< >
分享
所有回答(4)
0

那你把byte【】 數組,轉換成字符串 。

無限遐想 | 园豆:3740 (老鸟四级) | 2012-05-18 11:33
0

求代码.我不是要byte[]转换成字符串的代码.我是求把返回数据显示在textbox的代码

一坨黄色 | 园豆:13 (初学一级) | 2012-05-18 11:40

         string m = "";
            byte[] bytes = { 0, 1, 14, 168, 255 };
            foreach (byte byteValue in bytes)
                m += byteValue.ToString();

支持(0) 反对(0) 無限遐想 | 园豆:3740 (老鸟四级) | 2012-05-18 11:50
0

stringBulider  使用这个进行字符串追加就ok

wvsy | 园豆:297 (菜鸟二级) | 2012-05-18 13:32
0

显示为16进制数或者10进制数吧。

forhells | 园豆:724 (小虾三级) | 2012-05-20 15:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册