PDA

Click to See Complete Forum and Search --> : serialport.ReadExisting


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

petevick
Feb 21st, 2009, 01:41 AM
Take a look at Dick Griers site (http://www.hardandsoftware.com/) - there is a good example of using the serial port

husky_doggy
Feb 23rd, 2009, 06:05 PM
Take a look at Dick Griers site (http://www.hardandsoftware.com/) - there is a good example of using the serial port

i cant find the example in that site..

petevick
Feb 24th, 2009, 12:57 AM
Go to the site, click on 'Software Downloads' and then Visual Studio 2005 Serial Terminal Example (uses the System.IO.Ports namespace). Download here. (Updated July 31, 2006) -- FREE