Question Serial timers problem...

softhard

Active member
Joined
Sep 29, 2012
Messages
35
Programming Experience
Beginner
Hello,
i am using serial protocol code which is working on based on the timers. A timer for every 100ms check the avaliable bytes and read them into program. The problem is sometimes i lost the data, half of the packet received or my receiving packet is cut down to two parts. How can i control this receiving event in good way. My every receiving packet starts with <7E>. see th below image.
 

Attachments

  • packets.jpg
    packets.jpg
    40.7 KB · Views: 42
Hi,

If you need to continue using this forum to solve your problems, which we are happy to try and do by the way, then a few Thank You's maybe in order to the numerous people who have already helped you with your numerous issues.

You will find that a little respect goes a long, long way young programmer but it's your choice.

Ian
 
Hello,
I am not asking for your help without trying in my limited knowledge VB.Net. I have been trying and could not able to find way to go on. I no need direct answers from you and that never going to teach me nothing. Your interpretation of me is wrong and nevertheless try to give me any hint then i could finish it my self.
 
Hello,
Plese some one help me about this problem and i have been trying restless.
I receive the serial data with help of a timer with 100ms time gap. I am sure that because of this timer sometimes i lost some part of data or data cut down to two lines. I would like to remove this timer and want try it in other way but don't know the way to choose.
Here are two more pictures i attached taken two PC's at the same time and see the received the data for your reference.

Hoping for any help.
 

Attachments

  • namnlös.JPG
    namnlös.JPG
    68.5 KB · Views: 32
  • Untitled.jpg
    Untitled.jpg
    118.5 KB · Views: 34
Public Sub tmrReadTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadTimer.Tick
Dim NewStr As String = ""
Dim SpecialWord As Integer = 0
Dim FunctionCode As Integer = 0
Dim FoundGroup As Integer = 0
Dim FoundNodeAddr As Long = 0
Dim FoundProductID As Integer = 0
Dim FoundProductVer As Integer = 0
Try
If WPGport > 0 Then
If KommPort.IsOpen Then
Try
If KommPort.BytesToRead > 0 Or SimulatePacket Then
SimulatePacket = False
Dim Data As String
Data = KommPort.ReadExisting
Buffert &= Data
'Reading into buffer
AntalTeckenIBuffert += Len(Data) 'length of the data
RaiseEvent OnDataReceive(Buffert)
If Buffert.Length > 4 Then
'check if protocolID = 0 / MBZ package
If ProtocolID(Buffert) = 0 Then
' MBZ
NewStr = GetMBZpayload(Buffert)
SpecialWord = Asc(Mid(NewStr, 1, 1))
SpecialWord *= 256
SpecialWord += Asc(Mid(NewStr, 2, 1))
FunctionCode = Asc(Mid(NewStr, 3, 1))
NewStr = Mid(NewStr, 4, Len(NewStr) - 3)
If Mid(Buffert, 1, 3) = Chr(&H7E) & Chr(0) & Chr(&H7E) Then
Buffert = Mid(Buffert, 3, Len(Buffert) - 2)
End If
If CRC16Str2(Mid(Buffert, 2, Len(Buffert) - 3)) = Mid(Buffert, Len(Buffert) - 1, 2) Then
RaiseEvent OnMBZpacketReceive(InterpretMBZpacket(Buffert, SpecialWord, FunctionCode, NewStr))
End If
ElseIf ProtocolID(Buffert) = 3 Then
' NDP
NewStr = GetMBZpayload(Buffert)
If Asc(Mid(NewStr, 1, 1)) = 1 Then
'Pong
FoundNodeAddr = GetZCPdata(Buffert).Source
FoundGroup = Asc(Mid(NewStr, 2, 1))
FoundProductID = Asc(Mid(NewStr, 3, 1))
FoundProductVer = Asc(Mid(NewStr, 4, 1))
RaiseEvent OnPongReceive(FoundGroup, FoundNodeAddr, FoundProductID, FoundProductVer)
End If
ElseIf ProtocolID(Buffert) = 1 Then
Dim hej = 1
End If
End If
End If
Buffert = ""
AntalTeckenIBuffert = 0
Catch ex As Exception
End Try
Else
WPGport = 0
End If
End If
Catch
End Try
End Sub
 
There is no problem with reception of data. Problem is only with visualization ón the GUI. You can see in the aboue pictures with red at 10-08 10:38:19 and next line to it, in black image at 10/08/2012 10:40:47AM.
In the red image the same data is cut down to two lines and printed the whole packet second time only see under the blackline in image at 10-08 10:38:19. This has taken from one PC.
In balck image the same data is showned pretty good and no problems with that at all. See at 10/08/2012 10:40:47AM the rounded one and displayed only once. This has taken from another PC.

I can say this is the only one example that i can show you now but i have been watching many like this at daily work.
Why is it so? I also tried using the function Readline() but always getting these kind of broken down visualization at least 5% of whole reading data. I srongly guess this due to timer but i am not sure. The timer value is 100ms.
 
Hi,
Thanks for the further explanation. I have had a quick look at the code now and there are lots of calls to external functions which I have no access to and therefore I do not think I could track down the issue nor help.

I will keep looking and come back to you if I find anything useful that I can comment on to help you.

Cheers,

Ian
 
Yes, here is the another function used to print data on the terminal and this gets the data from the given timer function before.
Ok i will wait for your comment and i do search my self in mean time.

