Results 1 to 2 of 2

Thread: Sending Hex bytes Over TCP Problem!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    4

    Sending Hex bytes Over TCP Problem!!!

    Hi,

    I am currently working on a project at work for a print and inspect system, this involves an Omron Camera system, which is basically a Windows CE machine and an APS printer. The Camera system is connected to the printer via Ethernet. I have the comunications working fine and can send commands to the printer, which consist of a number of Hex numbers:

    This is a typical command: (this switches the print head on)

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

    Dim networkstream As NetworkStream = tcpClient.GetStream
    If networkstream.CanWrite And networkstream.CanRead Then

    'Turn Print Head On

    Dim sendBytes As [Byte]() = {&H0, &H0, &H0, &H0, &H0, &HE, &H1, &H65, &H7, &H0, &H0, &H0, &H1, &H1, &H0, &H1, &HFF, &HFF, &HFF, &H0}

    networkstream.Write(sendBytes, 0, sendBytes.Length)

    End if

    When the command is sent in this way it works fine!. My problem is, some of the command strings need to change as they contain text to be printed etc...

    So I've created some text boxes so the printed text can be changed, then extracted the values from the text boxes and converted them to Hex then concatenated them together to create a string like this &H0, &H0, &H0, &H0, &HFF etc......... This string is stored in a string variable. Lets call it (TEST) for this example.

    If I then do the following:

    Dim sendBytes As [Byte]() = (TEST)

    networkstream.Write(sendBytes, 0, sendBytes.Length)

    I get the good old sqiggly line under TEST and the message "Value of type 'String' cannot be converted to '1-dimensional array of byte' !!!

    If I then add the squiggly brackets:

    Dim sendBytes As [Byte]() = {(TEST)}

    networkstream.Write(sendBytes, 0, sendBytes.Length)

    I dont get the squiggly line, but when I try and run the command, its as if the printer isnt receiving anything at all (I can monitor this on the printer).

    I have tried the following too, to try and convert the string to a byte array:

    Public Shared Function StrToByteArray(ByVal str As String) As Byte()

    Dim encoding As New System.Text.ASCIIEncoding()

    Return encoding.GetBytes(str)

    End Function

    So my command now looks like this:

    Dim networkstream As NetworkStream = tcpClient.GetStream
    If networkstream.CanWrite And networkstream.CanRead Then

    Dim TEST as String = "&H0, &H0, &H0, &HFF, &HFF" 'etc etc.......

    'Turn Print Head On

    Dim sendBytes As [Byte]() = StrToByteArray(TEST)

    networkstream.Write(sendBytes, 0, sendBytes.Length)

    End if

    This is sending something to the APS printer because I can see on the printer that it is returning an error code.

    It seems as though the data isn't in the same format as if I simply put the hex values in to the byte array as in my first bit of sample code earlier in this post.

    I hope this all makes sense to someone! I'm at the "hair pulling out stage" with this one.

    If anyone can help with this, or knows another/better way of sending the hex bytes to the printer, or has any other ideas, it'd be greatly appreciated.

    Thanks

    Parkyi

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Sending Hex bytes Over TCP Problem!!!

    You seem to be confusing Hex with something. What you are trying to get is the string converted to a byte array. That can be done with something like

    Dim SendBytes as Byte() = System.Text.Encoding.ASCII.GetBytes(TEST)

    (it may be just Byte, not Byte()).

    That will take the string and turn it into a byte array. You did the same thing with your function, but then you passed in a totally bizarre string:

    Dim TEST as String = "&H0, &H0, &H0, &HFF, &HFF" 'etc etc.......

    That's not even close to being what you think it is. For example, that will turn &H0 into three bytes because it will turn the & into one byte, the H into one byte, and the 0 into one byte (and not 0, either, but the ASCII representation of the character "0"). &H as a prefix is only telling the compiler that the number following it is a hexidecimal number rather than a decimal number. The string you actually want is this:

    Test As String = Char(&H0) & Char (&H0) & Char(&H0) & Char(&HFF) &.....etc.

    or else in decimal:

    Test as String = Char(0) & Char(0) & char(0) & char(128) & .....etc.

    Kind of an awkward thing to be doing. A better solution might be to have the text string that you want to insert, build your Byte array as a List (of Byte), convert your text string to a byte array (you already have the code for that), then insert the range into the list, and convert the list to a byte array with .ToArray.
    My usual boring signature: Nothing

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