Question Working with RS232 serial port

bjfguitar

New member
Joined
Jul 8, 2009
Messages
2
Programming Experience
5-10
First of all I want to apologise for only signing up to ask for help, but I've been debugging, experimenting and searching for days for a solution to this problem. Posting is always the last resort.

I'm not very experienced with Visual Basic, although I am proficient in a variety of languages. I got asked to create a program for my fathers product that connects to it to read and write information.

I am using Visual Basic 2008 Express, and to connect I'm using the Serial Port class. I have everything working fine, but the problem is, every time I try to read data from it (with the Read function) the program crashes.

I wrote my own code at first, but through the debugging I decided to just download a pre-made code which I found and modify it to what I need.
VB.NET:
Public Class Form1
    Dim WithEvents serialPort As New IO.Ports.SerialPort

    Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MyBase.Load

        For i As Integer = 0 To _
           My.Computer.Ports.SerialPortNames.Count - 1
            cbbCOMPorts.Items.Add( _
               My.Computer.Ports.SerialPortNames(i))
        Next
        btnDisconnect.Enabled = False
    End Sub

    Private Sub DataReceived( _
       ByVal sender As Object, _
       ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
       Handles serialPort.DataReceived
        Dim rcvBuf(1024) As Byte
        serialPort.Read(rcvBuf, 0, rcvBuf.Length)
        'txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
    End Sub

    Private Sub btnSend_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnSend.Click
        Try
            Dim xmtBuf() As Byte = {&H96, &H1, &H50, &H51, &H74}
            serialPort.Write(xmtBuf, 0, xmtBuf.Length)
            With txtDataReceived
                .SelectionColor = Color.Black
                .AppendText(txtDataToSend.Text & vbCrLf)
                .ScrollToCaret()
            End With
            txtDataToSend.Text = String.Empty
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
        With txtDataReceived
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(serialPort.ReadExisting)
            .ScrollToCaret()
        End With
    End Sub

    Private Sub btnConnect_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnConnect.Click
        If serialPort.IsOpen Then
            serialPort.Close()
        End If
        Try
            With serialPort
                .PortName = cbbCOMPorts.Text
                .BaudRate = 96000
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                ' .Encoding = System.Text.Encoding.Unicode
            End With
            serialPort.Open()

            lblMessage.Text = cbbCOMPorts.Text & " connected."
            btnConnect.Enabled = False
            btnDisconnect.Enabled = True
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

Thankyou to anyone who can help me!
Ben.
 
Hey Robert, thanks for the reply.

I send a hex code:
Dim xmtBuf() As Byte = {&H96, &H1, &H50, &H51, &H74}
serialPort.Write(xmtBuf, 0, xmtBuf.Length)

Which translates out to 96 01 50 51 74

The device I'm writing to reads this function and will respond as a hex as well, which I'm trying to read here:
Dim rcvBuf(1024) As Byte
serialPort.Read(rcvBuf, 0, rcvBuf.Length)

Thanks,
Ben.
 
Does this reply always have a fixed length? If yes, you can set ReceivedBytesThreshold-Property to that value, so that the Received event will only fire if all data has arrived.

Which translates out to 96 01 50 51 74
We are still talking about Hex-Values and not Decimal-Values, do we?

Try This:
VB.NET:
    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
        Dim rcvBuf(Me.serialPort.ReadBufferSize) As Byte
        Dim read As Integer = Int32.MinValue

        read = Me.serialPort.Read(rcvBuf, 0, rcvBuf.Length)

        Dim res As New StringBuilder()

        For i As Integer = 0 To read Step 1
            res.Append(rcvBuf(i).ToString("X2"))
        Next

        Debug.Print(res.ToString())
    End Sub

This should print every received data to the Debug-Window.

Bobby
 

Latest posts

Back
Top