问题是,我可以使用串口软件“Hercules”发送命令<SYN>T<CR><LF>
触发扫描仪,在数据表中据说使用命令[SYN]T[CR]
来触发扫描仪,但是我不能使用我的串口通信来触发它(两个命令)。我手动使用扫描仪时输入但无法触发...有什么问题? (端口是虚拟的)
private static SerialPort port;
private static bool _continue = false;
public static void Main(string[] args)
{
port = new SerialPort();
port.PortName = "COM8";
port.BaudRate = 115200;
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Handshake = Handshake.None;
port.RtsEnable = true;
port.DtrEnable = true;
port.ReadTimeout = 500;
port.WriteTimeout = 500;
port.Open();
_continue = true;
Thread thr = new Thread(SerialPortProgram);
thr.Start();
}
private static void SerialPortProgram()
{
Console.WriteLine("Writing to port: <SYN>T<CR><LF>");
string command = "<SYN>T<CR><LF>";
port.WriteLine(command);
while (_continue)
{
try
{
string input = port.ReadLine();
Console.WriteLine("Input is - " + input);
}
catch (TimeoutException) { }
}
}
Python barcode scanner serial trigger是一篇文章,我回答了类似的Python问题。
内容如下所示。
发生这种情况是因为您将文档中编写的抽象表达式编码为原始输出数据。
该文件代表3个字节的数据传输。
'SYN'和'CR'是以下十六进制数字。
'SYN'= \ξ16
'CR'= \ x0d或转义序列\ r \ n
'T'是普通的ASCII字符。
空格和<> [] {}用于分隔文档中的数据,而不是要发送的数据。
而且,即使你需要命令前缀它。
也可以使用@Turbofant编写的Write
而不是WriteLine
。
你应该写这样的。请试一试。
string command = "\x16M\x0d\x16T\x0d";
port.Write(command);
我想问题是,你发送错误的命令字符串。 <Syn>
,<CR>
和<LF
>代表特殊的,不可打印的ascii字符同步空闲,回车和换行。您需要在字符串中正确编码它们
尝试发送:
string command = "\x16t\r\n";
port.Write(command);
\x16
is <Syn>
(因为Syn是ascii字符0x16,或十进制22)
\r
是<CR>
\n
是<LN>
并使用port.Write
而不是port.WriteLine
,因为WriteLine会自动在字符串的末尾添加\r\n
。