2

我正在尝试通过我正在构建的 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                                    .

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

编码:

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),但我没有从寄存器中得到任何响应。我发送的所有内容似乎都是正确的。有任何想法吗?我错过了什么愚蠢的东西吗?

4

1 回答 1

1

好的,我知道了。我找到了一个旧版本的通信协议,上面写着: - Baud rate: 9600/115200 baud 。所以我将波特率更改为 115200 并且它起作用了。这是愚蠢的。

于 2011-11-19T15:08:43.483 回答