2012-07-18 168 views
1

我正在做1个桌面应用程序。 我想在此添加条形码阅读设施。 在我的应用程序中,所有产品的价格标签都有条形码。 我将使用一些条码扫描仪进行扫描。 但我对所有这些没有ide。 有人可以给任何人apme示例代码或一些参考?用vb.net扫描条形码

+0

你有一个硬件条形码扫描仪还是你不想使用相机和图像处理? – teamalpha5441 2012-07-18 09:55:15

+0

@ TEAM-ALPHA-我将使用硬件设备。客户端稍后会给我们...首先我必须收集代码。我搜索了很多..但不能得到一些结果 – 2012-07-18 09:56:59

+0

设备使用什么接口? PS/2或COM(也许通过USB虚拟COM)? – teamalpha5441 2012-07-18 09:57:41

回答

0

对于COM通过USB您需要的COM端口号(在设备管理器看),让说COM15 使用VB.net使用System.IO.Ports.SerialPort类:

Dim comPort As New SerialPort("COM15") 'New com port' 
Dim terminatingChar As Char = Chr(10) 'Terminate at vbLF (new line)' 

comPort.BaudRate = 9600 '9600 baud speed' 
comPort.Encoding = Encoding.ASCII 'Decode the bytes via ASCII code' 

comPort.Open() 'Open the port' 

Dim myBarcode as String = "" 'Current barcode is empty' 

While True'Read chars until the terminating char appears' 
    Dim tempChar as Char = Convert.ToChar(comPort.ReadChar()) 'Read a char' 
    If tempChar = terminatingChar Then Exit While 'If its the terminating char, exit the loop' 
    myBarcode = myBarcode & tempChar 'If not append it to the barcode string' 
End While 

comPort.Close() '(!) Close the port' 

Console.WriteLine(myBarcode.Trim()) 'Trim it and show it to the user' 
+0

注:它可以得到更复杂这是因为条码扫描器可以配置任意数量的终止字符,在这种情况下,您需要将自己接收的字符串拆分为代码。这也假定条形码扫描器使用默认的COM端口设置9600,8,n,1,这也可以改变。 – 2012-07-18 10:17:29

+0

我编辑我的帖子,以便您可以更改波特率,编码和终止字符(在这种情况下,空格字符),我不知道这是否是有效的vb.net代码,我停止编程vb自3年以来 – teamalpha5441 2012-07-18 10:26:51

+0

你'虽然'循环无效,'ReadChar'返回一个不是Char的整数加终止符可能不止一个字符'CR + LF'很常见 – 2012-07-18 10:43:28

0

大多数可用的条码扫描仪模拟键盘。他们带有配置条形码,让你配置它做不同的事情,比如在代码扫描结束时包含回车符。所以,一旦你扫描了一段代码,它就会出现在屏幕上,就好像它被输入了一样(例如,如果文本框有焦点)。

+0

但为了这一些代码必须需要..有任何代码片段? – 2012-07-18 10:02:13

+0

无需代码。它模仿一个键盘。这意味着,当扫描仪读入一组字符时,它会将这些字符发送给假装成键盘的计算机。试一试,插入条形码扫描仪并打开文本编辑器(例如记事本)。扫描条形码,代码出现在编辑器窗口中。 – 2012-07-18 10:07:25

+0

和我应该为vb.net表单做什么? – 2012-07-18 10:09:32