dcsimg
Results 1 to 11 of 11

Thread: Sending RS232 Hex commands.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    4

    Sending RS232 Hex commands.

    Greetings,

    This is my first time creating a program. I haven't started from the beginning of programming, I just dove right into the middle of it. As a result, I know what I know based on what I have dealt with so far. My program basically works, in that, I can pick my com port, send commands, both on a button press and also on release. My problem comes with sending hex commands. I have tried to send commands many different ways, always getting something, but not quite right. My last hurdle is trying to send for example: 81 01 06 01 09 09 03 01 FF in hex.

    If I use:
    SendCommand(Chr(&H81) & Chr(&H1) & Chr(&H6) & Chr(&H1) & Chr(&H9) & Chr(&H9) & Chr(&H3) & Chr(&H3) & Chr(&HFF))

    I get:
    3F 01 06 01 09 09 03 03 3F 0A

    I kind of know what the problem is, but I can't find what the solution should be. Chr has only 128 characters in it, the highest being 7F. 81 seems to be off the table, and so is FF. The output seems to be correct except for these two values. I am going to attempt to paste my program below. My apologies if it doesn't format correctly.

    Code:
    ' Camera Control on COM Port 
    
    Option Explicit On
    Option Strict On
    Imports System.IO.Ports
    
    Public Class Form1
    
        Dim myComPort As New SerialPort
    
        ''' <summary>
        ''' Call a routine to write a command.
        ''' </summary>
        ''' 
        ''' Tilt Up
    
        Private Sub UpBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpBTN.MouseDown
    
            SendCommand(Chr(&H81) & Chr(&H1) & Chr(&H6) & Chr(&H1) & Chr(&H9) & Chr(&H9) & Chr(&H3) & Chr(&H3) & Chr(&HFF))
    
        End Sub
        Private Sub UpBTN_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpBTN.MouseUp
    
            SendCommand(Chr(&H81) & Chr(&H1) & Chr(&H6) & Chr(&H1) & Chr(&H9) & Chr(&H9) & Chr(&H3) & Chr(&H3) & Chr(&HFF) & vbCrLf)
    
        End Sub
    
        ''' Tilt Down
    
        Private Sub DownBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownBTN.MouseDown
    
            SendCommand(Chr(&H81) & Chr(&H1) & Chr(&H6) & Chr(&H1) & Chr(&H9) & Chr(&H9) & Chr(&H3) & Chr(&H2) & Chr(&HFF) & vbCrLf)
    
        End Sub
        Private Sub DownBTN_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownBTN.MouseUp
    
            SendCommand(Chr(&H81) & Chr(&H1) & Chr(&H6) & Chr(&H1) & Chr(&H9) & Chr(&H9) & Chr(&H3) & Chr(&H3) & Chr(&HFF) & vbCrLf)
    
        End Sub
    
        ''' Pan Left
    
        Private Sub LeftBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftBTN.MouseDown
    
            SendCommand("PAN RIGHT" & vbCrLf)
    
        End Sub
        Private Sub LeftBTN_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftBTN.MouseUp
    
            SendCommand("STOP" & vbCrLf)
    
        End Sub
    
        ''' Pan Right
    
        Private Sub RightBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightBTN.MouseDown
    
            SendCommand("PAN RIGHT" & vbCrLf)
    
        End Sub
        Private Sub RightBTN_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightBTN.MouseUp
    
            SendCommand("STOP" & vbCrLf)
    
        End Sub
    
        ''' Zoom In
    
        Private Sub InBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InBTN.MouseDown
    
            SendCommand("ZOOM IN" & vbCrLf)
    
        End Sub
        Private Sub InBTN_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InBTN.MouseUp
    
            SendCommand("STOP" & vbCrLf)
    
        End Sub
    
        ''' Zoom Out
    
        Private Sub OutBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutBTN.MouseDown
    
            SendCommand("ZOOM OUT" & vbCrLf)
    
        End Sub
        Private Sub OutBTN_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutBTN.MouseUp
    
            SendCommand("STOP" & vbCrLf)
    
        End Sub
    
        ''' Position 1
    
        Private Sub Pst1_BTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pst1_BTN.MouseDown
    
            SendCommand("POSITION 1" & vbCrLf)
    
        End Sub
    
        ''' Position 2
    
        Private Sub Pst2_BTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pst2_BTN.MouseDown
    
            SendCommand("POSITION 2" & vbCrLf)
    
        End Sub
    
        ''' Position 3
    
        Private Sub Pst3_BTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pst3_BTN.MouseDown
    
            SendCommand("POSITION 3" & vbCrLf)
    
        End Sub
    
        ''' Position 4
    
        Private Sub Pst4_BTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pst4_BTN.MouseDown
    
            SendCommand("POSITION 4" & vbCrLf)
    
        End Sub
    
        ''' Position 5
    
        Private Sub Pst5_BTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pst5_BTN.MouseDown
    
            SendCommand("POSITION 5" & vbCrLf)
    
        End Sub
    
        ''' Position 6
    
        Private Sub Pst6_BTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pst6_BTN.MouseDown
    
            SendCommand("POSITION 6" & vbCrLf)
    
        End Sub
    
        ''' <summary >
        ''' If myComPort is open, finish transmitting.
        ''' Exiting the Using block closes the port and releases its resources.
        ''' </summary>
        ''' 
        Sub CloseComPort()
    
            Try
    
                Using myComPort
    
                    If (Not (myComPort Is Nothing)) Then
    
                        ' The COM port exists.
    
                        If myComPort.IsOpen Then
    
                            ' Wait for the transmit buffer to empty.
    
                            Do While (myComPort.BytesToWrite > 0)
                            Loop
    
                        End If
                    End If
    
                End Using
    
            Catch ex As UnauthorizedAccessException
    
                ' The port may have been removed. Ignore.
    
            End Try
    
        End Sub
    
        ''' <summary>
        ''' Set the BaudRate property of myComPort to match the bit rate selected in the combo box.
        ''' </summary>
        ''' 
        Private Sub CmbBitRate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbBitRate.SelectedIndexChanged
    
            myComPort.BaudRate = CInt(CmbBitRate.SelectedItem)
    
        End Sub
    
        ''' <summary>
        ''' If the previously selected COM port is open, close it.
        ''' Set the PortName property of myComPort to match the port selected in the combo box.
        ''' Call a routine to open the port.
        ''' </summary>
        ''' 
        Private Sub CmbPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbPorts.SelectedIndexChanged
    
            CloseComPort()
            myComPort.PortName = CmbPorts.SelectedItem.ToString
            OpenComPort()
        End Sub
    
        ''' <summary>
        ''' Call a routine to close the COM port.
        ''' </summary>
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    
            CloseComPort()
    
        End Sub
    
        ''' <summary>
        ''' Call routines to initalize the form and open the selected COM port.
        ''' </summary>
        ''' 
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            InitializeForm()
            OpenComPort()
    
        End Sub
    
        ''' <summary>
        ''' Set up the form and select a default port and bit rate.
        ''' </summary>
    
        Sub InitializeForm()
    
            Dim bitRates(9) As Integer
            Dim nameArray() As String
    
            ' Find the COM ports on the system.
    
            nameArray = SerialPort.GetPortNames
            Array.Sort(nameArray)
    
            ' Fill a combo box with the port names.
    
            CmbPorts.DataSource = nameArray
            CmbPorts.DropDownStyle = ComboBoxStyle.DropDownList
    
            ' Select a default port.
    
            CmbPorts.SelectedIndex = 1
    
            'Bit rates to select from.
    
            bitRates(0) = 300
            bitRates(1) = 600
            bitRates(2) = 1200
            bitRates(3) = 2400
            bitRates(4) = 9600
            bitRates(5) = 14400
            bitRates(6) = 19200
            bitRates(7) = 38400
            bitRates(8) = 57600
            bitRates(9) = 115200
    
            'Place the bit rates in a combo box.
    
            CmbBitRate.DataSource = bitRates
            CmbBitRate.DropDownStyle = ComboBoxStyle.DropDownList
    
            ' Select a default bit rate.
    
            CmbBitRate.SelectedItem = 1200
    
        End Sub
    
        ''' <summary>
        ''' Set port parameters and open the COM port 
        ''' associated with the SerialPort object myComPort.
        ''' </summary>
    
        Sub OpenComPort()
    
            Try
                ' Get the selected COM port's name from the combo box.
    
                If Not myComPort.IsOpen Then
                    myComPort.PortName = CmbPorts.SelectedItem.ToString
    
                    ' Get the selected bit rate from the combo box.
    
                    If CmbBitRate.SelectedIndex > 0 Then
                        myComPort.BaudRate = CInt(CmbBitRate.SelectedItem)
                    End If
    
                    ' Set other port parameters.
    
                    myComPort.Parity = Parity.None
                    myComPort.DataBits = 8
                    myComPort.StopBits = StopBits.One
                    myComPort.Handshake = Handshake.None
    
                    myComPort.ReadTimeout = 3000
                    myComPort.WriteTimeout = 5000
    
                    ' Open the port.
    
                    myComPort.Open()
    
                End If
    
            Catch ex As InvalidOperationException
                MessageBox.Show(ex.Message)
    
            Catch ex As UnauthorizedAccessException
                MessageBox.Show(ex.Message)
    
            Catch ex As System.IO.IOException
                MessageBox.Show(ex.Message)
    
            End Try
        End Sub
    
        ''' <summary>
        ''' Write a command to the SerialPort object and read the response.
        ''' </summary>
        ''' <param name= "command"> The command to send. </param>
    
        Private Sub SendCommand(ByVal command As String)
    
    
    
            myComPort.WriteLine(command)
    
    
        End Sub
    
        Private Sub UpBTN_MouseUp(sender As Object, e As MouseEventArgs) Handles UpBTN.MouseUp
    
        End Sub
    
        Private Sub LeftBTN_MouseDown(sender As Object, e As MouseEventArgs) Handles LeftBTN.MouseDown, Pst1_BTN.MouseDown, Pst6_BTN.MouseDown, Pst5_BTN.MouseDown, Pst4_BTN.MouseDown, Pst3_BTN.MouseDown, Pst2_BTN.MouseDown
    
        End Sub
    
        Private Sub LeftBTN_MouseUp(sender As Object, e As MouseEventArgs) Handles LeftBTN.MouseUp, Pst1_BTN.MouseUp, Pst6_BTN.MouseUp, Pst5_BTN.MouseUp, Pst4_BTN.MouseUp, Pst3_BTN.MouseUp, Pst2_BTN.MouseUp
    
        End Sub
    
        Private Sub Pst1_BTN_Click(sender As Object, e As EventArgs) Handles Pst1_BTN.Click, Pst6_BTN.Click, Pst5_BTN.Click, Pst4_BTN.Click, Pst3_BTN.Click, Pst2_BTN.Click
    
        End Sub
    End Class

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Sending RS232 Hex commands.

    This is a thing that gets my goat, part of the problem is people who don't know what they're doing write bad tutorials, then people like you read them and don't know they're bad, then you end up confused by the things the original person didn't understand at all.

    Break the habit, right now, of thinking about "hex commands". They don't exist, and thinking about them this way is part of what's making it hard for you to continue.

    Data is bits, which we organize into bytes. &H81 is not "a hex number", it is just "a number". 129, to be exact. Or 10000001. It doens't actually matter which representation we use, it's the same thing. The main reason we use hexadecimal at all is because 4 bits represent 16 values and so does 1 hexadecimal digit. So it's easy to remember &H8 = 1000 or &HA = 1010 and thus it's very convenient to represent 1 byte with a 2-digit hexadecimal number if we care about the binary. When we're talking about character data, we prefer to use decimal. I can remember 'A' is 65 in ASCII, but I had to do a lookup to know it's &H41 because I'm not accustomed to using hexadecimal to represent ASCII.

    In short: "Hex is a way to talk about numbers, but is not a special data type in and of itself."

    In addition: The biggest mistake people make is believing that "hex" is some kind of special data type, or that it's related to Char. That's something people believe if they see C code, but the reasons why C does that are kind of dumb and have nothing to do with VB.

    The COM port doesn't work with "hex data". It works with "arrays of Byte". Byte is an 8-bit integer type, so it's just the numbers 0-255. Or if you want to talk about it with hexadecimal representation, it's &H00 through &HFF. Those are still numbers.

    (C uses char for this because in the very early language, char was represented as an 8-bit integer and there was no other platform-agnostic 8-bit integer type! But now in almost every language, char can be anything from 1 byte to 4 bytes depending on a lot of complicated rules, so smart modern languages have a type like Byte that is EXPLICITLY an 8-bit integer.)

    If you want a Byte array, you can make it with numbers:
    Code:
    Dim data() As Byte = { 1, 2, 3, 4 }
    If your task is more comfortable with hexadecimal representation, that works too:
    Code:
    Dim data() As Byte = { &H1, &H2, &H3, &H4 }
    Both of the arrays above are identical. There's nothing special about &H1 compared to 1, they are both the number 1.

    So what really helps you imagine the problem is to stop thinking of "hex commands" and be more comfy thinking of them as "numbers". You have to send some specific numbers to your device to make things work, and hex makes for a convenient representation.

    Using Chr() or ChrW() isn't really the right way to go about that. To make a long story short, Chr() and ChrW() use special rules defined by an "encoding" to decide how a particular number converts to a character. For some numbers, the rules might dictate "that number does not represent a character". Oops! If you're only seeing values &H7F and below, that's because Chr() uses ASCII, and the strict ASCII standard only uses the lower 7 bits. ANSI code pages can use the values with the high bit set, but there's no guarantee every possible value from 0-255 is a valid character.

    Or, put shorter: you use Char to build Strings like "Hello". The COM port uses an encoding to figure out how to convert "Hello" to a Byte array, then sends those bytes for you.

    But if you want to send specific numbers, you should just make the Byte array yourself and build it out of numbers. That way no other rules interfere, you just send what you want.

    That doesn't mean you never send strings. Some devices use command strings like "SET MODE=A1024". When you are working with one of those, you might use Chars and Strings and your version of SendCommand(). But that method is just doing the work of converting the String to Bytes, then sending each byte to the device for you.

    But if you just want to send some bytes, you want a version that looks like this:
    Code:
    Private Sub SendBytes(ByVal data() As Byte)
        myComPort.Write(data, 0, data.Length)
    End Sub
    You would call it like this:
    Code:
    Dim dataToSend() As Byte = { &H81, &H01, &H06, &H01, &H09, &H03, &H03, &HFF }
    SendBytes(dataToSend)
    So, again, I'm repeating it because a lot of people get confused by it:
    • If you're sending text to the device, use Strings and WriteLine() like you did in your SendCommand() Sub. Some devices want you to work with them this way.
    • If you're sending "hex", 99% of the time that means "numbers" which means "byte arrays". Use byte arrays and the Write() method that works with Byte arrays.


    RARELY I've seen a weird device that actually wants to get hexadecimal digits as a String. I think that's what confuses people the most. Here's about the best way I can quickly explain that:
    • "FF" is a string that is two "letter F" characters. It takes two bytes to send it, those bytes are probably { &H46, &H46 }.
    • &HFF is a number in hexadecimal. It represents the decimal number 255 and is a valid Byte.
    • &HF, &HF is two numbers in hexadecimal. They are two bytes, { 15, 15 } if we represent them in decimal.

    You have to know which you are sending: Strings or numbers. Then you have to use the right rule for sending them.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    4

    Re: Sending RS232 Hex commands.

    Than you for your reply. While I have seen the explanation for the use of the word hex, it's still worth mentioning because terminal programs and devices do differentiate when deciphering the string. If I turn of hex in my terminal program, I get   as the display, which isn't correct at all. I don't want to sound like I am arguing, but hex is a real thing in the integration world, even if it means nothing in the program world. I still need the string to come out the other end of the pipe as 81 01 06 01 09 09 03 01 FF

    With that being said, how would I correct this line?

    Code:
    SendCommand(Chr(&H81) & Chr(&H1) & Chr(&H6) & Chr(&H1) & Chr(&H9) & Chr(&H9) & Chr(&H3) & Chr(&H3) & Chr(&HFF))

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Sending RS232 Hex commands.

    What you are explaining to me is something you don't understand at the depth I am explaining. It's all bytes. The sooner you realize that, the sooner you can proceed.

    Let's take this piece by piece.

    • Chr() gives you the character represented by a number in ASCII.
    • In ASCII, &H81 is not a valid character, because only &H0 through &H7F are supported.
    • ASCII's behavior when it tries to translate a character it can't recognize is to replace it with '?'.
    • A chart confirms that ? corresponds to &H3F.
    • So any value > &H7F gets replaced with &H3F

    This is because you are not sending "ASCII text". If you had "ASCII text", you'd have no bytes over &H7F. You are trying to send bytes. I know this because if you were sending text, even with extended ASCII, the characters in the extended range would be in a pattern that makes sense for some language.

    For example, this is a sensible string: { &H66, &H61, &H87, &H61, &H64, &H65 }. That is the word "faade". But if we convert it with Chr(), the will get converted to ? and we'll have "fa?ade". Your string is "<SOH><ACK><SOH><TAB><TAB><ETX><ETX><DEL>". I don't know what kind of English you're speaking, but I don't call that "text". It is a byte packet. It should be sent as bytes, because it only has 1 character in the entire string that makes any sense in a common encoding.

    Let's say it was magic, and did what you wanted. Here's how the process would work:
    • You build a String with Chr(&H81) in it. That is the character .
    • You give the String to the SerialPort.WriteLine() method.
    • It has to convert the String to a Byte array, so it asks the ASCII encoding what the value of is.
    • The encoding answers &H81.
    • The byte array { &H81 } is sent.


    So:
    • You know you want to send the byte &H81.
    • I am telling you to build an array with the byte { &H81 } and send it.
    • You are disagreeing, and insist that , the character represented by the byte { &H81 }, is not the same thing as the byte { &H81 }.

    Try the code I suggested. If it doesn't work, your byte sequence is wrong. Any other expert will verify.

    All data is bytes. "A character" could be 1-4 bytes, depending on encoding. An Integer is 4 bytes. A Long is 8 bytes. A Double is 8 bytes, interpreted differently. Part of what makes this confusing is I can literally show you at least 5 different byte sequences I could get a computer to interpet as the string "™∞". None of them work with strict ASCII.
    Last edited by Sitten Spynne; Dec 15th, 2017 at 05:30 PM.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    4

    Re: Sending RS232 Hex commands.

    I agree that I am having trouble with everything over 7F, and as I suspected, you cant get there with Chr. That's about the extent I am confident. I understand what you want me to do, I am just not sure how to set it up. If you can help me with this one phrase, I think I can fix the rest. How can I get 81 01 06 01 09 09 03 01 FF to come out the other end of the pipe? I tried below, but VB is not accepting it.

    Code:
    Private Sub UpBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpBTN.MouseDown
    
            SendCommand({&H81, &H01, &H06, &H01, &H09, &H09 &H03 &H01 &HFF})
    
        End Sub

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Sending RS232 Hex commands.

    Look closely at the code I provided in the post that had a lot of words. I wrote them because they meant something. Lines of code are not magic if you don't know the words that describe what they are doing. When that happens they're just dangerous.

    The code itself looked like this:
    Code:
    Private Sub SendBytes(ByVal data() As Byte)
        myComPort.Write(data, 0, data.Length)
    End Sub
    That code goes in your file and uses the variable I assume is your SerialPort instance to write an array of bytes.

    When you want to send some bytes, you can use that method like this:
    Code:
    Dim dataToSend() As Byte = { &H81, &H01, &H06, &H01, &H09, &H03, &H03, &HFF }
    SendBytes(dataToSend)
    The code you tried didn't work because it doesn't have both pieces. You can't give an array of Byte to a function that wants a String, and you can't use WriteLine() on just about anything to send an array of Byte. Those methods expect to work with Strings. So when you want to work with arrays of Bytes, you have to use methods that expect arrays of Bytes.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,848

    Re: Sending RS232 Hex commands.

    This,
    Code:
    Private Sub UpBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpBTN.MouseDown
    
            SendCommand({&H81, &H01, &H06, &H01, &H09, &H09 &H03 &H01 &HFF})
    
        End Sub
    doesn't look like what sitten told you to do,
    Code:
    Private Sub SendBytes(ByVal data() As Byte)
        myComPort.Write(data, 0, data.Length)
    End Sub
    Code:
    You would call it like this:
    
    Dim dataToSend() As Byte = { &H81, &H01, &H06, &H01, &H09, &H03, &H03, &HFF }
    SendBytes(dataToSend)
    Does it??

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,657

    Re: Sending RS232 Hex commands.

    Believe it or not Sitten, there are interfaces that want an ASCII string of hex representation of the data sent, not the raw bytes.

    Digital Identity, give something like this a try.
    Code:
            Dim msg() As Byte = {&H81, &H1, &H6, &H1, &H9, &H9, &H3, &H1, &HFF}
            Dim sb As New System.Text.StringBuilder
            For Each b As Byte In msg
                sb.Append(b.ToString("X2"))
            Next
            SendCommand(sb.ToString)
    I don't know if this is one of those interfaces, but it does seem like it is leaning that way. I agree most of the interfaces I've used has just been the byte values directly, but I know that at least once I had to interface with a device that expected hex string representation.

    If sending the hex string representation doesn't work, then perhaps Sitten's assumption is correct, and you're just not getting the combination right.

    p.s. Since this appears to be a camera control interface, and from what I've seen, most of them do use a byte array of values for the command messages, not an ASCII hex string, what SittenSpynne has given should work. As I noted, it is very rare to have an interface that expects a hex string representation.

    p.p.s. The more I read your posts, DI, the more I'm convinced that Sitten is correct. When you select to display the values in Hex, you're seeing the byte values converted to hex strings and it is "almost" what you want. That is definitely an indication that you don't want to send hex string representation, otherwise when converted to hex strings it would not look anything like what you want. Perhaps ironically, if you turned off "Hex" in your terminal program, then the above code would look like what you want, but it wouldn't be what you want.

    Bottom line, I think what Sitten has provided in post #2, and wes4dbt has reiterated, is what you want, so this post can be ignored.
    Last edited by passel; Dec 16th, 2017 at 01:10 AM.

  9. #9
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Sending RS232 Hex commands.

    I believe it because I wrote:
    RARELY I've seen a weird device that actually wants to get hexadecimal digits as a String. I think that's what confuses people the most. Here's about the best way I can quickly explain that:


    I think those devices are the ones that confuse early programmers the most.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    4

    Re: Sending RS232 Hex commands.

    Okay,
    I have had the chance to try this, and thank you Sitten, your suggestion worked! As to PowerPoster, if your not helping, go back to the peanut gallery. Sitten gave several examples, and I was trying one that looked like it would drop in to what I was doing, or at least show how I was interpreting the suggestion. While I was unsure just where to add those statements, I tried it where it made sense to me, and it worked. Part of learning is understanding why your logic is flawed, and how the advice corrects that thinking. I am not challenging to prove myself or disprove others, but rather reexplain my confusion. Much respect for those who are willing to take the time to teach others. For now, I have much to learn!

    Code:
    ' Camera Control on COM Port 
    
    Option Explicit On
    Option Strict On
    Imports System.IO.Ports
    
    Public Class Form1
    
        Dim myComPort As New SerialPort
    
        Private Sub SendBytes(ByVal data() As Byte)
            myComPort.Write(data, 0, data.Length)
        End Sub
    
        ''' <summary>
        ''' Call a routine to write a command.
        ''' </summary>
        ''' 
        ''' Tilt Up
    
        Private Sub UpBTN_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpBTN.MouseDown
    
            Dim dataToSend() As Byte = {&H81, &H1, &H6, &H1, &H9, &H9, &H3, &H1, &HFF}
            SendBytes(dataToSend)
    
        End Sub
        Private Sub UpBTN_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpBTN.MouseUp
    
            Dim dataToSend() As Byte = {&H81, &H1, &H6, &H1, &H9, &H9, &H3, &H3, &HFF}
            SendBytes(dataToSend)
    
        End Sub
    blah blah blah... and so on.

    Thanks again.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,657

    Re: Sending RS232 Hex commands.

    Quote Originally Posted by Sitten Spynne View Post
    I believe it because I wrote:
    RARELY I've seen a weird device that actually wants to get hexadecimal digits as a String. I think that's what confuses people the most. Here's about the best way I can quickly explain that:


    I think those devices are the ones that confuse early programmers the most.
    I agree. And sorry that I missed that part of your post. I even gave a second quick look over before I posted, but still missed it. Oh well. I'm sure we'll still be seeing this issue come up now and then for years to come.

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