locked
Dead simple example of sending hex data to a serial port

    General discussion

  • Hey guys, I'm offering my 'dead simple' example of writing hex data to a device over a serial port.

    I ran into this problem while trying to write a Pan/Tilt/Zoom controller program. I found some very complicated examples, but managed to put the pieces together into this simple test program.

    I first import System.IO.Ports

    then I define a new serial port and give it the required parameters

    I open the com port when the form loads

    I have two working buttons, one to pan left, one to stop panning. The hex commands are pre-configured.

    The one for Stop is A0 00 00 00 00 00 AF 0F

    The one for Pan Left is A0 00 00 04 14 00 AF 1F

    I declare a byte array to contain the command bytes and put them into the correct position.

    Then, I call the Write method, passing the byte array, starting position in the array, and the number of elements to send.

    Obviously, a working program will be much more complex, but I wanted to put out a dead simple program to get other folks past the hump I just got over.

     

    Code below:

    Imports System.IO.Ports
    Public Class Form1
    
      Dim ComPort As New SerialPort("COM1", 4800, Parity.None, 8, 1)

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComPort.Open() End Sub Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click Dim stopArray(8) As Byte stopArray(1) = &HA0 stopArray(2) = &H0 stopArray(3) = &H0 stopArray(4) = &H0 stopArray(5) = &H0 stopArray(6) = &H0 stopArray(7) = &HAF stopArray(8) = &HF ComPort.Write(stopArray, 1, 8) End Sub Private Sub cmdPanLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPanLeft.Click Dim PanLeftArray(8) As Byte PanLeftArray(1) = &HA0 PanLeftArray(2) = &H0 PanLeftArray(3) = &H0 PanLeftArray(4) = &H4 PanLeftArray(5) = &H14 PanLeftArray(6) = &H0 PanLeftArray(7) = &HAF PanLeftArray(8) = &H1F ComPort.Write(PanLeftArray, 1, 8) End Sub End Class

     

     

     

    • Changed type JohnCEinTexas Tuesday, July 12, 2011 12:51 PM not a question
    Wednesday, July 06, 2011 9:40 PM

