Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: help me serial port my problem  (Read 3261 times)

0 Members and 1 Guest are viewing this topic.

virus_aboaly

  • Guest
help me serial port my problem
« on: January 29, 2008, 03:09:02 AM »
Can somebody help? Really need help.
I got this sample from internet but there some problem ..
1. when that code run and i`m choose my comport and  received data from serial port that display the data like that 
this is my user interface-------->     http://img292.imageshack.us/img292/8876/uixv7.jpg
and received data  display like this------->  http://img214.imageshack.us/img214/6304/errorfw1.jpg
 
Really need help to show the send data like the recived data
note :::: my recived data like -----> (466798 kg )

...thanks ..


Code: ( vb.net 2005)
Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived
  ' Note this subroutine is executed on the serial port thread - not the UI thread.
  Dim RXByte, Nibble As Byte
  Dim RXArray(2047) As Char
  Dim I As Integer = 0
  Do
    RXByte = COMPort.ReadByte
    Nibble = (RXByte >> 4) + 48          ' Convert received byte to Hex
   
    If Nibble > 57 Then
      Nibble = Nibble + 7
    End If
    RXArray(I) = Chr(Nibble)
    I = I + 1
    Nibble = (RXByte And 15) + 48
    If Nibble > 57 Then
      Nibble = Nibble + 7
    End If
    RXArray(I) = Chr(Nibble)
    I = I + 1
    RXArray(I) = " "
    I = I + 1
    SpaceCount = (SpaceCount + 1) And 31 ' Insert spaces and CRLF for better readability
    If SpaceCount = 0 Then               ' Insert CRLF after 32 numbers
      RXArray(I) = Chr(13) ' CR
      I = I + 1
      RXArray(I) = Chr(10) ' LF
      I = I + 1
    Else
      If (SpaceCount And 3) = 0 Then   ' Insert two extra spaces for each 4 numbers
        RXArray(I) = " "
        I = I + 1
        RXArray(I) = " "
        I = I + 1
      End If
    End If
  Loop Until (COMPort.BytesToRead = 0)
  Dim RxString As New String(RXArray, 0, I) ' Convert the first part of the Char Array to a String
  ' Put a message with a delegate, which points to the display routine and holds the RxString,
  '   on the message queue and return immediately.
  Me.BeginInvoke(New StringSubPointer(AddressOf Display), RxString)
End Sub
 
' Text display routine, which appends the received string to any text in the Received TextBox.
 
Private Sub Display(ByVal Buffer As String)
  Received.AppendText (Buffer)
End Sub
 
' Transmitter subroutine.
 
Private Sub Transmitter(ByVal sender As Object, ByVal e As EventArgs) Handles SendButton.Click
  Received.AppendText ("TX" & vbCrLf)       ' Switch to a new line after every transmission
  SpaceCount = 0
  Dim TextString As String
  Dim TXArray(2047) As Byte
  Dim I As Integer
  Dim J As Integer = 0
  Dim Ascii As Boolean = False
  Dim Quote As Boolean = False
  Dim Temp As Boolean
  Dim Second As Boolean = False
  Dim TXByte As Byte = 0
  Dim CharByte As Byte
  If COMPort.IsOpen Then
    TextString = Transmitted.Text
    For I = 0 To TextString.Length - 1
      CharByte = Asc(TextString.Chars(I))
      If CharByte = 34 Then ' If " Then
        Temp = Ascii
        Ascii = Ascii Or Quote
        Quote = Not (Temp And Quote)
      Else
        Ascii = Ascii Xor Quote
        Quote = False
      End If
      If Not Quote Then
        If Ascii Then
          TXArray(J) = CharByte
          J = J + 1
        Else
          If (CharByte <> 32) And (CharByte <> 10) And (CharByte <> 13) Then ' Skip spaces, LF and CR
            CharByte = (CharByte - 48) And 31 ' And 31 makes it case insensitive
            If CharByte > 16 Then
              CharByte = CharByte - 7
            End If
            If Second Then
              TXArray(J) = TXByte + CharByte
              Second = False
              J = J + 1
            Else
              TXByte = CharByte << 4
              Second = True
            End If
          End If
        End If
      End If
    Next
    Try
      COMPort.Write(TXArray, 0, J)
    Catch ex As Exception
      MsgBox (ex.Message & "  Check CTS signal or set Flow Control to None.")
    End Try
  Else
    thanks again..
    MsgBox ("COM port is closed. Please select a COM port")
  End If
End Sub