Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27376

SerialPort problems, readline() hangs when using win xp (fine on win 7)

$
0
0
I am very new to VB.net so please be understanding!!

I have based an application around some examples from the net. Basically I have a microprocessor connected to a serial port. The Micro does 2 main things, firstly it issues an identification string when powered on, then it waits for a sequence of parameters to configure it, following this it issues other status messages during later operation. My VB form has a rtb that displays the string/s captured on the serial port and a button that sends the sequence of parameters. To capture the identification (which can happen at anytime) I used this code to trigger a read event on any data.

Code:

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())    ‘Automatically called every time a data is received at the serialPort
End Sub

Private Sub ReceivedText(ByVal [text] As String)
‘compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub


Later on when a button is pressed I used this code to send a parameter and wait for a confirmation string back from the micro:


Code:

Private Sub btnSendParameters_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click

        Dim response As String

        Try

            SerialPort1.WriteLine(command)
            response = SerialPort1.ReadLine


        Catch ex As TimeoutException
            MessageBox.Show(ex.Message)

        Catch ex As InvalidOperationException
            MessageBox.Show(ex.Message)

        Catch ex As UnauthorizedAccessException
            MessageBox.Show(ex.Message)

        End Try
    End Sub

Now this all works fine when compiled on a win7 machine. However when I deployed to an XP machine the application hangs at a readline() method. I have then tried to debug and rebuild it using vb.net express on the xp machine and found:

If I comment out the line
Code:

ReceivedText(SerialPort1.ReadExisting())    ‘Automatically called every time a data is received at the serialPort
it works but obviously stops any randomly arriving messages on the port. I think the line above is intercepting the response string before readline gets to see it. Why it works on win 7 and not xp is confusing!!

So please if anyone can help me to understand what I am missing here I would be greatly appreciated - I 've been on it for days now, although I now see where the problem lies I am not sure how to about solving it.

Thanks

Bally

Viewing all articles
Browse latest Browse all 27376

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>