问题
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();
}
}
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.
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.
回答1:
private void type(byte[] data)
{
...
serialPort1.DiscardInBuffer();
}
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.
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.
You must delete that statement.
来源:https://stackoverflow.com/questions/31510232/read-serial-port-work-good-in-windows-xp-but-stop-working-and-slow-in-windows-7