问题是,我可以使用串口软件"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) { }
}
}发布于 2018-09-17 08:13:09
Python barcode scanner serial trigger是我回答类似Python问题的一篇文章。
内容如下所示。
这是因为您将在文档中编写的抽象表达式编码为原始输出数据。
该文档表示数据传输的3个字节。
‘'SYN’和'CR‘是以下十六进制数。
‘'SYN’= \x16
‘'CR’= \x0d或转义序列\r
不‘是一个普通的ASCII字符。
空白和<>{}用于分隔文档中的数据,而不是要发送的数据。
而且,甚至您也需要命令前缀。
还可以使用Write代替@Turbofant编写的WriteLine。
你应该这样写。请试试看。
string command = "\x16M\x0d\x16T\x0d";
port.Write(command);发布于 2018-09-17 08:16:46
我想问题是,您发送了错误的命令字符串。<Syn>、<CR>和<LF>表示特殊的、不可打印的ascii字符同步空闲、载波返回和行提要。您需要在字符串中正确地编码它们。
试着发送:
string command = "\x16t\r\n";
port.Write(command);\x16是<Syn> (因为Syn是ascii字符0x16,或十进制中的22 )。
\r是<CR>
\n是<LN>
使用port.Write而不是port.WriteLine,因为WriteLine会在字符串的末尾自动添加\r\n。
https://stackoverflow.com/questions/52363044
复制相似问题