【问题标题】:Read serial port work good in windows XP but stop working and slow in windows 7读取串行端口在 Windows XP 中工作良好,但在 Windows 7 中停止工作并且速度很慢
【发布时间】:2015-10-09 04:56:31
【问题描述】:

我说的是 C# 编程和与串行端口通信以及 Windows 7 和 XP 中的不同结果。 我的代码是:

    int count = 0;
    float data1;
    float data2;
    private void button1_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = textBox1.Text;
        serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);

        serialPort1.Open();
        serialPort1.Write("?");

    }

    private void button2_Click(object sender, EventArgs e)
    {
        serialPort1.Close();

    }

    private void button3_Click(object sender, EventArgs e)
    {
        //string pathfile = @"C:\Documents and Settings\Dlab\Desktop\";
        //string filename = "data1.txt";
        //System.IO.File.WriteAllText(pathfile + filename, chart1.SaveImage();
       // this.chart1.SaveImage(@"C:\Documents and Settings\Dlab\Desktop\data1p.png", System.Drawing.Imaging.ImageFormat.Gif);

        //Bitmap bmp1 = new Bitmap(500, 500);
        //chart1.DrawToBitmap(bmp1, new Rectangle(0, 0, 500, 500));
        //bmp1.Save(@"C:\Documents and Settings\Dlab\Desktop\data1b.png");

        //chart1.Serializer.Save(@"C:\Documents and Settings\Dlab\Desktop\data1t.text");



        //this.chart2.SaveImage(@"C:\Documents and Settings\Dlab\Desktop\data2p.png", System.Drawing.Imaging.ImageFormat.Gif);

        //Bitmap bmp2 = new Bitmap(500, 500);
        //chart2.DrawToBitmap(bmp2, new Rectangle(0, 0, 500, 500));
        //bmp2.Save(@"C:\Documents and Settings\Dlab\Desktop\data2b.png");

        //chart2.Serializer.Save(@"C:\Documents and Settings\Dlab\Desktop\data12.text");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // string[] ports = SerialPort.GetPortNames();
        //foreach (string port in ports)
        //{
        //   comboBox1.Items.Add(port);

        // }

    }

    byte[] rs = new byte[53];


    int rscnt = 0;
   // DateTime then = DateTime.Now;
   // float dt;
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {





        if (e.EventType != SerialData.Chars) return;
        rscnt += serialPort1.Read(rs, rscnt, 53 - rscnt);
        if (rscnt == 53)
        {
            this.BeginInvoke(new Action(() => type(rs)));
            rs = new byte[53];
            rscnt = 0;
        }

    }
    private void type(byte[] data)
    {
        //if (rs[0] == 65)
        //{ 
       // }
        //DateTime now = DateTime.Now;
        //dt = ((now.Second - then.Second));
        //label8.Text =  dt.ToString();
        //textBox3.Text = dt.ToString();


        data1 = ((rs[1] * 65536) + (rs[2] * 256) + (rs[3])-10000000)/100;
        data2 = ((rs[4] * 16777216) + (rs[5] * 65536) + (rs[6] * 256) + (rs[7])-1000000000)/2136;

        count++;
        label5.Text = count.ToString();
        label3.Text = data1.ToString();
        label4.Text = data2.ToString();

        //chart1.Series[0].Points.AddXY(count, data1);
        //chart2.Series[0].Points.AddXY(count, data2);

        //list1.Add(count, rs[1]);
        //zedGraphControl1.GraphPane.AddCurve("", list1, Color.Red);

        //zedGraphControl1.AxisChange();
        // zedGraphControl1.Refresh();
        serialPort1.DiscardInBuffer();



    }
    //  PointPairList list1 = new PointPairList();
}
}

这个程序在 Windows XP 中运行良好,但是当我在 Windows 7 中尝试时,它很慢,得到错误的数据,并且在一点点停止工作后。

我在 Windows 7 和 xp 中再次编写了这个程序,并在 Visual Studio 2008 和 2012 上对其进行了测试,但得到了相同的结果。

【问题讨论】:

  • 您是使用计算机内置的串口还是使用串口适配器?
  • “错误数据”是什么意思?您收到任何错误消息吗?
  • 我使用 USB 转 RS 458 转换器,一开始我没有收到任何错误,但过了一会儿我收到停止工作错误,错误的数据意味着我收到的 data1 和 data2 的值是错误的
  • 错误数据 -> 你的意思是字节错误吗?可能设置不同(检查XOn XOff等设置)您可以尝试通过“RealTerm”工具接收数据以检查您是否能够接收正确的数据。
  • 您使用的是哪种适配器? (我在使用适配器时遇到了很多问题)

标签: c# visual-studio windows-7 serial-port windows-xp


【解决方案1】:
private void type(byte[] data)
{
   ...
   serialPort1.DiscardInBuffer();
}

这是一个令人讨厌的错误。您正在丢弃接收缓冲区中不应丢弃的字节。您执行此操作的确切时间是高度不可预测的,这取决于 type() 何时开始运行。它在 UI 线程上运行,时间是非常不可预测的,并且肯定会受到您运行它的操作系统的影响。这在 XP 上完全有效只是运气不好。

使用 DiscardInBuffer() 几乎永远不会正确,只能在调用 SerialPort.Open() 从接收缓冲区中清除旧数据后立即使用它。或者从协议错误中恢复,尝试重新同步发送器和接收器。但是您没有协议,像这样丢弃字节只会产生丢失随机字节集的垃圾数据。产生延迟,您只能从下一次传输中获取用 一些 字节填充的缓冲区。并且崩溃,无论您对损坏的数据做什么都可能会严重崩溃。

必须删除该声明。

【讨论】:

  • 谢谢,我这样做了,现在我没有停止工作错误,但我仍然很慢(但与过去不同)...,我每 9 毫秒发送一次数据,我可以阅读和绘图它在 Windows XP 中正确,但现在我在 Windows 7 中每 25 毫秒获取一次数据!!..有时(比过去少)我的数据跳了
  • 当我在stackoverflow.com/questions/31155232/… 中使用代码时,另一件事我现在得到了相同的结果......我在 Windows 7 中没有最后一个问题
  • 每 9 毫秒更新一次图表是毫无意义的,没有人能真正看到这一点。而且它很昂贵,当 UI 线程跟不上时,你会很容易地烧掉它。收集数据并且在至少 50 毫秒过去之前不要更新图表。并通过丢弃旧数据来保持系列较小。
  • 我的转换器有一个 LED 来显示接收到的数据......奇怪的是它很亮,稍微停止并重新启动后,我在 C# 中收到同样的东西,LED 显示......似乎 C# get 命令 micro 不像 windows XP
  • 我现在不绘图了,我只是每 40 秒读取一次“计数”(图表是注释)并计算时间...波特率为 115200,我读取了 53 个字节
猜你喜欢
  • 2012-07-04
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多