读取串行端口在 Windows XP 中运行良好,但在 Windows 7 中停止工作且速度缓慢 [英] Read serial port work good in windows XP but stop working and slow in windows 7

查看:65
本文介绍了读取串行端口在 Windows XP 中运行良好,但在 Windows 7 中停止工作且速度缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm talking about C# programing and communicate with serial port and different results in Windows 7 and XP. my code is:

    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 中尝试时,它很慢,得到错误的数据,然后停止工作.

this program work good in windows XP but when I try it in windows 7, It is slow, get wrong data and after a little stop working.

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

I wrote this program again in windows 7 and xp and I tested it with visual studio 2008 and 2012 but I got same results.

推荐答案

private void type(byte[] data)
{
   ...
   serialPort1.DiscardInBuffer();
}

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

That's one heck of a nasty bug. You are throwing away bytes in the receive buffer that should never be discarded. The exact moment in time that you do this is highly unpredictable, it depends on when type() starts running. Which runs on the UI thread, that timing is very unpredictable and is certainly affected by what OS you run this on. That this works at all on XP is just blind luck.

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

Using DiscardInBuffer() is almost never correct, it should only ever be used right after calling SerialPort.Open() to purge old data from the receive buffer. Or to recover from a protocol error, trying to re-synchronize the transmitter and receiver. But you have no protocol, discarding bytes like this just produces garbage data with a random set of bytes missing. Producing delays, you can only get the buffer filled with some bytes from the next transmission. And crashes, whatever you do with the corrupt data is liable to fall over badly.

必须删除该声明.

这篇关于读取串行端口在 Windows XP 中运行良好,但在 Windows 7 中停止工作且速度缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