情况是我有霍尼韦尔N5683条形码扫描仪,我无法在霍尼韦尔EZConfig扫描软件之外的任何串行通信程序中发送命令。
请注意扫描仪有许多不同的配置设置可供接受。见这里:https://www.honeywellaidc.com/CatalogDocuments/N56XX-UG%20Rev%20C.pdf
此程序的目标是发送软件触发器,而不必手动按下扫描按钮。到目前为止我编写的程序能够在按下物理按钮时接收数据,但当前无法通过.NET中的SerialPort
类发送数据。请参阅以下代码:
public partial class Form1 : Form
{
private SerialPort serialPort1 = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(scannerDataReceivedHandler);
}
private void Form1_Load(object sender, EventArgs e)
{
// COM Settings
serialPort1.RtsEnable = true;
serialPort1.DtrEnable = false;
if (serialPort1.IsOpen)
serialPort1.Close();
serialPort1.Open();
}
private void scannerDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
Console.WriteLine("Scan Successful");
SerialPort sp = (SerialPort)sender;
Thread.Sleep(50);
int bytes = sp.BytesToRead;
byte[] buffer = new byte[bytes];
sp.Read(buffer, 0, bytes);
string bufferOutput;
bufferOutput = ByteArrayToHexString(buffer);
Console.WriteLine(bufferOutput);
}
private void button1_Click(object sender, EventArgs e)
{
// on
//this is the string I send with the Honeywell Program = "PAPTPR";
string toSend = "50 41 50 54 50 52";
try
{
byte[] data = HexStringToByteArray(toSend);
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
serialPort1.Write(data, 0, data.Length);
}
catch (System.IO.IOException)
{
}
}
// UTILITY
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
}
以下是在霍尼韦尔EZConfig应用程序中发送到Honeywell N5683的成功命令的屏幕截图: