-
MSComm1 Help
I'm trying to read in strings of hex values, 12 bytes in total (ex. 01 09 0C 96 38 F0 07 00 00 00 00 5C). I want to be able to take these strings, ignore the first three bytes (they're always the same), and compare them so that they can be used in IF statements (ie IF "hex string" comes in, send out value). The deal is that I have very little Visual Basic experience. If someone could give me a few pointers it would be greatly appreciated.
-
Re: MSComm1 Help
This is off the top of my head but basically use mid to grab the data you want
Then use select case to compare it to known values and respond accordingly.
Code:
dim myInput as string
myInput=trim(mid(myInput,10,len(myInput))) 'should recreate the string starting at 4th byte
select case myInput
case "96 38 F0 07 00 00 00 00 5C"
'output serial command here
case "96 37 FA 27 00 00 00 00 FF"
'output serial command here
case else
end select
-
Re: MSComm1 Help
I'm not sure I really understand the code. As I said I have very little experience with VB.
Here is the code that I have so far. All it does is takes in the data and outputs it to a text box. How could I insert that code in there to display just the 9 bytes in the text box?
Code:
Private Sub cmdRun_Click()
Do
dummy = DoEvents()
Loop Until MSComm1.InBufferCount >= 12
Value = MSComm1.Input
Text1.Text = Value
Timer1.Enabled = True
End Sub
Private Sub Option1_Click()
On Error GoTo Error:
MSComm1.PortOpen = False
Exit Sub
Error:
MsgBox "Cannot Perform Function"
End Sub
Private Sub Option2_Click()
MSComm1.PortOpen = True
End Sub
Private Sub Timer1_Timer()
cmdRun_Click
End Sub
-
Re: MSComm1 Help
try this...
Code:
text1.text=trim(mid(Value,10,len(Value)))
-
Re: MSComm1 Help
I have tried to insert that code, but nothing is appearing in the text box. If i remove that and just print Value I get values such as " –8ð" (there's usually more black bars or boxes in place of the space). Another question, the hex values coming in get changed into those ascii character when displayed in the text box. Is there any way to prevent this from happening so that we can just get the hex?
Code:
Private Sub cmdRun_Click()
Do
dummy = DoEvents()
Loop Until MSComm1.InBufferCount >= 12
Value = MSComm1.Input
Text1.Text = Trim(Mid(Value, 10, Len(Value)))
Select Case Value
Case "96 38 F0 07 00 00 00 00 5C"
'output serial command here
Text2.Text = 5
Case "07 39 F0 07 00 00 00 00 CC"
'output serial command here
Case Else
End Select
Timer1.Enabled = True
End Sub
-
Re: MSComm1 Help
Installation Question?????
-
Re: MSComm1 Help
-
Re: MSComm1 Help
You are posting in the Application Deployment Forum and there are no deployment questions.
-
Re: MSComm1 Help
Where would be the best place to post?
-
Re: MSComm1 Help
It depend on your question. If you are asking about VB6 then the Visual Basic Classic forum if VB.Net then the Visual Basic .NET forum... About Application Deployment... Then this forum
-
Re: MSComm1 Help
Moved to Classic VB forum
-
Re: MSComm1 Help
As I mentioned in the other post, you need to use Byte Arrays to get your data from the RS232 port correctly.
-
Re: MSComm1 Help
Hello.
By the way did you check if you're getting this string on other software? Sometimes, because of bad configuration, you won't be able to get anything, even your program is perfect.
Best regards
-
Re: MSComm1 Help
We've used programs called "HHD Serial Monitor" and "Advanced Serial Port Terminal" to read the information sent back and forth between computer and our RFID reader. Every time there is a valid read of a transponder we get back a values such as 01 09 0C 07 39 F0 07 00 00 00 00 CC. We have it set up for 9600.8.n.1 which is what the reader operates at.
-
Re: MSComm1 Help
Thanks for the help. We now have our problem solved!!!!
-
Re: MSComm1 Help
What you're getting back is the values (in hex) 01 09 0C 96 38 F0 07 00 00 00 00 5C. What you want to display are the characters "0", "1", " ", "0", "9", etc. What you're seeing is "☺ ♀ û 8 ≡ • \" (plus a few non-printable characters - the 09 and the 00 values, in this case).
The input isn't "01 09 0c ...", it's ""☺<a tab here>♀ ...", so your case statement will never match anything.
If you want to display the input, either get the input as an array of byte, or input it as a string (you'll need the Asc() of each character instead of the value of each element of the byte array) and convert each value into a 2-byte string representing its hex value. There are examples on the site, but basically you use the value of each part of the character ((value And 240) \ 16) and (value And 15) to select the nth element of a 16 element string array, ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F").
5C, for instance, is 92. (92 And 240) \ 16 is 5, so Array(5) is "5". 92 And 15 is 12. Array(12) is "C". So Array(5) & Array(12) is "5C".
For the case statement you'd need a string consisting of Chr$(1) & Chr$(9) & Chr$(12) ... or you could take your hex string (the one you'd display) and use your current case statement.
BTW, you should really use the OnComm event to get serial input. Polling the port is very CPU-intensive. Also, there's no need to use the DoEvents() function if you're not going to use the return - just use the DoEvents statement.