Results 1 to 16 of 16

Thread: Missing leading zeros when reading RS232

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    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

  2. #2
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    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)

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    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.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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)

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    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.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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 ?

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    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"

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    Re: Missing leading zeros when reading RS232

    I receive "0B" in immediate window and "11" in textbox

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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?

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    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

  12. #12

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    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

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

  14. #14
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Missing leading zeros when reading RS232

    Quote Originally Posted by Doogle View Post
    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.

  15. #15

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    8

    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.

  16. #16
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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
  •  



Click Here to Expand Forum to Full Width