AdvancedHMI Software
General Category => Support Questions => Topic started by: bachphi on September 08, 2015, 05:27:31 PM
-
In the past, I was using the RTA module from Real Time Automation to read barcode scanning directly into CompactLogix, but with ADHMI, I can now read or type in the barcode from PC, then pass it on to PLC.
First, I set the KeyPreview in the MainForm_Load, then using KeyPress event to catch the content of barcode and check for valid expression using regular expression.
It works fine until I added a FormChange button to go Form2, since many scanners come with the default setting of sending CR key, now after scanning the barcode, it then open Form2. I like to avoid reprogramming the scanner not to send CR key since it is default setting.
Any ideas of avoiding this situation? TIA.
Private Sub MainForm_KeyPress(sender As Object, e As KeyPressEventArgs)
e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper())
Dim pattern As String = "[R]{1}[2]{1}[N]{1}[MPW]{1}[A-Z0-9]{4}" '//R2NP1234
Dim regex As Regex = New Regex(pattern)
If ((e.KeyChar >= ChrW(48) And e.KeyChar <= ChrW(57)) Or (e.KeyChar >= ChrW(65) And e.KeyChar <= ChrW(90))) Then
If ((barcodeSerial.Length = 0) And (e.KeyChar = "R")) Then
barcodeSerial = barcodeSerial + e.KeyChar.ToString()
Label2.Text = barcodeSerial.ToString()
ElseIf ((barcodeSerial.Length > 0) And (barcodeSerial.Length < 8)) Then
barcodeSerial = barcodeSerial + e.KeyChar.ToString()
Label2.Text = barcodeSerial.ToString()
End If
End If
If ((e.KeyChar = ChrW(8)) And (barcodeSerial.Length > 0)) Then '//if mistake, use the backspace key
barcodeSerial = barcodeSerial.Substring(0, barcodeSerial.Length - 1)
Label2.Text = barcodeSerial.ToString()
End If
If (regex.IsMatch(barcodeSerial)) Then
BasicLabel1.Text = barcodeSerial.ToString()
EthernetIPforCLXCom1.Write("FromPC_Barcode", barcodeSerial)
End If
End Sub
-
You can suppress the action of the Enter key like this:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Enter Then
Return True
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
-
Thank You Archie. From what I read, KeyPress event is old school, ProcessCmdKey is better, I found this code , it works for me as well , but I do not understand the (6) and (1) purpose in the code below:
Private scannedBarcode As String = String.Empty
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.A To Keys.Z
Me.scannedBarcode &= keyData.ToString()
Case Keys.NumPad0 To Keys.NumPad9
Me.scannedBarcode &= keyData.ToString()(6)
Case Keys.D0 To Keys.D9
Me.scannedBarcode &= keyData.ToString()(1)
Case Keys.Return, Keys.Enter
Me.ValidateScannedBarcode()
Me.scannedBarcode = String.Empty
Return True
Case Keys.Back AndAlso scannedBarcode.Length > 0
Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
'Case Keys.Escape
' Me.Close()
Case Else
'do nothing?
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function
Sub ValidateScannedBarcode()
Dim pattern As String = "[R]{1}[2]{1}[N]{1}[MPW]{1}[A-Z0-9]{4}" '//R2NP1234
Dim regex As Regex = New Regex(pattern)
If (scannedBarcode.Length = 8) AndAlso (regex.IsMatch(scannedBarcode)) Then
If PLCIsConnected Then
EthernetIPforCLXCom1.Write("FromPC.Barcode", scannedBarcode)
End If
Label1.Text = scannedBarcode.ToString()
End If
End Sub
-
When a key is pressed on the numeric keypad and "keydata" is converted to a string, it will return "numpadX" where X is the digit. So the 6 tells it to use the character in the 6th position of the string.
-
Archie, the code below works when I typed it manually, but when using scanner, it only catch the numbers and lower case letters , but not capital letters. How do I fix this?
Private scannedBarcode As String = String.Empty
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.A To Keys.Z
Me.scannedBarcode &= keyData.ToString()
Case Keys.NumPad0 To Keys.NumPad9
Me.scannedBarcode &= keyData.ToString()(6)
Case Keys.D0 To Keys.D9
Me.scannedBarcode &= keyData.ToString()(1)
Case Keys.Return, Keys.Enter
Me.ValidateScannedBarcode()
Me.scannedBarcode = String.Empty
Return True
Case Keys.Back AndAlso scannedBarcode.Length > 0
Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
'Case Keys.Escape
' Me.Close()
Case Else
'do nothing?
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function
Sub ValidateScannedBarcode()
Dim pattern As String = "[R]{1}[2]{1}[N]{1}[MPW]{1}[A-Z0-9]{4}" '//R2NP1234
Dim regex As Regex = New Regex(pattern)
If (scannedBarcode.Length = 8) AndAlso (regex.IsMatch(scannedBarcode)) Then
If PLCIsConnected Then
EthernetIPforCLXCom1.Write("FromPC.Barcode", scannedBarcode)
End If
Label1.Text = scannedBarcode.ToString()
End If
End Sub
[/quote]
-
Try this:
Private scannedBarcode As String = String.Empty
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Return, Keys.Enter
Me.ValidateScannedBarcode()
Me.scannedBarcode = String.Empty
Return True
Case Keys.Back AndAlso scannedBarcode.Length > 0
Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
Case Else
'do nothing?
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function
Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
If e.KeyChar >= "A" And e.KeyChar <= "z" Then
scannedBarcode &= e.KeyChar
e.Handled = True
End If
End Sub
-
LOL, I had to resort back to the KeyPress event. Your code does work , but it needs to catch the numbers as well.
Thanks, Archie.
Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
If e.KeyChar >= "A" And e.KeyChar <= "z" Then
scannedBarcode &= e.KeyChar
e.Handled = True
End If
If e.KeyChar >= "0" And e.KeyChar <= "9" Then
scannedBarcode &= e.KeyChar
e.Handled = True
End If
End Sub
-
It's very strange, I copy your code to a brand new project v399a, put a label in there to see the scannedbarcode . tested . It worked.
then I added some AAHMI controls, no addtional code. try again and it's stop working...
I put a break point inside sub OnKeyPress and it never jump there.
I removed all AAHMI controls and it worked again.
Added one MessageDisplayByValue. still work.
Added one BasicButton. stop working!
-
Check KeyPreview property on the MainForm and make sure it is True
-
Yup! I added KeyPreview=True in the formload. Thanks again!
-
I found another way to use ProcesscmdKey , it will scan numbers, lowercase as well uppercase.
Private scannedBarcode As String = String.Empty
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Shift + Keys.A To Keys.Shift + Keys.Z
Me.scannedBarcode &= keyData.ToString()(0)
Case Keys.A To Keys.Z
Me.scannedBarcode &= keyData.ToString()
Case Keys.NumPad0 To Keys.NumPad9
Me.scannedBarcode &= keyData.ToString()(6)
Case Keys.D0 To Keys.D9
Me.scannedBarcode &= keyData.ToString()(1)
Case Keys.Return, Keys.Enter
Me.ValidateScannedBarcode()
Me.scannedBarcode = String.Empty
Return True
Case Keys.Back AndAlso scannedBarcode.Length > 0
Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
Case Else
'do nothing?
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function