2012-07-18 169 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