| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1672 人关注过本帖, 1 人收藏
标题:C#串口通信
只看楼主 加入收藏
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
结帖率:66.67%
收藏(1)
已结贴  问题点数:20 回复次数:11 
C#串口通信
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-28 14:31
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
收藏
得分:0 
,高手大侠尽快指点啊,不胜感激,谢谢了 啊
2011-03-28 14:35
yehuanfeng
Rank: 4
等 级:业余侠客
帖 子:75
专家分:201
注 册:2010-8-3
收藏
得分:0 
挺长的,把关键代码拎出来嘛
2011-03-28 14:43
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
收藏
得分:0 
回复 3楼 yehuanfeng
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-29 14:52
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
收藏
得分:0 
回复 3楼 yehuanfeng
关键代码已经找出,请帮忙解决问题,谢谢
2011-03-29 14:54
yehuanfeng
Rank: 4
等 级:业余侠客
帖 子:75
专家分:201
注 册:2010-8-3
收藏
得分:0 
      private void ckbxSend_CheckedChanged(object sender, EventArgs e)
        {            
            byte [] array=new byte[txtSend.Text.Length];
            array = Encoding.ASCII.GetBytes(txtSend.Text.Trim());
            string str = ByteArrayToHexString(array);
            txtSend.Text = str;
        }

        private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
            {
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
                sb.Append(" ");
            }
            return sb.ToString().ToUpper();
        }

试试吧,有问题再说
2011-03-29 18:51
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
收藏
得分:0 
回复 5楼 ykp
您好,我现在的问题还是没解决,例如:我在输入框输入11,当我点击的时候将11转化为十六进制即31 31,当我取消点选的时候,将31 31转化为11,你QQ多少,方便告诉我的话,就说一下,我的350674330
2011-03-30 14:42
yehuanfeng
Rank: 4
等 级:业余侠客
帖 子:75
专家分:201
注 册:2010-8-3
收藏
得分:0 
你的意思就是说点了以后能将11转换为31 31,但是去掉以后不能把31 31转换成11对吧?我想这个工作应该你自己做了,你说对吧?
2011-03-30 15:30
yehuanfeng
Rank: 4
等 级:业余侠客
帖 子:75
专家分:201
注 册:2010-8-3
收藏
得分:0 
       private void ckbxView_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbxView.Checked)
            {
              //这里的代码将字符创转化成十六进制
            }
            else
            {
              //这里的代码将十六进制转化成字符串
            }
        }

        }

2011-03-30 15:33
yehuanfeng
Rank: 4
等 级:业余侠客
帖 子:75
专家分:201
注 册:2010-8-3
收藏
得分:20 
        
程序代码:
String res=null;
        private void ckbxSend_CheckedChanged(object sender, EventArgs e)
        {
           if (ckbxView.Checked)
            {           
            byte [] array=new byte[txtSend.Text.Length];
            array = Encoding.ASCII.GetBytes(txtSend.Text.Trim());
            string str = ByteArrayToHexString(array);
            res=txtSend.Text;
            txtSend.Text = str;
            }
           else
           txtSend.Text=res;
        }

        private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
            {
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
                sb.Append(" ");
            }
            return sb.ToString().ToUpper();
        }

弄个小技巧,现在应该没问题了
2011-03-30 15:55
快速回复:C#串口通信
数据加载中...
 
   



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

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