Send command to weigh indicator.

huangapple go评论77阅读模式
英文:

Send command to weigh indicator

问题

I am currently attempting to establish an interface with an LP7510 Weighing Indicator using the RS232 to USB protocol, aiming to retrieve the weight information displayed on the device. According to the user guide, the serial interface of the indicator is capable of receiving "simple ASCII commands" (Page 9). The designated command words and their corresponding functionalities are as follows:

"P" - PRINT: Initiates the printing of the weight.
"R" - REPLY: Requests a reply containing the weight value.

Since this is my first experience with serial port communication, it is plausible that the issue lies within my code implementation. Provided below is an initial code snippet that I have employed for this purpose:

private SerialPort serialPort;

private void BtnOpenConnection_Click(object sender, RoutedEventArgs e)
{
    serialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
    serialPort.DataReceived += SerialPort_DataReceived;
    serialPort.Open();
}

private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string data = serialPort.ReadLine().ToString();

    textBox1.Text = data;
}

private void BtnSend_Click(object sender, RoutedEventArgs e)
{
    serialPort.Write("R");
}

During the initial testing phase, I encountered a situation where no data was received despite successfully establishing a connection. This prompted me to investigate the cable itself. In an attempt to address the issue, I swapped the RX and TX lines on the printed circuit board of the scale indicator, suspecting that the lines might have been incorrectly positioned. However, this modification did not resolve the problem, and the issue persisted.

To further assess the cable's functionality, I conducted a loop test and performed communication tests using TeraTerm. In this scenario, I was able to transmit commands and receive responses successfully. I still would not rule out the Cable however I am now at some loss.

Another possible fix maybe due to the way I can currently call serialPort.Write("R"). Would I need to first convert this to a byte?

byte asciiCommand = (byte)'R';
serialPort.Write(new byte[] { asciiCommand }, 0, 1);

Any help or suggestions would be greatly appreciated!

英文:

I am currently attempting to establish an interface with an LP7510 Weighing Indicator using the RS232 to USB protocol, aiming to retrieve the weight information displayed on the device. According to the user guide, the serial interface of the indicator is capable of receiving "simple ASCII commands" (Page 9). The designated command words and their corresponding functionalities are as follows:

"P" - PRINT: Initiates the printing of the weight.
"R" - REPLY: Requests a reply containing the weight value.

Since this is my first experience with serial port communication, it is plausible that the issue lies within my code implementation. Provided below is an initial code snippet that I have employed for this purpose:

private SerialPort serialPort;

private void BtnOpenConnection_Click(object sender, RoutedEventArgs e)
{
    serialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
    serialPort.DataReceived += SerialPort_DataReceived;
    serialPort.Open();
}

private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string data = serialPort.ReadLine().ToString();

    textBox1.Text = data;
}

private void BtnSend_Click(object sender, RoutedEventArgs e)
{
    serialPort.Write("R");
}

During the initial testing phase, I encountered a situation where no data was received despite successfully establishing a connection. This prompted me to investigate the cable itself. In an attempt to address the issue, I swapped the RX and TX lines on the printed circuit board of the scale indicator, suspecting that the lines might have been incorrectly positioned. However, this modification did not resolve the problem, and the issue persisted.

To further assess the cable's functionality, I conducted a loop test and performed communication tests using TeraTerm. In this scenario, I was able to transmit commands and receive responses successfully. I still would not rule out the Cable however I am now at some loss.

Another possible fix maybe due to the way I can currently calling serialPort.Write("R"). Would I need to first convert this to a byte?

byte asciiCommand = (byte)'R';
serialPort.Write(new byte[] { asciiCommand }, 0, 1);

Any help or suggestions would be greatly appreciated!

答案1

得分: 1

以下是要翻译的内容:

你使用 TeraTerm 的第一步是最好的。现在你确信设备、电缆、转换器等都运行正常。你的应用程序是唯一需要维护的关键点。

因此,请在 TeraTerm 中注意当回复返回时:是在 R 之后立即回复,还是需要按 Enter 键?你需要编写相应端口的符号。这应该有所帮助。否则,请提供反馈意见。

在 TeraTerm 中还要注意回复的内容。多次重复 R 命令并确定回复中是否存在 "new line"。根据情况,你需要使用 "serialPort.ReadLine()" 或 "serialPort.ReadExisting()",因为 ReadLine() 等待 "new line" 符号,只有在它后面才会返回。

设备的手册说明 CR/LF 会结束回复,所以 "serialPort.ReadLine()" 应该可以工作。

英文:

Your first step with TeraTerm is the best one. Now you are sure the device, the cable, the convertor etc. works perfectly. Your app is the only point to maintain.

So, please note in TeraTerm when the reply comes back: immediately after R or you need to press Enter? You need to write the port respective symbols. It should help. Otherwise provide your feedback.

In TeraTerm also take a note at the reply. Repeat R command several times and determine if 'new line' presents in the reply. Depending on it you need to use 'serialPort.ReadLine()' or 'serialPort.ReadExisting()' because ReadLine() waits the 'new line' symbol and comes back after it only.

Device's manual tells what CR/LF ends the reply, so 'serialPort.ReadLine()' should work.

huangapple
  • 本文由 发表于 2023年7月4日 23:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定