-
Jun 13th, 2013, 02:31 PM
#1
Thread Starter
New Member
SerialPort problems, readline() hangs when using win xp (fine on win 7)
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
-
Jun 13th, 2013, 03:29 PM
#2
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
I am actually surprised that it works consistently anywhere. Depending on the timing you could be reading in the event handler OR the button click. The difference might be the framework you are targeting on the two machines.
My advice is to ONLY receive data in the event handler. Take the data received and store it in a queue or list. Process it elsewhere, like a timer or separate thread.
-
Jun 13th, 2013, 04:05 PM
#3
Thread Starter
New Member
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
HI Thanks for the reply
I will certainly think about how I can change things to avoid the uncertainty of which event gets the port data.
I had another thought about using a boolean flag set by the button event to prevent the data being read in the datareceived event, (a simple if button pressed statement) - the datarecieved event would still occur but having not read anything the serial data would still be preserved ready for readline to be performed.
Code:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
if buttonPressed = false then ‘Automatically called every time a data is received at the serialPort
ReceivedText(SerialPort1.ReadExisting()) 'Port read only if button was not being processed
end if
End Sub
Would this work?.......
Bally
-
Jun 13th, 2013, 05:20 PM
#4
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
Why have the event handler at all?
-
Jun 14th, 2013, 08:21 AM
#5
Thread Starter
New Member
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
After more head pounding I am left very confused.
I dropped the serial receiving event and used just readline and write methods triggered simply by button presses. Still i get it hanging at readline.
For example:
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
txtResponce.Clear()
ClearBuffer = SerialPort1.ReadExisting 'just in case clear any previous data
SerialPort1.Write("s")
responce = SerialPort1.ReadLine 'expect to read back the char "E" in reponce
txtResponce.Text &= responce
End Sub
There are now several other buttons to send out the other parts of the sequence, all following the code structure above. The following sequence of buttons pressed in turn would give: "1" , "1000" + vbcr , "500" + vbcr and finally "100" + vbcr. Completing the output sequence of command parameters.
Now here's the thing that that's really killing me. If I use a win 7 machine the app runs fine. If I use hyperterminal to send out the sequence above that too works fine. As soon as I go near XP it fails......
Not sure what to do now, but I am going to be avoiding high places for the foreseeable future
Any help, suggestions, or debugging ideas will be hugely appreciated....
-
Jun 14th, 2013, 09:16 AM
#6
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
What version of .Net are you using? Does hyperterminal work on XP?
Last edited by dbasnett; Jun 14th, 2013 at 09:19 AM.
-
Jun 14th, 2013, 09:21 AM
#7
Thread Starter
New Member
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
Taken from VB2010 about menu:
Microsoft Visual Studio 2010
Version 10.0.30319.1 RTMRel
Microsoft .NET Framework
Version 4.0.30319 RTMRel
Installed Version: VB Express
Microsoft Visual Basic 2010 01012-169-2510015-70468
Microsoft Visual Basic 2010
Security Update for Microsoft Visual Basic 2010 Express - ENU (KB2251489) KB2251489
This security update is for Microsoft Visual Basic 2010 Express - ENU.
If you later install a more recent service pack, this security update will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/2251489.
Thanks Andy
-
Jun 14th, 2013, 10:15 AM
#8
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
Does hyperterminal work on XP?
An example using timer. Note that there is only one SerialPort read of any kind.
Code:
Public Class Form1
Dim WithEvents timer1 As New System.Windows.Forms.Timer
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'port setup and open
Dim p() As String = IO.Ports.SerialPort.GetPortNames
With SerialPort1
.PortName = p(1)
.BaudRate = 9600
.Encoding = System.Text.Encoding.GetEncoding(28591)
.DtrEnable = True
End With
nl = CByte(Asc(SerialPort1.NewLine))
Try
SerialPort1.Open()
timer1.Interval = 50
timer1.Start()
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Dim nl As Byte
Dim dataByts As New List(Of Byte)
Dim dataLock As New Object
Dim setup As Boolean = False
Dim datarcvd As New Threading.AutoResetEvent(False)
Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
If datarcvd.WaitOne(0) Then 'do we have bytes to process
If dataByts.Count > 0 Then
Dim s As String = ""
Threading.Monitor.Enter(dataLock) 'yes
Dim idxOfNL As Integer = dataByts.IndexOf(nl) 'does the list have a newline
If idxOfNL >= 0 Then
idxOfNL += 1 'include the newline
'get the string, including the newline character
s = SerialPort1.Encoding.GetChars(dataByts.ToArray, 0, idxOfNL)
dataByts.RemoveRange(0, idxOfNL) 'remove the bytes processed
End If
Threading.Monitor.Exit(dataLock)
RichTextBox1.AppendText(s)
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
SerialPort1.WriteLine("s")
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
Private Sub SerialPort1_DataReceived(sender As Object, _
e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim br As Integer = SerialPort1.BytesToRead '# of bytes to read
If br > 0 Then
Dim b(br - 1) As Byte 'create buffer to read into
Try
br = SerialPort1.Read(b, 0, b.Length) 'read the bytes
If br < b.Length Then 'adjust length if required
Array.Resize(b, br)
End If
'add bytes just read to list
Threading.Monitor.Enter(dataLock)
dataByts.AddRange(b)
Threading.Monitor.Exit(dataLock)
datarcvd.Set() 'signal event fired
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End If
End Sub
Private Sub SerialPort1_ErrorReceived(sender As Object, _
e As IO.Ports.SerialErrorReceivedEventArgs) _
Handles SerialPort1.ErrorReceived
Debug.WriteLine(e.EventType)
End Sub
Private Sub SerialPort1_PinChanged(sender As Object, _
e As IO.Ports.SerialPinChangedEventArgs) _
Handles SerialPort1.PinChanged
Debug.WriteLine(e.EventType)
End Sub
End Class
Last edited by dbasnett; Jun 14th, 2013 at 10:20 AM.
-
Jun 14th, 2013, 10:41 AM
#9
Thread Starter
New Member
Re: SerialPort problems, readline() hangs when using win xp (fine on win 7)
Hi
Yes Hyperterminal works fine on xp.
Thanks for the code I will check your timer based example sometime over the weekend as right now my heads kind of fried!! - thanks.
Bally
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|