-
Mar 2nd, 2018, 08:05 AM
#1
Thread Starter
New Member
Serial Port not receiving full data
I am currently workng on a peoject that requires serial communication between PIC 24FV16KA302 and a PC software.
I have searched the internet for the past 3 days and i cant find an answer for my proble so i decidet to ask here . This is my first visual studio program so i dont have any expirience with the software.
The PIC has few variables and two 8x16 tables that i need to view and modify on the PC side . The problem comes when i send the tables , all other information is received without a problem . I am using serial connection ( 38400/8-N-1 ) via uart to usb converter FT232
When the PC send "AT+RTPM" to the PIC .
Code:
Button7.Click
SerialPort1.ReceivedBytesThreshold = 128
MachineState = MS.Receive_table
SerialPort1.Write("AT+RTPM")
End Sub
The PIC sends back 128 Bytes( the values in the table )
Code:
case read_table_pwm : // send pwm table
for (yy = 0 ; yy < 8 ; yy ++) {
for (xx = 0 ; xx < 16 ; xx++ ) {
uart_send_char(controll_by_pmw_map_lb[yy][xx]) ;
}
}
at_command = receive_state_idle ;
break ;
Which the software is suppose to get and display in a DataGrid.
Code:
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
If MachineState = MS.Receive_table Then
SerialPort1.Read(Buffer_array_received_data, 0, 128)
cellpos = 0
For grid_y As Int16 = 0 To 7 Step 1
For grid_x As Int16 = 0 To 15 Step 1
DataGridView1.Rows(grid_y).Cells(grid_x).Value = Buffer_array_received_data(cellpos)
cellpos += 1
Next
Next
End Sub
The problem is that most of the time ( 99 % ) it displays only part of the dataset and zeros to the end , and when i try to do it again it display the other part and it starts from the begining .
First Try:
Second Try:
If i try the same thing with another program i allways get the full dataset
I have tried doing it cell by cell , but it only wors if i request them one very second , other wise i get the same problem .
After that i need to use a timer ( 100 ms ) to request live data from the PIC . this work better but still some of the time i get some random data. I havent focused on that for the moment because without the dataset everything else is useless .
Am i missing something , has anyone encountered the same problem .
-
Mar 2nd, 2018, 11:00 AM
#2
Re: Serial Port not receiving full data
The serial port read method will read up to the count. If fewer bytes are available that is what is returned. See the documentation for further information.
https://msdn.microsoft.com/en-us/lib...or=-2147217396
-
Mar 2nd, 2018, 11:05 AM
#3
Addicted Member
Re: Serial Port not receiving full data
SerialPort1.Read will not wait for the full 128 bytes as you have it. You could perhaps read a byte at a time and fill your array that way.To test this you could try the following, create a Form with 1 RichTextbox and 1 SeriaPort and configure the port so it opens when the Form is loaded. Next add the following code snippets which add a delegate for the RichTextbox to display the data and a loop to read the port.
Delegate:
Code:
Public Delegate Sub myDelegate(ByVal sData As Byte)
Private Sub Text_Out(ByVal sData As Byte)
RichTextBox1.AppendText(Hex(sData.ToString) & " ")
End Sub
Loop:
Code:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim sData As Byte
SerialPort1.ReadTimeout = 20
Do
Try
sData = CByte(SerialPort1.ReadByte)
Me.BeginInvoke((New myDelegate(AddressOf Text_Out)), sData)
Catch ex As Exception
Exit Do
End Try
Loop
-
Mar 3rd, 2018, 08:58 AM
#4
Re: Serial Port not receiving full data
If you know exactly the number of bytes you should receive, you can set the SerialPort.ReceivedBytesThreshold Property which determines when the DataReceived event fires based on how many bytes are in the RX buffer.
If you use this property, make sure to also use an appropriate SerialPort.ReadTimeout value that will allow you to recover from corrupt data packets.
kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Mar 3rd, 2018, 12:47 PM
#5
Thread Starter
New Member
Re: Serial Port not receiving full data
Hi,
I have managed to solve the problem by replacing :
Code:
SerialPort1.Read(Buffer_array_received_data, 0, 128)
with :
Code:
For byte_pos As Int16 = 0 To 127 Step 1
Buffer_array_received_data(byte_pos) = SerialPort1.ReadByte()
Next
If you know exactly the number of bytes you should receive, you can set the SerialPort.ReceivedBytesThreshold Property which determines when the DataReceived event fires based on how many bytes are in the RX buffer.
Thats why whe you press the button to request the data , it changed the SerialPort.ReceivedBytesThreshold to 128 , but even with SerialPort.ReadTimeout set at many diferent variables i wasn't able to make it work reliably .
The serial port read method will read up to the count. If fewer bytes are available that is what is returned. See the documentation for further information.
With me setting the SerialPort.ReceivedBytesThreshold to 128 doesn't that mean that when the 128'th byte is received then the SerialPort1_DataReceived Sub is called ... meaning there are 128 byte already available in the buffer ?
-
Mar 3rd, 2018, 01:11 PM
#6
Re: Serial Port not receiving full data
Originally Posted by Dope40
Thats why whe you press the button to request the data , it changed the SerialPort.ReceivedBytesThreshold to 128
Sorry I missed that in your post.
Originally Posted by Dope40
With me setting the SerialPort.ReceivedBytesThreshold to 128 doesn't that mean that when the 128'th byte is received then the SerialPort1_DataReceived Sub is called
Technically when the in buffer count equals the threashold, the DataReceived event is raised which gets handled by your SerialPort1_DataReceived method. But yea, the sub should run when there are enough bytes.
Also, according to MSDN
The DataReceived event is also raised if an Eof character is received, regardless of the number of bytes in the internal input buffer and the value of the ReceivedBytesThreshold property.
Another way to handle it is to set the threshold at 1 so the received event will fire for every byte, and use a RXBuffer (List(Of char) or char array) declared in the class. When the receive event fires, read everything that is in the DataReceived buffer in to your RXBuffer, and when the RXBuffer has 128 bytes in it, go process it.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Mar 3rd, 2018, 09:51 PM
#7
Addicted Member
Re: Serial Port not receiving full data
Another way to handle it is to set the threshold at 1 so the received event will fire for every byte, and use a RXBuffer (List(Of char) or char array) declared in the class. When the receive event fires, read everything that is in the DataReceived buffer in to your RXBuffer, and when the RXBuffer has 128 bytes in it, go process it.
That is exactly what I had in mind in my post, I said array but a queue would probably be better and there is a lot of flexibility on how much the queue can hold and how to manage that data
After that i need to use a timer ( 100 ms ) to request live data from the PIC . this work better but still some of the time i get some random data. I haven't focused on that for the moment because without the dataset everything else is useless .
If the above does not require the entire 128 bytes it seems a little wasteful and you will have to figure out what your live data is and what to do with it. Of course you can also have several different Read routines with varying values in the For - Next
Ideally if you have control over what the PIC is transmitting a simple header may make the communications more reliable without too much overhead.
Glad you have resolved the problem and I hope these tips give food for thought, perhaps in your next project.
-
Mar 5th, 2018, 05:02 PM
#8
Re: Serial Port not receiving full data
Originally Posted by Mc_VB
That is exactly what I had in mind in my post, I said array but a queue would probably be better and there is a lot of flexibility on how much the queue can hold and how to manage that data
If the above does not require the entire 128 bytes it seems a little wasteful and you will have to figure out what your live data is and what to do with it. Of course you can also have several different Read routines with varying values in the For - Next
Ideally if you have control over what the PIC is transmitting a simple header may make the communications more reliable without too much overhead.
Glad you have resolved the problem and I hope these tips give food for thought, perhaps in your next project.
My experience is not to mess with ReceivedBytesThreshold and to accumulate the bytes received, as mentioned already. No timer should be required.
-
Mar 5th, 2018, 05:53 PM
#9
Addicted Member
Re: Serial Port not receiving full data
My experience is not to mess with ReceivedBytesThreshold and to accumulate the bytes received, as mentioned already. No timer should be required.
Hi dbasnett, I agree with you on all three points, and that is what I set out as an example.
Including my quote appears as though I advocate the opposite, if that is how it appears then I should have given more thought to the wording.
A person would be well advised to review code that you have previously posted regarding serial port issues, I know that I do occasionally although I tend to do things slightly different. So if the OP reads this he/she should add these resources to their bookmarks if they intend to continue with projects of this kind. In fact I would argue that VBForums overall is probably the best resource out there.
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
|