| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1264 人关注过本帖
标题:C#串口通信中当我点选HexSend发送的时候将输入框数据转化为十六进制,取消点 ...
只看楼主 加入收藏
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:2 
C#串口通信中当我点选HexSend发送的时候将输入框数据转化为十六进制,取消点选将数据转化过来,点选HexView将接收框数据转化为十六进制,取消点选将数据转
代码如下,请大侠帮忙完善,不胜感激
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using System.Text.RegularExpressions;
using System.Threading;


namespace TESS
{
    public partial class fomMain : Form
    {


        public fomMain()
        {
            InitializeComponent();
        }


        private void fomMain_Load(object sender, EventArgs e)
        {

            for (int i = 1; i < 15; i++)
            {
                cmbxComNum.Items.Add("COM" + i.ToString());

            }
            string[] sacheck = Enum.GetNames((typeof()));
            for (int i = 0; i < sacheck.Length; i++)
            {
                cmbxCheck.Items.Add(sacheck[i]);
            }
            string[] saStop = Enum.GetNames((typeof()));
            for (int i = 0; i < saStop.Length; i++)
            {
                cmbxStop.Items.Add(saStop[i]);
            }
            cmbxBaudRate.SelectedIndex = 3;
            cmbxCheck.SelectedIndex = 1;
            cmbxData.SelectedIndex = 0;
            cmbxComNum.SelectedIndex = 0;
            cmbxStop.SelectedIndex = 1;

        }


        private void btnOpen_Click(object sender, EventArgs e)
        {
            serialPortMain.PortName = cmbxComNum.SelectedItem.ToString();
            serialPortMain.BaudRate = int.Parse(cmbxBaudRate.SelectedItem.ToString());
            serialPortMain.Parity = (Parity)(Enum.Parse(typeof(Parity), cmbxCheck.SelectedItem.ToString()));
            serialPortMain.StopBits = (StopBits)(Enum.Parse(typeof(StopBits), cmbxStop.SelectedItem.ToString()));
            //serialPortMain.StopBits = StopBits.One;
            try
            {
                serialPortMain.Open();
                btnOpen.Enabled = false;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (serialPortMain.IsOpen)
            {
                serialPortMain.Close();
            }
            btnOpen.Enabled = true;
        }
        private void btnSend_Click(object sender, EventArgs e)
        {

            nt bytes = serialPortMain.BytesToRead; // 获取字节长度  
            byte[] buffer = new byte[bytes];// 创建字节数组
            serialPortMain.Read(buffer, 0, bytes);// 读取缓冲区的数据到数组
            string str = "";
            for (int i = 0; i < buffer.Length; i++)
            {
                str += buffer[i].ToString("X2");
            }

            serialPortMain.WriteLine(txtSend.Text.Trim());
            

        }
        private void serialPortMain_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            this.Invoke((EventHandler)
               (delegate
                {
                    int bytes = serialPortMain.BytesToRead; // 获取字节长度  
                    byte[] buffer = new byte[bytes];// 创建字节数组               
                    serialPortMain.Read(buffer, 0, bytes);// 读取缓冲区的数据到数组,参数1字节数组,参数2数组起始位,参数3数组长度
                    if (ckbxView.Checked)
                    {
                        string str = "";
                        for (int i = 0; i < buffer.Length; i++)
                        {
                            str += buffer[i].ToString("X2");
                        }
                        txtRcv.Text = str;
                        //txtRcv.AppendText("\r\n" + DateTime.Now.ToString() + "接收到数据:" +str );
                    }
                    else
                    {
                        txtRcv.AppendText(Encoding.ASCII.GetString(buffer));

                    }


                }

               )
            );

        }
        
        private void ckbxSend_CheckedChanged(object sender, EventArgs e)
        {
            string hexOutput = "";
            if (ckbxSend.Checked)
            {


                char[] values = txtSend.Text.ToCharArray();
                foreach (char letter in values)
                {
                    int value = Convert.ToInt32(letter);
                    hexOutput += String.Format("{0:X}", value);
                }
                txtSend.Text = hexOutput;
            }
           
            else
            {
               
            }           
        }

        private void ckbxView_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbxView.Checked)
            {
                string hexOutput = "";
                char[] values = txtRcv.Text.ToCharArray();
                foreach (char letter in values)
                {
                    int value = Convert.ToInt32(letter);
                    hexOutput += String.Format("{0:X}", value);
                }
                txtRcv.Text = hexOutput;
            }
            else
            {

            }

        }






        }
 }
搜索更多相关主题的帖子: 十六进制 不胜感激 
2011-03-23 13:20
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
收藏
得分:0 
,哪位大侠帮忙完善一下,急用
2011-03-24 09:06
nbzwnz
Rank: 2
等 级:等待验证会员
帖 子:6
专家分:27
注 册:2011-2-16
收藏
得分:20 
我也期待中。。。。
2011-03-25 14:59
快速回复:C#串口通信中当我点选HexSend发送的时候将输入框数据转化为十六进制, ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028759 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved