com 端口无限期响应

发布于 2024-11-08 07:46:51 字数 1987 浏览 0 评论 0原文

我使用 com 端口 遇到问题时,向 com 端口发送请求并等待答复。答案是无限的......

static void Main(string[] args)
        {
             byte[] cahs;
             byte[] cash_with_bbc;
            SerialPort mySerialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
            mySerialPort.Open();
            cahs = new byte[] { 0x04, 0x30, 0x02, 0x44, 0x03 };
            cash_with_bbc = new byte[] { 0x04, 0x30, 0x02, 0x44, 0x03, GetBCC(cahs) };
            mySerialPort.Write(cash_with_bbc, 0, cash_with_bbc.Length);

            Thread.Sleep(50);

            Console.WriteLine(mySerialPort.ReadLine());

            while (mySerialPort.BytesToRead > 0)
            {
                Console.WriteLine(mySerialPort.ReadLine());
            }

            mySerialPort.Write(new byte[] {0x06}, 0, 1);

            mySerialPort.Close();
        }
        private static byte GetBCC(byte[] inputStream)
        {
            byte bcc = 0;

            if (inputStream != null && inputStream.Length > 0)
            {
                // Exclude SOH during BCC calculation
                for (int i = 0; i < inputStream.Length; i++)
                {
                    bcc ^= inputStream[i];
                }
            }

            return bcc;
        }

文档

自动提款机由 来自上层设备(主机)的命令 并发送相应的响应。什么时候 自动提款机收到命令后, 响应应在之前发送 收到下一个命令。如果有一个命令 在处理期间发送 响应,自动提款机不会 做出反应并响应命令 全部。自动提款机也不给 命令之前的任何响应 到达。

当一条消息(命令或响应) 已发送,回复已发送至 表明该消息是否已被 已成功接收。

ACK (0x06):表示该消息 已被接受。 NAK(0x15):至 表明该消息已被 被拒绝并且该消息应该 被反感。

重新发送一条消息将 最多尝试 3 次,以防万一 试验失败,该消息将 取消并采用新的传输模式 做好准备。除 ACK 之外的所有文本 将被视为 NAK。 (例外情况。EOT (0x04) 是 上层新发送的字符集 级别并被认定为EOT 这使得能够为新的做好准备 通讯传输方式。)

每条消息都有块检查 字符(BCC),显示是否 消息正常或异常。 因此,在正确的 BCC 情况下, 消息被称为正常状态 (发送 ACK)。否则,发送 NAK 并注意消息失败 传播。 EOT的字符集 用于头部和尾部 信息。如果它不在 BCC 上 检查一下,所有的传输顺序都是 被忽略,新的通信模式是 设置。

I work with com port
faced with a problem, send a request to the com port and waiting for an answer. the answer is infinite ...

static void Main(string[] args)
        {
             byte[] cahs;
             byte[] cash_with_bbc;
            SerialPort mySerialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
            mySerialPort.Open();
            cahs = new byte[] { 0x04, 0x30, 0x02, 0x44, 0x03 };
            cash_with_bbc = new byte[] { 0x04, 0x30, 0x02, 0x44, 0x03, GetBCC(cahs) };
            mySerialPort.Write(cash_with_bbc, 0, cash_with_bbc.Length);

            Thread.Sleep(50);

            Console.WriteLine(mySerialPort.ReadLine());

            while (mySerialPort.BytesToRead > 0)
            {
                Console.WriteLine(mySerialPort.ReadLine());
            }

            mySerialPort.Write(new byte[] {0x06}, 0, 1);

            mySerialPort.Close();
        }
        private static byte GetBCC(byte[] inputStream)
        {
            byte bcc = 0;

            if (inputStream != null && inputStream.Length > 0)
            {
                // Exclude SOH during BCC calculation
                for (int i = 0; i < inputStream.Length; i++)
                {
                    bcc ^= inputStream[i];
                }
            }

            return bcc;
        }

documentation

Cash dispenser is operated by the
command from upper level device (host)
and sends the response for that. When
cash dispenser receives a command, the
response should be sent before the
next command is received. If a command
sends during the processing the
response, cash dispenser would not
react and respond to the command at
all. Also cash dispenser doesn’t give
any response before a command is
arrived.

When a message (command or response)
has been sent, a response is sent to
indicate whether the message has been
successfully received.

ACK (0x06): to indicate that message
has been accepted. NAK (0x15): to
indicate that the message has been
rejected and that the message should
be resent.

The re-sending of one message will be
tried up to 3 times and, in case all
of the trials fail, the message will
be canceled and new transmission mode
be ready. All the texts except ACK
would be considered as NAK.
(Exceptionally. EOT (0x04) is the
newly sent character set from upper
level and it is recognized as EOT
which enables to be ready for new
communication transferring mode.)

Every message has Block Check
Character (BCC), which shows whether
the message is normal or abnormal.
Therefore, in case of right BCC, the
message is known as normal state
(Sending ACK). Otherwise, NAK is sent
and notice the failure of message
transmission. The character set of EOT
is used in the head and the end of the
message. If it is not located on BCC
Check, all the transmission order is
ignored and new communication mode is
set up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云雾 2024-11-15 07:46:51

设备可能会使用与 Serial.RaedLine() 期望的 NewLine 不匹配的 NewLine 进行响应。来自 文档 默认为 LF,尝试使用 CR 甚至 CRLF

mySerialPort.NewLine = "\r";

The device might respond with a NewLine that does not match the one that expect Serial.RaedLine(). From documentation default is LF, try with CR or even CRLF:

mySerialPort.NewLine = "\r";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文