husky_doggy
Feb 20th, 2009, 03:31 PM
hi,
i have the serial open for reading data from a RFID reader.
because the data comes in Hex ASCII, so I did a code snippet to decode them to characters. below code, I need to click a button in order to decode, but it can only decode once because the text in the textbox wont be HEX anymore after one decode (get error) and I dont want to clear the txtbox after the 1st decode.
2nd is that I cant do it without a button click event, I wanted to do automatically when the data exist. but I can not put anything else in the updateTextBox() function, otherwise i'll get an error...
any ideas how to decode without error?
Serial port code:
public class
Dim updatefromdevice As String
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived
txtReceivedMessage.BeginInvoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
txtReceivedMessage.Text = serialport.ReadExisting & vbCrLf & txtReceivedMessage.Text 'Display received data to textbox
End Sub
decoder code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim hexnum As String
Dim converttohex As String
Dim hextonum As Integer
Dim chara As Char
updatefromdevice = txtReceivedMessage.Text
hexnum = updatefromdevice.Trim()
For i = 0 To Len(hexnum) - 1 Step 2
converttohex = hexnum.Substring(i, 2)
hextonum = Convert.ToInt32(converttohex, 16)
chara = Chr(hextonum)
txtReceivedMessage.Text += chara
Next i
End Sub
i have the serial open for reading data from a RFID reader.
because the data comes in Hex ASCII, so I did a code snippet to decode them to characters. below code, I need to click a button in order to decode, but it can only decode once because the text in the textbox wont be HEX anymore after one decode (get error) and I dont want to clear the txtbox after the 1st decode.
2nd is that I cant do it without a button click event, I wanted to do automatically when the data exist. but I can not put anything else in the updateTextBox() function, otherwise i'll get an error...
any ideas how to decode without error?
Serial port code:
public class
Dim updatefromdevice As String
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived
txtReceivedMessage.BeginInvoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
txtReceivedMessage.Text = serialport.ReadExisting & vbCrLf & txtReceivedMessage.Text 'Display received data to textbox
End Sub
decoder code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim hexnum As String
Dim converttohex As String
Dim hextonum As Integer
Dim chara As Char
updatefromdevice = txtReceivedMessage.Text
hexnum = updatefromdevice.Trim()
For i = 0 To Len(hexnum) - 1 Step 2
converttohex = hexnum.Substring(i, 2)
hextonum = Convert.ToInt32(converttohex, 16)
chara = Chr(hextonum)
txtReceivedMessage.Text += chara
Next i
End Sub