【发布时间】:2016-12-03 10:06:34
【问题描述】:
因此,我尝试将条形码扫描仪用作“串行”设备,而不是键盘模拟器,但它没有创建 com 端口。我已经扫描了将其设置为串行设备的手册中的设置代码,这似乎正确配置了扫描仪(它停止将扫描的代码发送到文本框\文本编辑器)但是因为没有 COM 端口,我不能当我扫描条形码时捕获数据......
Windows 在第一次插入时安装了驱动程序,没有提供磁盘\驱动程序...想知道是否有其他人遇到过同样的问题.....
这是我的代码....
class Program
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("com1", 9600, Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
new Program();
}
private Program()
{
string[] ports = System.IO.Ports.SerialPort.GetPortNames();
Console.WriteLine("Incoming Data:");
// Attach a method to be called when there
// is data waiting in the port's buffer
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);
// Begin communications
port.Open();
// Enter an application loop to keep this thread alive
Application.Run();
}
private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine(port.ReadExisting());
}
}
当我尝试打开端口时,我收到错误消息.....'端口 'com1' 不存在'.....
当我创建一个虚拟端口(使用 3rd 方应用程序)时,代码运行但我仍然没有从扫描仪获取数据....
【问题讨论】:
-
您必须编写一段代码才能连接到 COM 端口,如果不通过指定端口、波特率、数据位和其他一些参数明确连接到它,扫描仪将无法为您工作。它不同于在键盘模式下捕获数据。
-
@mauro21pl 是的,这样做了,添加了我的代码,我收到错误消息.....'端口'com1'不存在'.....当我尝试打开港口。也许我做错了什么......
-
那将是“COM1”,而不是“com1”?不记得它是否像 DOS 时代那样区分大小写......只需调试
ports中的值。 -
@Mikko Viitala 同样的错误...'端口'COM1'不存在。'在'port.Open();'线…。扫描仪已插入 USB,我原以为它会创建一个 COMn 端口,并且会在设备管理器中看到......但它不是......
-
一旦您将扫描仪插入USB端口,即插即用,它应该安装并初始化扫描仪,并创建一个您应该能够在设备管理器中看到的COM端口。如果您没有看到它,请尝试将设备插入另一个 USB 端口,这可能会解决您的问题。一旦可以在设备管理器中访问扫描仪,它就是实际应用程序的起点
标签: c# serial-port barcode-scanner