当前位置:编程学习 > C#/ASP.NET >>

C# 串口接收数据单片机发回总是8位8位接收,如何将他们整合一起?

我串口设置为 9600,8,1,none,none,
发现如果用串口接收虚拟串口发出的数据,则是全部一瞬间收到,但是,如果是用单片机发送数据的话。就发现每次只能接收到8个字节丢调用一次serialPort_DataReceived事件,结果。每次都只能收到8个字节,
程序如下
public delegate void _SafetrTextCall(string text);
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int DateLenth = serialPort.BytesToRead;
            int i = 0;
            string datanum = null;
             StringBuilder sb = new StringBuilder();
            while (i < DateLenth)
            {
                byte[] bt = new byte[1024];
                int len = serialPort.Read(bt, 0, 1024);
                sb.Append(Encoding.ASCII.GetString(bt, 0, len));
                i += len;
            }
            
            datanum = sb.ToString();
            safetrText(datanum);
        }
string k=null;
#region  safetrText
        /// <summary>
        ///         /// </summary>
        /// <param name="text">接收到的数据</param>
        private void safetrText(string text)
        {
            try
            {

                if (this.InvokeRequired)
                {
                    _SafetrTextCall call =
                    delegate(string s)
                    {
                        if (s != null)
                        {
                            k = k + s;
                            
                             this.textBox1.Text = k;
                                this.textBox2.Text = (k.Length).ToString();
                                                                                  }
                    };
                    this.Invoke(call, text);

                }
            }
            catch
            {
                return;
            }
        }
        #endregion
用以上方法可以将整个数据都接收起来并显示。可是我总是觉得有些问题,希望各位大大能给点好建议
以上是接收字符串的写法。如何接收字节(byte)数组,也是一个难题。还有个问题是可有人知道如何接收到的字节连接起来?
我需要得到的是字节数组各位如果都是只知道字符串的就不要回答了。
例如 serialPort.ReadExisting();serialPort.ReadLine()等等就不用说了 --------------------编程问答-------------------- 自己做个缓冲,DataReceived只把数据放进缓冲区。
然后你自己处理不就得了 --------------------编程问答-------------------- 自己做个字节缓冲数组,你收到的单片机发过来的数据里,总应该有数据协议上的约定吧,类似于数据的头部,数据的BODY,数据的头部会写明数据的开始位和数据的长度吧,根据数据上的约定来处理缓冲数组内的内容不是就可以。 --------------------编程问答-------------------- 是有头有尾。可是如果发送数据期间单片机断电了,数据发送不完怎么办。
那啥 字节缓冲数组好像我也做过。只是好像有些不会怎么编写程序。各位大大能不能给点程序看看?借鉴下。。其实话我都会说。重点是怎么做 --------------------编程问答-------------------- 这个得自己写,弄个全局变量收到就往里存。用list<byte> --------------------编程问答--------------------
引用 4 楼 upc_xiaowei 的回复:
这个得自己写,弄个全局变量收到就往里存。用list<byte>


一般都byte[]  全部存在这里面就可以了,然后解析,但是c#串口也不是很完善,有时候接收不完, --------------------编程问答-------------------- 本人开发的时候都是用byte[]收  然后在一起显示  只是频率快点  基本没出过什么问题 --------------------编程问答--------------------
引用 4 楼 upc_xiaowei 的回复:
这个得自己写,弄个全局变量收到就往里存。用list<byte>

这个我试过。可惜不知道怎么搞,有程序不? --------------------编程问答--------------------
引用 6 楼 black1378411304 的回复:
本人开发的时候都是用byte[]收 然后在一起显示 只是频率快点 基本没出过什么问题

能把程序粘上来不?看看到底怎么实现撒 --------------------编程问答-------------------- 自己在网上找了个。测试感觉还是不错的。不知道有木有问题。
private List<byte> buffer = new List<byte>(4096);
        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int DateLenth = serialPort.BytesToRead;
            string tmpstr = null, strdata=null;
            #region 缓存方法
              byte[] buf = new byte[DateLenth];
            serialPort.Read(buf, 0, DateLenth);
            buffer.AddRange(buf);
            
            #endregion
            
            byte[] bytesData = new byte[buffer.Count];
            for (int i = 0; i < buffer.Count; i++)
            {
                bytesData[i] = (byte)buffer[i];
            }
            
                //buffer.
            tmpstr = Encoding.ASCII.GetString(bytesData);
           //假设以A为开始,B为结尾 本人愚笨。只能想到这个方法
                int w1 = tmpstr.IndexOf("A", 0);
                int w2 = tmpstr.IndexOf("B");
                int ww1 = tmpstr.LastIndexOf("A");
                int ww2 = tmpstr.LastIndexOf("B");

                if (w1 >= 0 && ww2 > w1 && tmpstr.Length > 6)
                {
                    strdata = tmpstr.Substring(w1, ww2 + 1 - w1);
                    byte[] bytedata = new byte[ww2 + 1 - w1];
                    for (int i = 0; i < w2 - w1 + 1; i++)
                    {
                        bytedata[i] = bytesData[i + w1];
                    }
                    buffer.Clear();
                    safemingltrText(strdata);
                }
            
        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,