文号
t79772
阅读
17586
回复
2
收藏
3
一个C#串口通讯的接收异常问题
2016/04/29
IP:山东
最近为我的一个单片机联机控制作品写上位机,考虑后,选择了C#,我之前一直在学单片机C语言,没接触过Windows编程语言,所以这次也是第一次接触C#,边摸索,边搜索资料边写。串口接收那里遇到了一个奇怪的问题,在网上搜索相关串口通讯程序研究后仍未解决问题,请各位帮忙看看问题出在哪,为了方便查看,我直接贴出故障功能模块的程序

基本意图:2个不同的图片当作指示灯(pictureBox),当串口收到指定数据后(如0X1E),pictureBox3可视,pictureBox4不可视;当串口收到指定数据后(如0X1D),pictureBox3不可视,pictureBox4可视,这样就实现了指示灯变色效果。


故障现象:发送“IE”,分别收到正常的数据“1E”;再次发送,会分别收到1次“1”和1次“E” (数据被分开了)
如果等一会儿(时间挺长)再发送一次,就会从头重复这个过程,如果不等待(间隔短)一直发下去,就一直出现数据被分开的情况

笔者自我排查故障所得:如果将波特率设置为115200,那么程序一切正常,可是设置到其他值都不正常,按说9600的波特率应该也正常才对啊

源代码如下:
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.Ports;
         
         
namespace SerialPortTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //display();
        }
         
                
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
                    comboBox1.Items.Add(com);
            }
            catch (Exception er)
            {
                MessageBox.Show("端口加载失败!" + er.Message, "提示");
            }
            comboBox2.SelectedIndex = 4;
            comboBox3.SelectedIndex = 2;
            comboBox4.SelectedIndex = 0;
            comboBox5.SelectedIndex = 1;
           // comboBox1.SelectedIndex = 0;
        }
         
        //打开串口
        private void btnOpen_Click(object sender, EventArgs e)
        {
         
            try
            {
                if (serialPort1.IsOpen)
                { serialPort1.Close(); }
                else
                {
                    serialPort1.BaudRate = int.Parse(comboBox2.Text.Trim());
                    serialPort1.DataBits = int.Parse(comboBox3.Text.Trim());
                    serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox4.Text.Trim());
                    serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox5.Text.Trim());
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.Open();
                      
                }
                groupBox1.Enabled = !serialPort1.IsOpen;
                if (btnOpen.Text == "打开串口")
                {
                    btnOpen.Text = "关闭串口";
                    pictureBox1.Visible = true;
                    pictureBox2.Visible = false;
                }
                else if (btnOpen.Text == "关闭串口")
                {
                    btnOpen.Text = "打开串口";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = true;
                }
         
                       
         
         
            }
            catch (Exception er)
            {
                MessageBox.Show("串口打开失败!" + er.Message, "提示");
            }
        }
         
         
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == false)
            {
                MessageBox.Show("请先打开串口!");
                return;
            }
            SendData();//发送数据传递textBox1.text中数据
            //display();
        }
         
         
         
        //接收数据
         
        string Recive;  
                
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
                     
            Invoke
             (new EventHandler
               (delegate
               {
                           
                  // textBox2.Text += serialPort1.ReadExisting();  
       
             //这里注释掉,经试验,textBox和Repeat同时接受会互相干扰,出现收到奇怪的数据
       
       
                   Recive = serialPort1.ReadExisting();       
                                              
             
                   if (Recive.Trim() == "1E")
                   {
                       pictureBox3.Visible = true;
                       pictureBox4.Visible = false;
                         
                             
                   }
         
                   MessageBox.Show(Repeat);        //查看当前收到了什么,排查故障用
         
                   if (Recive.Trim() == "1D") 
                   {
                       pictureBox3.Visible = false;
                       pictureBox4.Visible = true;
                        
                              
                   }
         
                   MessageBox.Show(Repeat);       //查看当前收到了什么,排查故障用
         
         
         
               }
               )
              );
        }
         
        //向串口发送数据
        public void SendData()
        {
            for (int i = 0; i < textBox1.Lines.Count(); i++)
            {
                serialPort1.WriteLine(textBox1.Lines);
                                         
            }
        }

运行状态:
QQ截图20160429212903.jpg



QQ截图20160429212911.jpg
[ 修改于10年7个月前 - 2016/04/29 22:46:15 ]
10年7个月前IP:广东
817771
1楼
miss掉数据,大概就3种原因吧
1 比特率不对
2 干扰
3 单片机太忙
引用
评论
10年7个月前IP:江苏
817788
2楼
byte recive =serialPort1.readReadbyte();试试
直接读取字节数据   不转成字符串
引用
评论
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的