[英]C# winform parsing serial port data
I am a newb armed with copy paste trying to get data from an old Avery-Weightronix 7820 weigh scale.我是一个拥有复制粘贴的新手,试图从旧的 Avery-Weightronix 7820 称重秤中获取数据。 I have it hooked up via an RJ-232 cable and have managed to get responses from the scale.
我通过 RJ-232 电缆将它连接起来,并设法从秤上获得响应。 I followed this tutorial https://www.studentcompanion.co.za/creating-a-serial-port-interface-with-c/ to get up and running.
我按照本教程https://www.studentcompanion.co.za/creating-a-serial-port-interface-with-c/启动并运行。
Once I got it working I wanted to simplify it and essentially use my computer as a remote display.一旦我开始使用它,我想简化它并基本上将我的计算机用作远程显示器。 I hard coded the serial connection parameters and set up the code to send a request for the weight every second so I could display it in a winform.
我对串行连接参数进行了硬编码,并将代码设置为每秒发送一个重量请求,以便我可以在 winform 中显示它。
Later I plan to extend it so I can get the total weight for multiple items.后来我打算扩展它,这样我就可以得到多个项目的总重量。 I plan to weigh an item, copy the weight to a running total, put a new item on the scale and repeat.
我计划称一个物品的重量,将重量复制到运行总数中,将一个新物品放在秤上并重复。
I am stuck at the moment because I am having trouble consistently parsing the data returned from the scale and because the loop locks up the UI.我现在被卡住了,因为我在解析从秤返回的数据时遇到了问题,并且因为循环锁定了 UI。
When I send W <CR>
which is the request for the current weight, an example response is:当我发送 W
<CR>
这是对当前重量的请求时,示例响应是:
<LF>
0001.10lb <CR>
<LF>
00 <CR><ETX>
<LF>
0001.10lb <CR>
<LF>
00 <CR><ETX>
Where <ETX>
= End of text character (Ø3 hex), <LF>
= Line feed character (ØA hex), <CR>
= Carriage return character (ØD hex).其中
<ETX>
= 文本结束字符(Ø3 十六进制), <LF>
= <LF>
(ØA 十六进制), <CR>
= 回车符(ØD 十六进制)。
The response from the scale is fixed length but when I go into debug mode the response is not received during the first couple of cycles through the code.来自秤的响应是固定长度的,但是当我进入调试模式时,在代码的前几个周期中没有收到响应。 Then it arrives later.
然后它稍后到达。 This is ok when I am outputting it to a rich text field but when I try to pull out a substring I get an error if there is no data.
当我将它输出到富文本字段时这是可以的,但是当我尝试提取子字符串时,如果没有数据,我会收到错误消息。
Also, the UI locks and the only thing I can do it stop the execution.此外,用户界面锁定,我唯一能做的就是停止执行。 From reading around it seems I should implement threading but I am unsure how to reorganize my code to do this.
从阅读来看,我似乎应该实现线程,但我不确定如何重新组织我的代码来做到这一点。
I would appreciate any pointers or advice on how to solve these issues.我将不胜感激有关如何解决这些问题的任何指示或建议。
This is the code I have:这是我的代码:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
namespace ScaleView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//updatePorts(); //Call this function everytime the page load
//to update port names
CheckForIllegalCrossThreadCalls = false;
}
private SerialPort ComPort = new SerialPort(); //Initialise ComPort Variable as SerialPort
private void connect()
{
bool error = false;
ComPort.PortName = "COM3";
ComPort.BaudRate = int.Parse("9600"); //convert Text to Integer
ComPort.Parity = (Parity)Enum.Parse(typeof(Parity), "Even"); //convert Text to Parity
ComPort.DataBits = int.Parse("7"); //convert Text to Integer
ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "1"); //convert Text to stop bits
try //always try to use this try and catch method to open your port.
//if there is an error your program will not display a message instead of freezing.
{
//Open Port
ComPort.Open();
ComPort.DataReceived += SerialPortDataReceived; //Check for received data. When there is data in the receive buffer,
//it will raise this event, we need to subscribe to it to know when there is data
//MessageBox.Show(this, "Connected", MessageBoxButtons.OK);
}
catch (UnauthorizedAccessException) { error = true; }
catch (System.IO.IOException) { error = true; }
catch (ArgumentException) { error = true; }
if (error) MessageBox.Show(this, "Could not open the COM port. Most likely it is already in use, has been removed, or is unavailable.", "COM Port unavailable", MessageBoxButtons.OK, MessageBoxIcon.Stop);
//if the port is open, Change the Connect button to disconnect, enable the send button.
//and disable the groupBox to prevent changing configuration of an open port.
if (ComPort.IsOpen)
{
btnConnect.Text = "Disconnect";
}
}
// Call this function to close the port.
private void disconnect()
{
ComPort.Close();
btnConnect.Text = "Connect";
}
//whenever the connect button is clicked, it will check if the port is already open, call the disconnect function.
// if the port is closed, call the connect function.
private void btnConnect_Click_1(object sender, EventArgs e)
{
if (ComPort.IsOpen)
{
disconnect();
}
else
{
connect();
rtxtDataArea.AppendText("Connected\n");
sendData();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//Clear the screen
rtxtDataArea.Clear();
}
// Function to send data to the serial port
private void sendData()
{
bool error = false;
while(ComPort.IsOpen) //if text mode is selected, send data as tex
{
try
{
// Convert string of hex digits (in this case representing W<CR>) to a byte array
string hextext = "57 0D";
byte[] data = HexStringToByteArray(hextext);
// Send the binary data out the port
ComPort.Write(data, 0, data.Length);
System.Threading.Thread.Sleep(3000);
rtxtDataArea.ForeColor = Color.Blue; //write Hex data in Blue
string response = ComPort.ReadLine();
int charfrom = 1;
int charto = 9;
//string weight = response.Substring(charfrom, charto - charfrom);
rtxtDataArea.AppendText(response + "TEST\n");
}
catch (FormatException) { error = true; }
// Inform the user if the hex string was not properly formatted
catch (ArgumentException) { error = true; }
if (error) MessageBox.Show(this, "Not properly formatted hex string: \n" + "example: E1 FF 1B", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
//Convert a string of hex digits (example: E1 FF 1B) to a byte array.
//The string containing the hex digits (with or without spaces)
//Returns an array of bytes. </returns>
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
private void btnSend_Click(object sender, EventArgs e)
{
sendData();
}
//This event will be raised when the form is closing.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (ComPort.IsOpen) ComPort.Close(); //close the port if open when exiting the application.
}
//Data recived from the serial port is coming from another thread context than the UI thread.
//Instead of reading the content directly in the SerialPortDataReceived, we need to use a delegate.
delegate void SetTextCallback(string text);
private void SetText(string text)
{
//invokeRequired required compares the thread ID of the calling thread to the thread of the creating thread.
// if these threads are different, it returns true
if (this.rtxtDataArea.InvokeRequired)
{
rtxtDataArea.ForeColor = Color.Green; //write text data in Green colour
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.rtxtDataArea.AppendText(text);
}
}
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
var serialPort = (SerialPort)sender;
var data = serialPort.ReadExisting();
SetText(data);
}
private void rtxtDataArea_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove try-catch
blocks, and MessageBox.Show(...)
in both the connect()
and sendData()
methods.在
connect()
和sendData()
方法中删除try-catch
块和MessageBox.Show(...)
。 The try-catch
in the new Thread(() => {
block will catch any exceptions and send them to the UI thread. new Thread(() => {
块中的try-catch
将捕获任何异常并将它们发送到 UI 线程。
Delete the line CheckForIllegalCrossThreadCalls = false;
删除行
CheckForIllegalCrossThreadCalls = false;
because it bypasses the built-in protection.因为它绕过了内置保护。 The
serialPort.ReadLine();
serialPort.ReadLine();
method might be better than serialPort.ReadExisting();
方法可能比
serialPort.ReadExisting();
as ReadLine()
will wait for a full line of data.因为
ReadLine()
将等待一整行数据。
This should give you an idea of how the code might look:这应该让您了解代码的外观:
Thread dataThread = null;
private void btnConnect_Click_1(object sender, EventArgs e) {
if (ComPort.IsOpen) {
disconnect();
}
else {
btnConnect.Enabled = false; // user has to wait for connection to succeed or fail. Prevents clicking the button twice.
Form form = this;
dataThread = new Thread(() => {
try {
connect();
// if you get here then connect succeeded
form.BeginInvoke((Action) delegate {
btnConnect.Enabled = true; // now becomes the Disconnect button
rtxtDataArea.AppendText("Connected\n");
});
// start sending and receiving data on the thread
sendData();
} catch (Exception ex) {
form.BeginInvoke((Action) delegate {
btnConnect.Enabled = true; // remains as the Connect button, user can try again
MessageBox.Show(form, ex.GetType().Name + ": " + ex.Message, "COM Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
});
dataThread.IsBackground = true;
dataThread.Start();
}
}
delegate void SetTextCallback(string text);
private SetTextCallback callSetText = new SetTextCallback(SetText); // don't create new delegates each time
private void SetText(string text) {
if (this.rtxtDataArea.InvokeRequired) {
rtxtDataArea.ForeColor = Color.Green; //write text data in Green colour
this.BeginInvoke(d, new object[] { text }); // use BeginInvoke instead
}
else {
this.rtxtDataArea.AppendText(text);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.