【发布时间】: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