title: Read all data from serial COM (electronic scale) VB.NET
body: <p>I need to read some data from electronic scale connected to COM3</p>
  
  <p>I found a vb.net COM read/write exemple there: 
  <a href="http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm" rel="nofollow">http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm</a>
  direct link for source : <a href="http://www.innovatic.dk/knowledg/SerialCOM/MaxiCOM.zip" rel="nofollow">http://www.innovatic.dk/knowledg/SerialCOM/MaxiCOM.zip</a></p>
  
  <p>this is my code :</p>
  
  <pre><code> Dim SpaceCount As Byte = 0 
      Dim LookUpTable As String = "0123456789ABCDEF" 
      Dim RXArray(2047) As Char ' Text buffer. Must be global to be accessible from more threads. 
      Dim RXCnt As Integer      ' Length of text buffer. Must be global too. 
  
      ' Make a new System.IO.Ports.SerialPort instance, which is able to fire events. 
      Dim WithEvents COMPort As New System.IO.Ports.SerialPort("COM3", _ 
                                     9600, _ 
                                     Parity.None, _ 
                                     8, _ 
                                     StopBits.One) 
  
        ' Transmitter subroutine. 
      Private Sub Transmitter(ByVal sender As Object, ByVal e As EventArgs) Handles SendButton.Click 
          COMPort.Open() 
          If COMPort.IsOpen Then 
              Try 
                  COMPort.Write(New Byte() {&amp;H5}, 0, 1) 
                  COMPort.Write(New Byte() {&amp;H11}, 0, 1) 
                  '0x05 0x11 
              Catch ex As Exception 
                  MsgBox(ex.Message &amp; "  Check CTS signal or set Flow Control to None.") 
              End Try 
  
          Else 
              MsgBox("COM port is closed. Please select a COM port") 
          End If 
  
      End Sub 
  
      Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived 
          Dim RXByte As Byte 
          Do 
              '----- Start of communication protocol handling ----------------------------------------------------------- 
              ' The code between the two lines does the communication protocol. In this case, it simply emties the 
              '   receive buffer and converts it to text, but for all practical applications, you must replace this part
              '     with a code, which can collect one entire telegram by searching for the telegram 
              '       delimiter/termination. In case of a simple ASCII protocol, you may just use ReadLine and receive 
              '         in a global string instead of a byte array. 
              ' Because this routine runs on a thread pool thread, it does not block the UI, so if you have any data 
              '   convertion, encryption, expansion, error detection, error correction etc. to do, do it here. 
              RXCnt = 0 
              Do 
                  RXByte = COMPort.ReadByte 
                  RXArray(RXCnt) = LookUpTable(RXByte &gt;&gt; 4) ' Convert each byte to two hexadecimal characters 
                  RXCnt = RXCnt + 1 
                  RXArray(RXCnt) = LookUpTable(RXByte And 15) 
                  RXCnt = RXCnt + 1 
                  RXArray(RXCnt) = " " 
                  RXCnt = RXCnt + 1 
                  SpaceCount = (SpaceCount + 1) And 31      ' Insert spaces and CRLF for better readability 
                  If SpaceCount = 0 Then                    ' Insert CRLF after 32 numbers 
                      RXArray(RXCnt) = Chr(13) ' CR 
                      RXCnt = RXCnt + 1 
                      RXArray(RXCnt) = Chr(10) ' LF 
                      RXCnt = RXCnt + 1 
                  Else 
                      If (SpaceCount And 3) = 0 Then        ' Insert two extra spaces for each 4 numbers 
                          RXArray(RXCnt) = " " 
                          RXCnt = RXCnt + 1 
                          RXArray(RXCnt) = " " 
                          RXCnt = RXCnt + 1 
                      End If 
                  End If 
              Loop Until (COMPort.BytesToRead = 0) 
              '----- End of communication protocol handling ------------------------------------------------------------- 
              Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread 
  
          Loop Until (COMPort.BytesToRead = 0)  ' Don't return if more bytes have become available in the meantime 
  
  
      End Sub 
  
      ' Text display routine, which appends the received string to any text in the Received TextBox. 
      Private Sub Display() 
          Received.AppendText(New String(RXArray, 0, RXCnt)) 
      End Sub  
  </code></pre>
  
  <p>....................</p>
  
  <p>My problem is:
  I send to electronic scale 2 commands (ENQ) 0x05 and DC1 (0x11) to get some results,after that sub Receiver is called automatically multiple times and gets data back pieces by pieces.Because of that I don't know when all data is received to process the results</p>
  
  <p>I've tried to close the port at the and of sub Transmitter but then sub Receiver gives me error for port closed.</p>
  
  <p>I need some help,thanks!</p>
  
  <p>EDIT:</p>
  
  <p>Hi Hans,I've tried this code :</p>
  
  <p>Dim Str As String = ""
          Dim com1 As New System.IO.Ports.SerialPort("COM3", _
                                    9600, _
                                    Parity.None, _
                                    8, _
                                    StopBits.One)</p>
  
  <pre><code>    Try
            com1.ReadTimeout = 10000
          com1.Open()
  
          com1.Write(New Byte() {&amp;H5}, 0, 1)
          com1.Write(New Byte() {&amp;H11}, 0, 1)
          Do
              Dim Incoming As String = com1.ReadLine
              If Incoming Is Nothing Then
                  Exit Do
              Else
                  Str &amp;= Incoming &amp; vbCrLf
                  RichTextBox.Text = Str
              End If
          Loop
      Catch ex As TimeoutException
          MessageBox.Show(ex.Message)
      Finally
          If com1 IsNot Nothing Then com1.Close()
      End Try
  End Sub
  </code></pre>
  
  <p>but I always got :Timeout connection, with code from post#1 I don't got this, thanks!</p>