dcsimg
Results 1 to 5 of 5

Thread: Reading 2 bytes from RS232

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    17

    Reading 2 bytes from RS232

    Hi,

    I am sending 2 bytes continuously from a RS232 to my PC. First I send F and then G... However I don't know how to read both the characters in Visual Basic. I js used strBuffer = MSComm1.Input and I get some weird symbol and a ?. That is if I run in debug mode. Otherwise, I only get the weird symbol. Can someone help me solve this problem? Thanks.

    z_jun

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Reading 2 bytes from RS232

    if you harvest the inputs into a string, there vb might tries to convert the input as to unicode, resulting the unwanted '?' symbols are displayed, because the received bytes are meant to be 16bit values. however, if you just set the InputMode to comInputModeText the received data will be hexadecimal representation of the input, so it can be shown in the immediate window, or anywhere else on the form.

    a second workaround could be is to declare a byte array, set the InputMode to comInputModeBinary, then get the input into this array. then, you can assign this array to the string by using the StrConv() method, that it will make a unicode (16bit) representation of this byteflow, that you can store in a string for displayings and such.

    Code:
      Redim bArray(MSComm1.InputLen-1) as Byte
      bArray = MSComm1.Input
      strBuffer = StrConv(bArray, vbUnicode)
    hope it helps, i can't test it.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    17

    Re: Reading 2 bytes from RS232

    hmm...my problem is the the RS232 sends byte by byte...so when I have strbuffer = MSComm1.Input...I"m not sure if it reads one byte or both the bytes I'm trying to receive...

  4. #4
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Reading 2 bytes from RS232

    switch to comInputModeText there you can find out how it is actually sends the data. printing out binary content is not a good idea, because some of the characters are not displayed correctly.

    if you just receive 'random' content, probably you also have to setup the .Settings property too, to make it suitable for the input.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    17

    Re: Reading 2 bytes from RS232

    have solved it already...had to make sure the baud rates were equal...thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width