All replies

  • Hi JohnCEinTexas,

    Thanks for your sharing. Based on my understanding, you share a simple example of sending hex data to a serial port without asking questions. Is it right? If so, could you please change this question to discussion? Thanks for your understanding.

    If I misunderstood, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 12, 2011 7:55 AM
  • As an alternative to this

     Dim stopArray(8) As Byte
     stopArray(1) = &HA0
     stopArray(2) = &H0
     stopArray(3) = &H0
     stopArray(4) = &H0
     stopArray(5) = &H0
     stopArray(6) = &H0
     stopArray(7) = &HAF
     stopArray(8) = &HF




    try


    Dim stopArray() As Byte = New Byte() {&HA0, &H0, &H0, &H0, &H0, &H0, &HAF, &HF}


    Serial Port      Random      Microsoft® Community Contributor 2011
    Tuesday, July 12, 2011 12:20 PM
  • Done!
    Tuesday, July 12, 2011 12:52 PM
  • That is much cleaner looking - thanks!

     

    One thing, though - the line:

     ComPort.Write(stopArray, 1, 8)

    Would have to be changed to this:

     ComPort.Write(stopArray, 0, 8)

    right?:

    Tuesday, July 12, 2011 12:52 PM
  • It should have been that regardless of how the array was created.  Arrays are zero based in .Net

    If you intended to send all of the bytes then

     

    ComPort.Write(stopArray, 0, stopArray.Length)

    Serial Port      Random      Microsoft® Community Contributor 2011
    Tuesday, July 12, 2011 1:38 PM
  • It should have been that regardless of how the array was created.  Arrays are zero based in .Net

    If you intended to send all of the bytes then

     

    ComPort.Write(stopArray, 0, stopArray.Length)

    Serial Port      Random      Microsoft® Community Contributor 2011

     

    Well, there are times when that is not necessarily the clearest way to write the code.

     

    In this particular case, I am working with a communications protocol for which the documentation uses a 1-based system for describing the structure of the commands to be sent.

    That is to say, the documentation lists a series of values starting with 'Byte 1' up through 'Byte 8' in describing the protocol commands

    Now, if I were to slavishly follow the the .Net convention that all arrays are zero based, I would have to mentally offset the first position value when going from the protocol docs to my program. I would probably make more mistakes that way.

     

    Using a 1 - based array in my program better models the documented protocol than does a zero-based array.

     

     

    Tuesday, July 12, 2011 5:47 PM
  • But anyone who uses your protocol wrapper (other than yourself) would expect 0 based arrays.  You are technically maintaining a zero at the first byte and then ignoring it - this could lead to issues later when you want to work with the list of bytes (e.g. if you pass the array to a method as a parameter, that method will have to deal with the first ignored zero).

    It would be better to maintain the array at the proper length and then offer a "GetByte" method which can accept a 1-based index parameter (which you then decrement by 1 when accessing the underlying array), if you had to.

    But typically when you wrap a protocol like this you create a data structure to hold a meaningful representation of the byte data and provide serialization between the protocol packet contents and the data structure.  You then won't work with the raw array of bytes (other than during the serialization processes) so it doesn't matter that you have to make the mental conversion to 0-based indexing just to write your serialization and deserialization routines.

    At any rate, however you look at it dbasnett is correct.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Tuesday, July 12, 2011 6:18 PM
  • @JohnCEinTexas - is there a link to the documentation for the device that you can provide.  As Reed pointed out most .Net users are comfortable using 0 based arrays.

    Serial Port      Random      Microsoft® Community Contributor 2011
    Tuesday, July 12, 2011 7:52 PM

  • In this particular case, I am working with a communications protocol for which the documentation uses a 1-based system for describing the structure of the commands to be sent.

    That is to say, the documentation lists a series of values starting with 'Byte 1' up through 'Byte 8' in describing the protocol commands

     

    Although the protocol may describe the bytes as 1 through 8, the array in .NET would be bytes 0 through 7. When the protocol says 'the first byte, byte 1' the index in the array of bytes would be zero in the programming world.


    Stephen J Whiteley
    Tuesday, July 12, 2011 8:06 PM

  • In this particular case, I am working with a communications protocol for which the documentation uses a 1-based system for describing the structure of the commands to be sent.

    That is to say, the documentation lists a series of values starting with 'Byte 1' up through 'Byte 8' in describing the protocol commands

     

    Although the protocol may describe the bytes as 1 through 8, the array in .NET would be bytes 0 through 7. When the protocol says 'the first byte, byte 1' the index in the array of bytes would be zero in the programming world.


    Stephen J Whiteley

     

    Which is why we get paid the big bucks ;)


    Serial Port      Random      Microsoft® Community Contributor 2011
    Tuesday, July 12, 2011 8:18 PM
  • @JohnCEinTexas - is there a link to the documentation for the device that you can provide.  As Reed pointed out most .Net users are comfortable using 0 based arrays.

    Serial Port      Random      Microsoft® Community Contributor 2011

    The device is a Toshiba PTZ camera that uses Pelco - P protocol

     

    This link: http://www.codeproject.com/KB/cs/PelcoPDinC.aspx?msg=2293318 Describes the basic commands for the 'P' variant of the Pelco protocol.

    A full description of the 'D' variant is available from Pelco.

    Tuesday, July 12, 2011 9:58 PM
  • Look, I'm willing to concede that there are good and valid reasons for using the zero based index.

     

    My larger point was that when someone like myself needs to find out how to send hex values out through a serial port and needs to know how to do so pretty darn quick, then something like this - http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm - is not going to be all that useful to him.

     

    The above was an example of what I found last week when trying to figure out how to make the quick and simple protocol transmission that I needed.

     

    From searching the forums, I found a lot of folks asking questions about how to send out hex data and running into the problem that the default Serial Write function sends out ASCII and I wanted to save someone else some time and trouble by showing them what I did.

    Tuesday, July 12, 2011 10:10 PM
  •  

    The first overload .Write is string, but as is the case with many of the .Net classes, there are others.  The point is that your code isn't as clear as it could be, and I found it very easy to miss what you were doing.  If you want to create buffers that are 1 byte longer than needed, so 1 = 1, that is up to you.  Just don't be surprised when people look at this and go HUH?

     

    Dim stopArray() As Byte = New Byte() {&H0, &HA0, &H0, &H0, &H0, &H0, &H0, &HAF, &HF}

    ComPort.Write(stopArray, 1, 8)

    Serial Port      Random      Microsoft® Community Contributor 2011
    Wednesday, July 13, 2011 12:03 AM