开发者

How to communicate with scale via rs232

开发者 https://www.devze.com 2023-03-30 10:13 出处:网络
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.

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.

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?

Thanks a lot for any help

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();
    }      
}

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.


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.

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.

EDIT - ADDED SOME POINTS FROM HANS PASSANT HERE THAT IS VITAL - MOST IMPORTANT IS THAT ENQ is ASCII VALUE 5, AND NOT A STRING

port.Write("ENQ");

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;

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.


If you need to send down ENQ, you need to send a binary 5, not a string 5.

If you are writing

port.Write("5");

Then you are sending down a binary 53 ( ASCII Chart) See on the ASCII chart how Dec 5 is the Enquiry and Dec 53 is the char '5'?

Try this:

byte [] Buff = new byte[1];
        Buff[0] = 0x05;
        port.Write(Buff, 0, 1);


The other issue is, do you have the correct cable? A breakout box works great to verify the wiring.

0

精彩评论

暂无评论...
验证码 换一张
取 消