Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am sending commands to an instrument via a serial port and getting back the data however
when I run the code the commands are sent and the reply is got (to a rich text box). How ever as the serial port is piped to the rtb via an invoke command not all the details are there every time if I run it through the debugger the commands work, the data is got, however if I run the .exe from the desktop not all the data is there and it falls over. I think it might be due to the system running quicker with out the debugger attachment. I need a way of slowing the threads not using a thread sleep (which takes the thread out!), I have tried using a Thread Sleep which works all the time in the debugger and only some times in the exe.

Sorry got lost in something else clicked post before reading properly

VB
'MsgBox(rtbIncoming.Text)
     Data_Read = rtbIncoming.Text
     MsgBox("Data :" + Data_Read)

The actual routine that does the reading is here:
VB
Private Sub port_DataReceived_1(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

       NoDataAtPort.Enabled = False
       InputData = myComPort.ReadExisting
       Reply_Status = REPLY.YES_REPLY

       If InputData <> [String].Empty Then
           Me.Invoke(New SetTextCallback(AddressOf SetText), New Object() {InputData})
           '    MsgBox("here!")
           DataReceived = True
       Else
           MsgBox("null")
       End If

       TmrNoDataAtPort.Enabled = False
       If (Reply_Status = REPLY.TIMEOUT_REPLY) Then
           Data_Back = "TIMEOUT"
       ElseIf (Reply_Status = REPLY.YES_REPLY) Then
           NoDataAtPort.Enabled = False
       End If

       If (DataReceived = True) Then Me.Invoke(New PutInRightTextBox(AddressOf DecphierCommand), New Object() {InputData})

   End Sub


DecphierCommand() is just a case statement to split the rtbIncoming data up.


Sorry about that got caught with something had to do it...

Glenn
Posted
Updated 17-Feb-14 23:57pm
v2
Comments
[no name] 18-Feb-14 5:45am    
Posting your code will help. As a general rule sleeps are needed to adapt to design flaws and the sleep interval will always be wrong.

1 solution

The way I do it is to have a separate "processing" thread which converts "raw" serial data into complete "messages" and then passes them to the main code for actual analysis.
So if the data is STX data EXT then a checksum, the processing thread will keep the "data so far" starting at the STX until it finds the ETX, grab the next as the checksum, validate it, and either discard or pass the whole message on up.

Adding Sleep and so forth is generally a mistake - it makes it very hard to be sure it works under all circumstances and it's far too easy to have a problem appear in six months time when something external has changed - perhaps a heavy processing task was added or removed from the PC running your app as well, or the PC was upgraded with more RAM.

I don't know what your data looks like, but mostly there is some kind of start and or end message sequence, even if it's only "it ends with newline".
 
Share this answer
 
Comments
glennPattonWork3 18-Feb-14 6:07am    
OK I'm guessing that the nice foreach() I use in C# does not exist in VB...(mutter, mutter, mutter) foreach (string subString in TextReply.Split(delimiters))
OriginalGriff 18-Feb-14 6:10am    
Um...yes it does: http://msdn.microsoft.com/en-us/library/5ebk1751.aspx
For Each name As String In listOfNames
Console.WriteLine(name)
Next
OriginalGriff 18-Feb-14 6:11am    
Sorry - didn't see your edit...

For Each subString As String In TextReply.Split(delimiters)
Console.WriteLine(subString)
Next

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900