最近在做医疗传感器的端口数据读取,希望边读边存到TXT中。串口数据一直在进
这是部分代码        private void button4_Click(object sender, EventArgs e)
        {   //接收数据
            byte[] data = new byte[port1.BytesToRead];
            port1.Read(data, 0, data.Length);
            string ss;
            ss = byteToHexStr(data);   //十六进制显示      
            Write(ss);
         }
 为什么只存了部分数据,没有一直储存。  难道只是read读取的缓冲区 ?  缓冲区满了就读不了了吗?              

解决方案 »

  1.   

    ”串口数据一直在进“  你这样写代码肯定只能读到一部分了参考以下代码吧
    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 读取串口数据
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            System.IO.Ports.SerialPort comport = new System.IO.Ports.SerialPort();
            private void Form1_Load(object sender, EventArgs e)
            {
                comport.BaudRate = 9600;
                comport.DataBits = 8;
                comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "One");
                comport.Parity = (Parity)Enum.Parse(typeof(Parity), "None");
                comport.Encoding = Encoding.GetEncoding("utf-8");//Encoding.GetEncoding("utf-8")和Encoding.Default
                comport.PortName = "COM14";
                comport.Open();
                comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            }
            private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                try
                {
                   string   data = comport.ReadExisting();
                    this.textBox1.Invoke(new EventHandler(delegate
                    {
                        string[] arr = data.Split('|');
                        if (arr.Length > 1)
                        {
                            this.textBox1.Text = this.textBox1.Text +arr[1] ;
                        }
                    }));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
      

  2.   

    comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);注册数据接收到时触发的事件,在事件读取数据就行了
      

  3.   

    设置一seek,如30,读一次减一,读到0为止还没完就算他超时喽。
      

  4.   


    哪错了??传输的数据会有可能漏字节byte。数据就不对了。你这个怎么避免这个问题。我是这个意思。
      

  5.   

    1,编写通讯协议,
    2,当DataReceived时就读取并放到缓存中,并读缓存中是否够一帧够了则解析,不够继续读。
      

  6.   


    哪错了??传输的数据会有可能漏字节byte。数据就不对了。你这个怎么避免这个问题。我是这个意思。数据是否有结束标志 ??
      

  7.   

    comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    用这个没错。即便串口数据一直在进那也是有个是规则的。如一条数据有几位表示。校验位是那个。你读取的时候校验一下,通过再记录,否则直接当错数据丢掉。
      

  8.   

    你没有遇到过这种情况??
    你传输ABCDE这个组合100次,然后你收到的应该是ABCDEABCDE这个样的数据总共100次循环。
    但是有可能未知情况会导致ABCDEACDE这种情况出现,B不见了,没有任何理由,就是没了。你怎么办?
      

  9.   

            #region 开始发送串口指令
            private void SerialPortStart(List<byte[]> sendBuffers, List<Metric> metricList)
            {
                try
                {
                    while (true)
                    {
                        foreach (byte[] sendBuf in sendBuffers)
                        {
                            byte[] addrBuf = new byte[2];
                            addrBuf[0] = sendBuf[2];
                            addrBuf[1] = sendBuf[3];
                            Metric metric = GetSerialPortMetricModel(metricList, addrBuf);                        if (metric != null)
                            {
                                byte[] tmp = ReceiveData(sendBuf, metric);                            if (tmp != null)
                                {
                                    ReceiveBufState state = new ReceiveBufState();
                                    state.Metric = metric;
                                    state.RecvBuf = tmp;
                                    ThreadPool.QueueUserWorkItem(new WaitCallback(DecBuffers), state);
                                    m_autoPortEvent.WaitOne(100, false);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    SerialPortStart(sendBuffers, metricList);
                }
            }
            #endregion
            #region 开始一个串口收发
            private byte[] ReceiveData(byte[] sendBuf,Metric metric)
            {
                byte[] recvData = null;
                while (true)
                {
                    int toatalWaitTime = 0;
                    SerialPort SerialPortClient = SerialPorts[metric.Equipment.ID];
                    
                    SerialPortClient.Write(sendBuf, 0, sendBuf.Length);
                    m_autoPortEvent.WaitOne(5, false);
                    int i = 5;
                    try
                    {
                        while (i > 0)
                        {
                            int readBufCount = SerialPortClient.BytesToRead;
                            if (readBufCount > 8)
                            {
                                m_autoPortEvent.WaitOne(40, false);
                                readBufCount = SerialPortClient.BytesToRead;
                                recvData = new byte[readBufCount];
                                SerialPortClient.Read(recvData, 0, recvData.Length);
                                return recvData;
                            }
                            i--;
                        }
                        if (recvData == null)
                        {
                            throw new Exception("time out for getData");
                        }
                    }
                    catch (Exception ex)
                    { }
                }
                return null;
            }
            #endregion
      

  10.   

    给你一个我的串口收发,在我的环境里反正是没出错,是在wince里的。windows里肯定能用。
      

  11.   


       设置好相关端口 添加textbox1后这个程序直接能跑吗  我是新手,麻烦解答下。。
      

  12.   

    不是很懂这句话
    另外
    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    是哪个控件的代码语句? 我是新手。。
      

  13.   

                comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);comport注册的
      

  14.   

    port1.Read(data, 0, data.Length);不能循环读取串口数据
    楼主先学习“异步操作”的概念  再来理解1楼所说的对象.DoSomething(); //一般DoSomething()返回 并不代表事情已经做完了  读取串口数据是一个漫长的过程
      

  15.   


    能发个工程文件吗。。实在是有困难 弄了好几天了 十分感谢! [email protected]