【问题标题】:to send hex codes to serial port发送十六进制代码到串口
【发布时间】:2018-06-02 18:59:57
【问题描述】:

我正在使用 posiflex 客户显示器,并且正在尝试清除显示器。我浏览了用户手册,找到了使用十六进制代码的 PST 命令模式。我不知道如何将这些十六进制代码传递给串口来清除我的显示。从手册中,我需要发送以下十六进制数字: 14 0E

我尝试了以下代码来发送这些字节,但我不知道如何同时传递两个字节。

SerialPort sp = new SerialPort();

    sp.PortName = "COM6";
    sp.BaudRate = 9600;
    sp.Parity = Parity.None;
    sp.DataBits = 8;
    sp.StopBits = StopBits.One;
    sp.Open();
   byte[] bytestosend = new byte[1] { 0x0E };

    sp.Write(bytestosend, 0, 1);
    sp.Close();
    sp.Dispose();
    sp = null;

当我使用这段代码时,没有执行任何操作(显示不清除)。

【问题讨论】:

  • 也许你也应该发送<14>
  • 始终包含错误和其他信息。这里发生了什么/没有发生什么?
  • @henk,我如何发送 14 和 0E
  • bytestosend = new byte[] { 0x14, 0x0E };Write(bytestosend, 0, 2);
  • 写入后不要不要立即关闭串口。实际发送字节需要时间,串行端口很慢。唯一明智的做法是在程序启动时打开端口,直到退出时才关闭它。如果那是立即的,那么您将不得不延迟到 BytesToWrite 变为 0。

标签: c#


【解决方案1】:

要发送多个字节,只需使用逗号分隔字节。你应该有这样的东西:

sp.PortName = "COM6";
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Open();
byte[] bytestosend = { 0x14, 0x0E };

sp.Write(bytestosend, 0, bytestosend.Length);
sp.Close();
sp.Dispose();
sp = null;

【讨论】:

  • 应该是sp.Write(bytestosend, 0, 2); 或者更好的sp.Write(bytestosend, 0, bytestosend.Length);
  • 感谢@ScottChamberlain - 复制和粘贴太多!
  • 谢谢@belogix。但是我得到了一个连接到这个系统的设备现在无法运行错误
  • 嗯,它现在应该发送两个字节。问题是你确定这就是你所需要的吗?通常您需要带有串行命令的 STX 和 ETX。另外,您报告的“错误”是什么?
  • 我是第一次使用这个杆显示器。我尝试打印一个字符串“欢迎”并且它有效。现在我正在尝试清除文本,它不起作用。
猜你喜欢
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
  • 2015-07-25
  • 2017-10-19
相关资源
最近更新 更多