Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 132,365 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,289 people online right now. Registration is fast and FREE... Join Now!




Serial Port Communication Problem

 
Reply to this topicStart new topic

Serial Port Communication Problem, Problems properly reading the sent text

Prog19
post 21 Aug, 2008 - 05:37 AM
Post #1


New D.I.C Head

*
Joined: 28 Jul, 2008
Posts: 4

Hi. If someone could please help me on this problem. ..

My problem is, I have this large program that is supposed to receive command alerts such as "start" or "stop" from another machine. I have a serial port chat set up that can receive/send text back and forth via a RS232. That seems to be working ok at first, except when I keep it running and practice typing back and forth, I for some reason start getting spaces in the communication log so it will look something like the following:

start
stop
start


stop
stop
stop
start

stop
start

There is no specific pattern to the spaces. That is problem number one. The second problem, the more important problem, is that I need to somehow read the text that is being sent into the text box, just one line at a time, I do not want it to read the entire box, so that I can send the text to a subroutine that can verify which command to do. Ex. If "Stop" is sent, please do this, or if "start" is sent, please tell my program to do this. I am once again getting problems in that I will type "start" yet for some reason, the program only sees "star" in one text box then a "t" in another text box. Or some other various division of the word among the message boxes. I really do not understand why this is happening. Here is my code for the chat and the command I would like to use. At the moment I am just trying to verify all this works with message boxes, which is why in my code you will see all the different message box lines.

CODE

'Load Communication Ports

        For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
            cboCommPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))
        Next
        cmdDisconnect.Enabled = False

'Connect to comm port
    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If
        Try
            With SerialPort1
                .PortName = cboCommPorts.Text
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
            End With
            SerialPort1.Open()

            lblMessage.Text = SerialPort1.PortName & " connected"
            lblConnection.Text = lblMessage.Text
            cmdDisconnect.Enabled = True
            cmdConnect.Enabled = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    'Disconnect from comm port
    Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click
        Try
            SerialPort1.Close()
            lblMessage.Text = SerialPort1.PortName & " disconnected"
            lblConnection.Text = lblMessage.Text
            cmdDisconnect.Enabled = False
            cmdConnect.Enabled = True
        Catch ex As Exception

        End Try
    End Sub

    'Send message to selected comm port
    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Try
            SerialPort1.Write(txtTextToSend.Text & vbCrLf)
            With txtDataReceived
                .SelectionColor = Color.Black
                .AppendText(txtTextToSend.Text & vbCrLf)
                .ScrollToCaret()
            End With

            txtTextToSend.Text = String.Empty
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    'Update text box with data recieved from comm port
    Public Sub updateTextBox()
        With txtDataReceived
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(SerialPort1.ReadExisting)
            .ScrollToCaret()
        End With

        MessageBox.Show(txtDataReceived.Text)
        Dim CheckText As String = txtDataReceived.Text
        Call checkStartStop(CheckText)
        txtDataReceived.Text = ""

    End Sub

    'Purpose:  Receive data
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {}) ', New Object()

    End Sub

Private Sub checkStartStop(ByVal CheckText)

        If CheckText = "start" Then
            MessageBox.Show("Start")
        ElseIf CheckText = "stop" Then
            MessageBox.Show("stop")
        Else
            MessageBox.Show("No Alerts")
        End If
    End Sub


Please , any advice on this. I am really stuck.

Thank you.

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 05:04AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month