串口通信与POS从.net

本文关键字:net POS 通信 串口 | 更新日期: 2023-09-27 18:15:19

我正试图通过我正在构建的c#应用程序的串行端口与收银机通信,我有一个从制造商提供的小实用程序,它给了我一些关于设备的信息,如固件版本等。

在协议规范中,它声明通信从发送端的"ENQ"ASCII控制(0x05h)代码开始,接收端必须响应"ACK"(0x06h)。然后可以发送包含命令等的请求包。

连接设置:

   - Baud rate:     9600 baud
   - Parity:        none
   - Data:          8
   - Stop:          1
   - Flow control:  none

传输:

Sender  Receiver
Idle    
    Idle
Enquire 
    Acknowledge
Packet  
    Acknowledge 

报文结构如下(STX = 0x02h, ETX = 0x03h):

                  +-----+--------- - - -+-----+
                  | STX |      Data     | ETX |  
                  +-----+--------- - - -+-----+

数据节中的字段用'/'分隔:

+--------++---------+---------+---------+- - - +---------++----------+
  | Header || Field 1 / Field 2 / .... / Field N || Checksum |
  +--------++---------+---------+---------+- - - +---------++-

发送方/接收方州:

      Sender                        Receiver
---------------------------------------------------
       IDLE                        IDLE
    ENQUIRE ---------------------> 
        <--------------------- ACKNOWLEDGE
     PACKET ---------------------> (verify error)
            <--------------------- NOT ACKNOWLEDGE
     PACKET ---------------------> (verify error)
            <--------------------- NOT ACKNOWLEDGE
     PACKET ---------------------> (verify success)
            <--------------------- ACKNOWLEDGE
     .
     .
     (Rest of packet exchange)
    ---------------------------------------------------

所以我试着沟通,但我什么也没得到。我下载了一个串行端口嗅探器,运行制造商的实用程序并按下"获取设备版本"按钮。我:

#   Time            Port        Data                     ASCII
000001  20:03:06.484    COM2    <<  18 18 18 05          ....
000002  20:03:06.500    COM2    >>  06 06                  ..
000003  20:03:06.500    COM2    <<  02 76 2F 36 35 03    .v/65.
000004  20:03:06.515    COM2    >>  02 30 30 2F 30 30 2F 30 32 2F 31 2E 30 31 2F 31 .00/00/02/1.01/1
000005              2E 30 31 2F 20 35 38 2F 35 30 30 30 2F 31 34 30      .01/ 58/5000/140
000006              2F 32 30 2F 31 30 2F 31 30 2F 20 36 2F 20 35 2F      /20/10/10/ 6/ 5/
000007              33 30 2F 37 37 03                                30/77.
000008  20:03:06.515    COM2    <<  06  .
000009  20:03:07.015    COM2    >>  06  .
000010  20:03:07.015    COM2    <<  18 18 18 05 02 61 2F 34 34 03                .....a/44.
000011  20:03:07.015    COM2    >>  06 02 30 30 2F 30 30 2F 30 32 2F 30 37 30 30 33 ..00/00/02/07003
000012              36 34 35 2F 31 2F CD D0 20 2F 44 4C 38 30 4C 49 645/1/ΞΞ  /DL80LI
000013              56 31 52 31 35 20 20 20 20 20 2F 39 34 03   V1R15 /94.
000014  20:03:07.015    COM2    <<  06  .
000015  20:03:07.515    COM2    >>  06 06 02 30 30 2F 30 30 2F 30 32 2F 31 38 31 31 ...00/00/02/1811
000016              31 31   11
000017  20:03:07.515    COM2    <<  18 18 18 05 02 74 2F 36 33 03             .....t/63.
000018  20:03:07.531    COM2    >>  2F 31 39 32 30 30 39 2F 31 31 03      /192009/11.
000019  20:03:07.531    COM2    <<  06                                    .

* * * * * * * * * * (询问= 05,ACK = 06, STX = 02, ETX = 03,可以= 18日'/' = 2 f)

代码:

public partial class Form1 : Form
    {
        // Add this variable 
        string RxString;
    public Form1()
    {
        InitializeComponent();
    }
    private void buttonStart_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = "COM2";
        serialPort1.BaudRate = 9600;
        serialPort1.DataBits = 8;
        serialPort1.Parity = System.IO.Ports.Parity.None;
        serialPort1.StopBits = System.IO.Ports.StopBits.One;
        serialPort1.DtrEnable = true;
        serialPort1.RtsEnable = true;
        try
        {
            serialPort1.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        if (serialPort1.IsOpen)
        {
            buttonStart.Enabled = false;
            buttonStop.Enabled = true;
            textBox1.ReadOnly = false;
        }
    }
    private void buttonStop_Click(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.Close();
            buttonStart.Enabled = true;
            buttonStop.Enabled = false;
            textBox1.ReadOnly = true;
        }
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort1.IsOpen) serialPort1.Close();
    }
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        // If the port is closed, don't try to send a character.
        if (!serialPort1.IsOpen) return;
        // If the port is Open, declare a char[] array with one element.
        char[] buff = new char[1];
        // Load element 0 with the key character.
        buff[0] = e.KeyChar;
        // Send the one character buffer.
        serialPort1.Write(buff, 0, 1);
        // Set the KeyPress event as handled so the character won't
        // display locally. If you want it to display, omit the next line.
        e.Handled = true;
    }
    private void DisplayText(object sender, EventArgs e)
    {
        textBox1.AppendText(RxString);
    }
    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        RxString = serialPort1.ReadExisting();
        this.Invoke(new EventHandler(DisplayText));
    }
    private void buttonENQ_Click(object sender, EventArgs e)
    {
        // If the port is closed, don't try to send a character.
        if (!serialPort1.IsOpen) return;
        byte[] Buff = new byte[4];
        Buff[0] = 0x18;
        Buff[1] = 0x18;
        Buff[2] = 0x18;
        Buff[3] = 0x05;
        serialPort1.Write(Buff, 0, 4);
    }
}

我设法从我的应用程序发送命令并记录通信(18 18 18 05正在发送),但我从寄存器中根本没有得到响应。我发送的一切似乎都是正确的。什么好主意吗?我错过了什么愚蠢的东西吗?

串口通信与POS从.net

好的,我知道了。我发现了一个旧版本的通信协议,上面写着:-波特率:9600/115200波特。所以我把波特率改为115200,它工作了。