PrivateSub OnDataReceive(ByVal DataString AsString)
Dim Buff AsString = ""
Dim DataStr AsString = ""
Dim productID AsLong = 0
Dim theDate AsDateTime
Dim productType AsString
theDate = DateTime.Now
Buff = DataString
For I = 1 To Buff.Length
DataStr &= (
"<" & Hex(Asc(Mid("" & Buff, I, 1))) & "> ")
Next
RichTextBox1.SelectionFont = NewFont("Verdana", 9, FontStyle.Regular)
RichTextBox1.SelectionColor =
Color.Red
RichTextBox1.AppendText(theDate &
":- " & DataStr & Environment.NewLine)
productType = productIDToProductType(productID)
EndSub
 
More visualization errors on my serial terminal....

See this image
Error1) two packets append each other continuously
2) packet break down to two lines
3) Some data missing at last in the receiving packet...
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    145 KB · Views: 33
Hi,

I have had a good look and think about this now and even though I do not have an answer for you here are a few ideas for you to look at and try out:-

1) You have implemented two error capture routines in the timer using Try/Catch which is good. However, what is not good, is the fact that you have not done anything to alert yourself in any of the catch statements if an exception occurs. The program can therefore be running with hundreds of errors and you are never going to know about it.

Let's fix this first then by adding something to the Catch statements to deal with any unknown errors. I suggest adding a textbox to your form with multiline set to true and having a vertical scroll bar. In the two Catch statements add the following code:-

VB.NET:
Try
.....
Catch ex As Exception
  With TextBox1
   .Text += "Data Read Error: " & ex.Message & vbCrLf & vbCrLf
   .BackColor = Color.Red
  End With
End Try
2) As mentioned before there are a lot of external function calls after the data is received that do something with the data so it's difficult to narrow down if the code is at fault. My gut feeling however seems to tell me that the code is correct since you get correct data 95% of the time.

I think that you therefore need to concentrate your efforts on the actual data that is being received from the serial port each time the timer is used. You have already mentioned that you think it maybe the timer that is causing the issue and I think I agree since by using a timer to interrogate the serial port you could very well pick up an incomplete transmitted package.

I think you should test this theory by adding a list to the timer (declare the list private within the form and not the timer so that you do not keep overwriting it) then each time the timer fires add the RAW data, not the data after your code processing, to the list. i.e.:-

VB.NET:
Public Class Form1
  Dim myRawDataList As New List(Of String)
.....
.....
 
Data = KommPort.ReadExisting
myRawDataList.Add(Data)
Buffert &= Data 'Reading into buffer
The next time that you then notice the GUI showing your invalid data you then need to interrogate the Raw data saved in the list to determine if it is the raw data from the serial port which is incorrect and not the code. You can get your raw data by adding a textbox and a button to the form and adding the following code to the button:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  For Each strRaw As String In myRawDataList
    TextBox2.Text += strRaw & vbCrLf
  Next
  myRawDataList.Clear()
End Sub
By comparing the two you should at least be able to rule out whether it is the serial port or the code that is the source of the error.

3) Once the above is done and you have your answer you then need to decide where to go next:-

3.1) If it is a serial port error then we can assume that it is the timer causing the issue and I therefore suggest that you need to change the code to interrogate the serial port on the SerialPort.DataReceived Event. Have a look here:-

SerialPort.DataReceived Event (System.IO.Ports)

3.2) If it is not a serial port error then I would suggest that you use the List capture idea we used with the serial port to capture the results of each manipulation of the Buffert variable so that you can again interrogate the result of each routine and therefore narrow down where you need to concentrate your efforts. We can then look into each routine in more depth as we need to.

4) Just as an additional afterthought you test for data on the serial port using If KommPort.BytesToRead > 0. If there is no data then you set Buffert ="" which effectively resets your buffer waiting for your next packet. Sounds good in theory but what if there is a tiny delay on the serial port, being anything more than 100ms, it will rest buffert and in theory give you the truncated results you have seen. On the basis that you receive data packets every few seconds why not try slowing down the timer to overcome any delay on the serial port. Say 300ms?

There is plenty here for you to try and good luck.

Let us know how you get on.

Cheers,

Ian
 
After our discussion, i tried to remove the timers in my program and rebuilded with datareceive event. I am successfully reading all my data with this event. But i strucked parsing the data in a good order. Still, i am not succesfull in displaying data line by line as i stated before. you can see the below image for reference. In that picture, A full packet is cut down to two or three packets and displayed at certain times and some times only time and date is separated from the display. Though, the received time same for all the separated packets(see time stamps infront of the received data). My each new packet starts with symbol "~" and i want to use this key to parse all the respective data in line by line. I have no idea how can i acheive this since i am new to VB.net.

Here is code snippet where i am displaying the data:
For I = 1 To Buff.Length
DataStr &= (
"<" & Hex(Asc(Mid("" & Buff, I, 1))) & ">")
Next
TextBox1.AppendText(theDate & ":- " & DataStr & Environment.NewLine)


How can i display the data line by line, I would like to store all the receiving data into buffer and make a search on the buffer for the positions of string "~" and then cut down them into new meaningful lines and last flush the buffer after once printing and repeat the same.

Any help this time!
 

Attachments

  • namnlös.JPG
    namnlös.JPG
    86.7 KB · Views: 30
Hi,

Some of the experts on this site may be able to help you but from a personal point of view I will try and explain this to you one more time. This being the last time I do this.

I am a very successful programmer and more importantly a very successful business man so what you need to realise is that I am not your employee and I do not work for you, but I am happy to try and help you if I can.

All you need to do is supply the magic words for the numerous attempts of trying to help you in the past and then maybe you might get another answer from me.

By the way the magic words have nothing to do with Abra-Ka-Dabra!

It's up to you.

Kind regards,

Ian
 
Back
Top