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