与.NET的POS串口通信串口、通信、NET、POS

2023-09-06 05:26:47 作者:若愛只如初見

我试图通过从C#应用程序我建立串行端口与收银机进行交流,我必须从制造商提供了一个小工具,它给了我有关设备,如固件版本等一些信息。

I am trying to communicate with a cash register through the serial port from a C# app I am building and I have a small utility provided from the manufacturer which gives me some info about the device like firmware version etc.

在协议规范它指出通信始于ENQASCII控制(0x05h)code在发送端,对此,对方必须回应ACK(0x06h)。然后,它包含的命令等的请求数据包可以发送

In the protocol specification it states that communication begins with the "ENQ" ASCII control (0x05h) code on the sender end, to which the receiver must respond "ACK"(0x06h). Then a request packet can be sent that contains a command etc.

连接设置:

   - 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):

The packet structure is the following (STX = 0x02h , ETX = 0x03h):

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

字段中的数据段之间用/:

Fields in data section are separated with '/':

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

发送/接收规定:

Sender/Receiver states:

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

于是我试着沟通,我什么也没有回来。我下载了一个串行端口嗅​​探器,运行商工具和pressed获取设备版本按钮。我:

So I try to communicate and I get nothing back. I downloaded a serial port sniffer, run the manufacturers utility and pressed the "get device version" button. I got:

#   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                                    .

的**的 * 的**的 * 的**** (ENQ = 05,ACK = 06, STX = 02,ETX = 03,CAN = 18,'/'= 2F)

**********(ENQ = 05, ACK = 06, STX = 02 , ETX = 03, CAN = 18, '/' = 2F)

在code:

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被发送),但我没有得到任何回应,在所有的寄存器。 一切我送似乎是正确的。有任何想法吗?我失去了一些愚蠢的事?

I managed to send the commands from my app and logged the communication (18 18 18 05 is being sent) but I get no response at all from the register. Everything I send seems correct. Any ideas? Am I missing something stupid?

推荐答案

好吧我已经知道了。我找到了通信协议,其中它说的是旧版本: - 波特率:一十一万五千二百分之九千六百波特。所以我改变了波特率为115200和它的工作。这是愚蠢的。

Ok I 've got it. I found an older version of the communication protocol where it says: - Baud rate: 9600/115200 baud . So I changed the baudrate to 115200 and it worked. This is stupid.