title: Serial Port Send Data in VB.NET with Packed message
body: <p>I have the Manual of Programming protocol,  but when I write code vb.net returns error message.</p>
  
  <p>The following is the table structure of bytes array</p>
  
  <p>position    1 &nbsp;    2 &nbsp;    3 &nbsp;    4 &nbsp;    5 &nbsp;    6 &nbsp;    7&nbsp; 8</p>
  
  <p>Name        SOH &nbsp;  LEN &nbsp;  SEQ &nbsp;  CMD &nbsp;  DATA &nbsp; Post-amble &nbsp;   BCC &nbsp;  ETX</p>
  
  <p>Lenth/bytes 1&nbsp; 1 &nbsp;    1 &nbsp;    1 &nbsp;    0-200 &nbsp;    1 &nbsp;    4 &nbsp;    1</p>
  
  <p>Value   (01h)   (20h-FFh)   (20h-FFh)   (20h-FFh)   (20h-FFh)   (05h)   (30h-3Fh)   (03h)</p>
  
  <p><strong>Abbreviations:</strong> </p>
  
  <p><strong>“SOH”</strong>- (Start Of Heading) start of packed message</p>
  
  <p><strong>“LEN”</strong>- total number of bytes from position 2 to position 6, plus fixed offset of 20h.</p>
  
  <p><strong>“SEQ”</strong>- serial number of packet. SLAVE puts the same “SEQ” in the reply message. In case when SLAVE
  receives a message with the same “SEQ”and “CMD” like the last correctly received message, the device ignores
  the message and repeats the last packet sent to the HOST.</p>
  
  <p><strong>“CMD”</strong> – code of command</p>
  
  <p><strong>“DATA”</strong>- data, according to the command. If there is no data, the length field is zero.</p>
  
  <p><strong>“BCC”</strong> – control sum(0000h-FFFFh). Sum of data bytes from position 2 to position 6. The control sum is
  transferred in ASCІІ type (12АВ is transferred as 31h 32h 3Аh 3Вh).</p>
  
  <p><strong>“ETX”</strong> – (End of TeXt) end of packed message.</p>
  
  <p><strong>Codes sample:</strong></p>
  
  <p>Public Class Form1</p>
  
  <pre><code>Private WithEvents sp As System.IO.Ports.SerialPort
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
      For Each y As String In System.IO.Ports.SerialPort.GetPortNames
          Me.ComboBox1.Items.Add(y)
      Next
      sp = New IO.Ports.SerialPort("COM4")
      sp.StopBits = IO.Ports.StopBits.One
      sp.BaudRate = 9600
      sp.DataBits = 8
      sp.Parity = IO.Ports.Parity.None
      sp.Handshake = IO.Ports.Handshake.None
  
      sp.Open()
      Dim by As Byte = &amp;H20
  
      ' MsgBox(&amp;H20)
  End Sub
  
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Try
          Dim By(9) As Byte
          By(0) = 1
          By(1) = &amp;H2 + &amp;H20
          By(2) = &amp;H58
          By(3) = &amp;H45
          By(4) = &amp;H5
          Dim S As String = Hex(&amp;H58 Xor &amp;H45)
          '  MsgBox(S)
          By(5) = &amp;H30
          By(6) = &amp;H30
          By(7) = &amp;H31
          By(8) = &amp;H3D
          By(9) = &amp;H3
  
          sp.Write(By, 0, By.Length - 1)
         ' MsgBox(sp.ReadByte)
      Catch ex As Exception
          MsgBox(ex.Message)
      End Try
  End Sub
  
  Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
      Dim rec As System.IO.Ports.SerialPort = sender
      MsgBox(Hex(rec.ReadByte))
  End Sub
  </code></pre>
  
  <p>End Class</p>