| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1345 人关注过本帖
标题:c#的串口通信问题,大大们帮忙看下是怎么回事啊!
取消只看楼主 加入收藏
gjq52199
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-10-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
c#的串口通信问题,大大们帮忙看下是怎么回事啊!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using

namespace _05
{
    public partial class Reader : Form
    {
        public Reader()
        {
            InitializeComponent();
        }

        private void CCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            serialPort1.Close();
            button1.Text = "打 开";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "打 开")
            {
                serialPort1.Open();
                button1.Text = "关 闭";
            }
            else
            {
                serialPort1.Close();
                button1.Text = "打 开";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            serialPort1.Close();
            button1.Text = "打 开";
        }

        private void Baud_SelectedIndexChanged(object sender, EventArgs e)
        {
            serialPort1.Close();
            button1.Text = "打 开";
        }

        private void serialPort1_DataReceived(object sender, e)
        {
            string serialReadString = serialPort1.ReadExisting();
             this.richTextBox1.Invoke
                 (
                 new MethodInvoker
                     (

                       delegate
                       {

                           this.richTextBox1.AppendText(serialReadString);
                       }

                     )

                 );
        }

        private void Reader_Load(object sender, EventArgs e)
        {
            chuankou();
            botelv();
            serialPort1.PortName = CCOM.Text;
            serialPort1.BaudRate = int.Parse(CBaud.Text);
            serialPort1.DataBits = 8;
            serialPort1.Parity =
            serialPort1.StopBits =
            serialPort1.ReadBufferSize = 1024;
        }
        private void chuankou()
        {
            CBaud.Items.Clear();
            foreach (string portName in ())
            {
                SerialPort handler = new SerialPort(portName);
                try
                {
                    handler.Open();
                    CCOM.Items.Add(portName);
                    //CCOM.Text = "COM1";
                    //comcom.Items.Add(portName);
                    CCOM.Text = portName;
                }
                catch (Exception)
                {

                }

                finally
                {
                    handler.Close();
                }
            }
        }
        private void botelv()
        {

            CBaud.Items.Add("2400");
            CBaud.Items.Add("4800");
            CBaud.Items.Add("9600");
            CBaud.Items.Add("38400");
            CBaud.Text = "38400";
        }
    }
}
下面是接收并且显示到richbox中的数据,每个数据是11字节,数据格式是:02开头(1字节),读头编号(1字节),数据类型(1字节),数据长度(1字节),通信数据(6字节),校验位(1字节)。
0384EB,03D2FA{,03638Ct,0278F3q,01CF83,03E116y。
但是当接收数据格式为02 32 45 41 44(例)的时候显示的就全是“???????”这是什么原因啊?请各位大大们解惑,还有,如何将接收到的信息(如02 32 45 41 44)原封不动的显示到richbox里啊(就是也显示02 32 45 41 44)
搜索更多相关主题的帖子: private public class using 
2011-10-15 16:02
gjq52199
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-10-15
收藏
得分:0 
回复 2楼 yhlvht
这为大大能不能帮忙看看啊,小弟初学c#,懂的不是很多,还请不吝赐教,谢谢啊。
2011-10-16 18:27
gjq52199
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-10-15
收藏
得分:0 
回复 4楼 panxue
你把你的代码发过来我看下,另外,我也是新手,不一定能帮到你哦,呵呵~
2011-10-17 11:25
快速回复:c#的串口通信问题,大大们帮忙看下是怎么回事啊!
数据加载中...
 
   



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

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