如何通过 rs232 与秤通信 [英] How to communicate with scale via rs232

查看:104
本文介绍了如何通过 rs232 与秤通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .net 中的 SerialPort 类使我的 C# 应用程序通过 rs232 与数字秤进行通信.很遗憾,我无法从体重秤中获得任何反馈.

I am trying to make my C# application communicate with digital scale via rs232 by using SerialPort class in .net. Unfortunatelly I can't get any feedback from the scale.

这是一个 TSCALE TSQSP 量表,显然它使用的是 CAS 通信协议.根据我对 CAS 协议的理解,我需要发送ENQ"等待ACK"然后发送DC1"来获取信息,但是在发送ENQ"之后我没有得到任何回复.这是我的代码.有没有人有使用相同通信协议进行扩展的经验?

It's a TSCALE TSQSP scale and aparently it's using CAS communication protocol. From my understanding in CAS protocol i need to send "ENQ" wait for "ACK" and then send "DC1" to get information, but after sending "ENQ" I am not getting any reply. Here is my code. Does anybody has experience with scale with same communication protocol?

非常感谢您的帮助

public partial class Form1 : Form
{
    private SerialPort port = new SerialPort("COM6", 4800, Parity.None, 8, StopBits.One);

    public Form1()
    {
        InitializeComponent();

        port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        port.Open();            
    }

    private void button1_Click(object sender, EventArgs e)
    {            
        port.Write("ENQ");

    }

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        this.textBox1.Text = port.ReadExisting();
    }      
}

<小时>

感谢大家的帮助,所有的答案都很有帮助,我终于能够让它工作了.首先我发现我没有正确设置秤,而且你是对的,我应该发送 05H.现在一切正常,谢谢.


Thanks everybody for the help, all answers were helpful, I was finally able to make it work. First i found out i didnt have the scale set up properly and also you are right that i am supposed to send down 05H. Everything works now, thx.

推荐答案

我已经编程了很多权重,通常如果你甚至没有在 ENQ 上得到响应,那是因为你没有正确设置 SerialPort.100% 确保正确设置波特率、数据位、停止位和奇偶校验.

I have programmed a lot of weights, and usually if you do not even get a response on the ENQ it's because you have set up the SerialPort uncorrectly. Be 100% sure that the Baud rate, data bits, stop bits and parity is set correctly.

我要做的是下载 LookRS232.它是一个通过 COM 端口进行通信的程序.安装好后,用我们的设置打开COM口,然后直接写ENQ到COM口.测试大量设置以使其首先在此处工作更容易.然后,当你让它工作时,在你的程序中使用完全相同的设置.

What I would do is to download LookRS232. Its a program to communicate over the COM port. After it's installed, open the COM port with our settings, and then write ENQ directly to the COM port. It's easier to test out a lot of settings to get it to work here first. Then, when you get it to work, use the exact same settings in you program.

编辑 - 在这里添加了 HANS PASSANT 的一些重要观点 - 最重要的是 ENQ 是 ASCII 值 5,而不是字符串

port.Write("ENQ");

ENQ、ACK 和 DC1 是控制代码,而不是字符串.使用 ASCII 表来了解代码.修复:

ENQ, ACK and DC1 are control codes, not strings. Use an ASCII table to know the code. Fix:

port.Write(5);

您还应该打开硬件握手信号,许多设备使用它来确保机器已通电并准备好接收数据.所以他们知道他们收到的是真实数据而不是噪音.修复:

You should also turn on the hardware handshake signals, many devices use it to ensure that the machine is powered up and ready to receive data. So they know whatever they receive is real data and not noise. Fix:

port.Open();            
port.DtrEnable = true;
port.RtsEnable = true;

如果您仍然遇到问题,测试与其他程序(如 HyperTerminal 或 Putty)的连接始终是一个好主意.使用键盘上的 Ctrl 键生成控制代码.ENQ 是 Ctrl+E,DC1 是 Ctrl+Q.

Testing the connection with another program like HyperTerminal or Putty is always a good idea if you still have trouble. Generate the control codes with the Ctrl key on your keyboard. ENQ is Ctrl+E, DC1 is Ctrl+Q.

这篇关于如何通过 rs232 与秤通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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