当前位置: 代码迷 >> WinCE >> HELP!关于串口接收数据。解决方案
  详细解决方案

HELP!关于串口接收数据。解决方案

热度:173   发布时间:2016-04-28 11:59:35.0
HELP!!关于串口接收数据。
请教大家一个问题,关于WINCE串口接收数据的。

常规运行的话,第一次能接受到完整的数据。之后每次都只能接收到后半截数据。

如果我调试琢句运行的话,每次都能接收到完整的数据。

请问下这是什么原因呢?代码如下:

 //方式一,直接读取ASCII码。
                    //BarCode = serialPort1.ReadExisting();   

                    //方式二,读取字节,再转码
                    int n = serialPort1.BytesToRead;
                    byte[] buf = new byte[n];
                    serialPort1.Read(buf, 0, n);
                    builder.Remove(0, builder.Length);
                    BarCode = System.Text.Encoding.GetEncoding("GB2312").GetString(buf, 0, buf.Length);
                    //builder.Append(Encoding.ASCII.GetString(buf,0,buf.Length));
                    //BarCode = builder.ToString();

而且还有一点很奇怪的,

 }
                       if()
                        else
                        {
                            MessageBox.Show("请扫描正确的条码");
                        }


"请扫描正确的条码"会提示两次,说明代码执行了两遍。。下面是完整的代码。。
 try
            {
                this.Invoke(new EventHandler(delegate
                {
                    string BarCode = "";

                    //方式一,直接读取ASCII码。
                    //BarCode = serialPort1.ReadExisting();   

                    //方式二,读取字节,再转码
                    int n = serialPort1.BytesToRead;
                    byte[] buf = new byte[n];
                    serialPort1.Read(buf, 0, n);
                    builder.Remove(0, builder.Length);
                    BarCode = System.Text.Encoding.GetEncoding("GB2312").GetString(buf, 0, buf.Length);
                    //builder.Append(Encoding.ASCII.GetString(buf,0,buf.Length));
                    //BarCode = builder.ToString();

                    if (BarCode != "")
                    {

                        if (txtProcessId.Text == "")
                        {
                            MessageBox.Show("请选设置工序");
                            return;
                        }
  相关解决方案