|
-
Sep 2nd, 2011, 10:38 AM
#1
Thread Starter
New Member
Missing leading zeros when reading RS232
Hello,
I'm stucked at the problem with reading rs232 and showing it in Textbox.
If I send from one computer using Docklight: "000 220 111 000",
I receive in texbox of my Visual Basic program: "02201110.
Where are missing zeros? I need them.
This is the part of code where I read RS232 and send it to Textbox:
Private Sub MSComm1_OnComm()
Dim strInput As String
With MSComm1
'test for incoming event
Select Case .CommEvent
Case comEvReceive
'display incoming event data to displaying textbox
strInput = .Input
Text1.SelText = Asc(strInput)
End Select
End With 'MSComm1
End Sub
-
Sep 2nd, 2011, 10:51 AM
#2
Fanatic Member
Re: Missing leading zeros when reading RS232
Data will come in in "chunks," not one-byte at a time.
Dim I As Integer
'then, add a loop in your code:
strInput = .Input
For I = 1 to Len(strInput)
Text1.SelText = Asc(Mid(strInput, I, 1)
Next I
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Sep 4th, 2011, 11:41 PM
#3
Thread Starter
New Member
Re: Missing leading zeros when reading RS232
Dear Mr. DickGrier,
thank you for your post, but it wasn't sucesfull, stil the same, I send 011 000 222 and textbox is showing 110222.
-
Sep 5th, 2011, 12:24 AM
#4
Re: Missing leading zeros when reading RS232
Why are you converting your received data to ASCI ? Have you tried
Code:
Text1.SelText = Mid(strInput, I, 1)
-
Sep 5th, 2011, 12:55 AM
#5
Thread Starter
New Member
Re: Missing leading zeros when reading RS232
Yes I did, and I receive fonts instead of numbers. For the code "011 000 222" I receive some fonts and strange signs. And I declared strInput As Integer or as String and I receive the same.
-
Sep 5th, 2011, 12:58 AM
#6
Re: Missing leading zeros when reading RS232
When you send the data from Docklite are you entering it as ASCI and typing the characters 000 220 111 000 ?
Have you checked that the MSCom settings match those of Docklite ie the speed, number of data bits, parity and number of stop bits are identical ?
-
Sep 5th, 2011, 01:08 AM
#7
Thread Starter
New Member
Re: Missing leading zeros when reading RS232
In Docklite I'm entering as decimal values, MSComm settings are the same as in Docklite: Settings = "9600,N,8,1"
-
Sep 5th, 2011, 01:21 AM
#8
Re: Missing leading zeros when reading RS232
Could you try this please:
Code:
Private Sub MSComm1_OnComm()
Dim strInput As String
Dim intI As Integer
With MSComm1
'test for incoming event
Select Case .CommEvent
Case comEvReceive
'display incoming event data to displaying textbox
strInput = .Input
Debug.Print Len(strInput)
For intI = 1 To Len(strInput)
Debug.Print Left("00", 2 - Len(strInput)) & Hex(Asc(Mid$(strInput, intI, 1)));
Next intI
Text1.SelText = Asc(strInput)
End Select
End With 'MSComm1
End Sub
and input 011 from Docklite.
This will output data to the Immediate Window. Copy and paste the results in a Post here.
-
Sep 5th, 2011, 01:26 AM
#9
Thread Starter
New Member
Re: Missing leading zeros when reading RS232
I receive "0B" in immediate window and "11" in textbox
-
Sep 5th, 2011, 01:32 AM
#10
Re: Missing leading zeros when reading RS232
OK. Docklite is converting your input into a binary value as opposed to sending the characters 0,1,1.
If you're emulating a device how will the device be sending data ? Will it be sending 'Decimal' values or characters. If the former, then unless you know how many leading zeros there ought to be, it's going to be very difficult to reconstruct them on receive.
Just out of interest, what output do you get if you send 257 from Docklite?
-
Sep 5th, 2011, 01:35 AM
#11
Thread Starter
New Member
Re: Missing leading zeros when reading RS232
For the above value "000 220 111 000" in HEX "00 DC 6F 00" I receive
1
00 1
0DC 1
06F 1
00
If I put the line for Immediate window in Texbox now, I receive: 000DC06F0
-
Sep 5th, 2011, 01:40 AM
#12
Thread Starter
New Member
Re: Missing leading zeros when reading RS232
The device will send me HEX values, If I put 257 in Dock lite it automatically converts to 255 and I receive 0FF
-
Sep 5th, 2011, 01:44 AM
#13
Re: Missing leading zeros when reading RS232
That's exactly what would be expected. If you're sending decimal values why are the leading zeros important ? You can, for example, use the Format Function to display them in whatever format you require
-
Sep 5th, 2011, 01:55 AM
#14
Re: Missing leading zeros when reading RS232
 Originally Posted by Doogle
You can, for example, use the Format Function to display them in whatever format you require
eg
Code:
Private Sub MSComm1_OnComm()
Dim strInput As String
Dim intI As Integer
Dim strTemp As String
With MSComm1
'test for incoming event
Select Case .CommEvent
Case comEvReceive
'display incoming event data to displaying textbox
strInput = .Input
For intI = 1 To Len(strInput)
strTemp = Mid$(strInput, intI, 1)
Text1.SelText = Format(Asc(strTemp), "000")
Next intI
End Select
End With 'MSComm1
End Sub
EDIT: If you use
Code:
Text1.SelText = Format(Asc(strTemp), "000 ")
it will add a trailing space after each triplet of digits.
Last edited by Doogle; Sep 5th, 2011 at 02:02 AM.
-
Sep 5th, 2011, 02:10 AM
#15
Thread Starter
New Member
Re: Missing leading zeros when reading RS232
This works now as I wanted. I need this, because I will receive long word from device, and first byte will be for one parameter, second for other parameter and so one... I will extract this parameters By using "Mid" function. And the number of positions must be correct. Altough there was missing only format function?
Thank you very much on this. I'm no programmer at all and you helped me a lot.
-
Sep 5th, 2011, 02:19 AM
#16
Re: Missing leading zeros when reading RS232
If the device is sending 4 bytes, for example, 011,010,012,014 (ie hex 0B0A0C0E) the first byte will always be the first byte irrespective of its value, the leading zeros are unimportant.
ie Mid$(strInput, 1, 1) = Hex 0B = Decimal 11 = First Parameter, Mid$(strInput, 2, 1) = Hex 0A = Decimal 10 = Second Parameter etc.
Good luck with the rest of the Project
Last edited by Doogle; Sep 5th, 2011 at 02:25 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|