Results 1 to 14 of 14

Thread: Hex problems

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    2

    Hex problems

    Hi..

    I'm having some trouble trying to send some information through to a controller connected to my serial port.

    I'm trying to send an FF but its being received as a 3F

    Heres how I've been sending it

    Serialport1.Output(Chr(&HFF))

    If I dont send it with the Chr() it comes out as 32 35 35 which is no good as my controller wont reply if its not in the format of FF

    Any help anyone can give me would be greatly appreciated.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    2

    Re: Hex problems

    I tried rewriting the code and using a different serial port ocx.. heres what i have

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With SerialConnection1
    .BaudRate = 9600
    .PortName = "COM1"
    .Open()
    End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SerialConnection1.Write(Chr(255))
    End Sub

    My port sniffer is still coming up with a 3F though

    Not sure if this helps, but I did a search on google to see if anyone else had the same trouble as me, and came across this post http://www.vbip.com/forum/topic.asp?id=4428
    Last edited by torpkevuk; Jul 30th, 2005 at 01:38 PM.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex problems

    How is SerialConnection1 declared? Is the paramater of the .Write method typed as a String?

  4. #4
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Question Re: Hex problems

    Quote Originally Posted by penagate
    How is SerialConnection1 declared? Is the paramater of the .Write method typed as a String?
    Hi There,

    Just having the exact same problem.
    Here's the code, very simple!:
    ---------------------------
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If SerialPort1.IsOpen = False Then
    SerialPort1.Open()
    End If

    SerialPort1.Write(Chr(255))
    -------------------------
    The output gives in the port-monitor a "?" and hex-code 3F.
    I need it to give the hex-stringoutput " AA AA 09 "
    I found the vbTAB to give me the "09" part, similar to the dec.code for TAB.
    Now the "AA" should be dec.170 similar to ª.

    I've been dangling with this crap for two weeks now, trying so many combinations and so, only having found the "09"..

    If anyone could have an idea of solving this - still simple problem, it'd be much appreciated.

    By the way, I'm using vb 2005 B2

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex problems

    I don't the .NET equivalent for this function offhand but in VB I would use StrConv, since VB strings are Unicode and you look like you are sending ANSI.

    VB Code:
    1. SerialPort1.Write(StrConv(Chr$(&HAA) & Chr$(&HAA) & Chr$(&H09), vbFromUnicode))

  6. #6
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: Hex problems

    Quote Originally Posted by penagate
    I don't the .NET equivalent for this function offhand but in VB I would use StrConv, since VB strings are Unicode and you look like you are sending ANSI.

    VB Code:
    1. SerialPort1.Write(StrConv(Chr(&HAA) & Chr(&HAA) & Chr(&H09), vbFromUnicode))
    It seems that the "vbFromUnicode" does not apply to the .net...
    It only has types like lowercase, uppercase and so..

    Any ideas? It seems to be totally impossible to find anything or anyone with similar problems.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex problems

    You gotta know where to look

    According to KB 311338:
    VB Code:
    1. Bytes = System.Text.Encoding.GetEncoding(1252).GetBytes(SomeString)

    Now, what does the .Write method accept as a parameter?

  8. #8
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: Hex problems

    It accepts .Write(buffer() As Byte, offset As integer, Count As Integer)
    buffer: The byte array to which the output is written.

    and ofcourse also a string..

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex problems

    OK, in that case it's much easier. Just chuck the whole load in a byte array.

    VB Code:
    1. ' I need to learn how to initialise arrays in VB.NET :D
    2. Dim chBuf(2) As Byte
    3. chBuf(0) = &HAA
    4. chBuf(1) = &HAA
    5. chBuf(2) = &H09
    6.  
    7. SerialPort1.Write(chBuf, 0 , 3)

    assuming that Count = length of transmission in bytes.

    You may also need to add a terminating null character.

  10. #10
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: Hex problems

    Quote Originally Posted by penagate
    OK, in that case it's much easier. Just chuck the whole load in a byte array.

    VB Code:
    1. ' I need to learn how to initialise arrays in VB.NET :D
    2. Dim chBuf(2) As Byte
    3. chBuf(0) = &HAA
    4. chBuf(1) = &HAA
    5. chBuf(2) = &H09
    6.  
    7. SerialPort1.Write(chBuf, 0 , 3)

    assuming that Count = length of transmission in bytes.

    You may also need to add a terminating null character.
    WOW, it's that simple.! Now, i'm quite new to coding, so I guess I gotta learn to translate those - to me - cryptic help popups..!

    Now what are you saying about that term. null char?

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex problems

    A null character is just a zero

    Strings in pretty much all languages are null-terminated. Because they are (usually) variable in length, the null character is appended to signify the end of the string data. Otherwise, whatever function it was would keep on reading memory (thinking it was string data) until it chucked an access violation

    I'm not sure whether you need to send terminating null characters in serial transmissions (I suspect you do), just in case here's how you do it.

    VB Code:
    1. ' Still haven't found out how to initialise an array properly :)
    2. Dim chBuf(3) As Byte
    3. chBuf(0) = &HAA
    4. chBuf(1) = &HAA
    5. chBuf(2) = &H09
    6. chBuf(3) = 0
    7.  
    8. ' and then, the length is just increased by 1 byte
    9. SerialPort1.Write(chBuf, 0, 4)

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex problems

    w00t, you learn something every day

    VB Code:
    1. Dim chBuf(3) As Byte = {&HAA, &HAA, &H09, 0}
    2. SerialPort1.Write(chBuf, 0, 4)

  13. #13
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: Hex problems

    Quote Originally Posted by penagate
    w00t, you learn something every day

    VB Code:
    1. Dim chBuf(3) As Byte = {&HAA, &HAA, &H09, 0}
    2. SerialPort1.Write(chBuf, 0, 4)
    Man... you are sooo kind!! This is really appreciated!!!

    What i'm using this for is a I/O interface card board thingy and it was sold with a sample software, but no source code.

    Well.. the last thing i think i need is something like a "enter" or something.
    If I paste the ª ª "tab" sequence in hyperterminal the unit responds with the status of the input lines. My port-monitor shows AA AA 09 and then makes a new line.

    wonder what the difference from this is...

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex problems

    How to do a new line depends on the platform. On Windows and DOS it is CR plus LF (carriage return and line feed) and that is character codes 13 and 10 respectively. However on some platforms you can get away with just sending the carriage return character.

    Try these:
    VB Code:
    1. Dim chBuf(5) As Byte = {&HAA, &HAA, &H09, 10, 13, 0}
    2. ' or
    3. Dim chBuf(4) As Byte = {&HAA, &HAA, &H09, 13, 0}
    and remember that the Count parameter takes the UBound of the array + 1, because the array is zero based

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