08-14-2017 09:44 PM
My vi (UdpRevLV.vi) receive data form MFC , and decompose the datas. After I added the code to quit the program(exit button), the displayed waveform is distorted, is that the reason I got wrong data?
correct wave is like this
now wave seem distorted
Attach the code,please help me .
Solved! Go to Solution.
08-15-2017 12:21 AM - edited 08-15-2017 12:26 AM
You do a single UDP read, then go into lengthy processing, filtering, loops, file writing, UDP writing, etc. until you do the next UDP read. During that time other packets might arrive and get lost. Why are your shift registers initialized with arrays containing one element instead of empty arrays?
Can you attach an example of the received string from a single UDP read? How long is the string?
Overall, most of your code is 10x too convoluted. Why do you have so many local variables, e.g. the length. You cannot guarantee that the local variable reads only occur after the indicator has been written. I see a few other race conditions: You read and write from locals in parallel (array 2, fstrings), but the result depends on what happens first.
Even the U8 array to single conversion could be done with much less code (also, index array is resizeable).:
08-15-2017 12:57 AM
Hi, thanks very much for your reply!!
Actually, each time I received 100 float numbers from MFC, below is the example of received data(I make it displayed in Hex format, it's length is 400):
I'm really a novice. I really don't know how to simplified code.Thank you for the solution of U8 array to float number, it is really amazing.
I want read from UDP port and change them to float numbers, display on a Waveform Chart, and filter these data, finally send back these filtered datas. Only after filtered these datas will be send back. I use a Flat Sequence Structure between the three sections. As you mentioned the local variables. I do not know how much datas are received each time in real terms, so I use local variables.But when testing, I send 100 data at a time.
08-15-2017 01:34 AM
Please attach the data as a VI (e.g. right-click the indicator containing the data...create constant, copy that constant to a new VI and attach it here). I cannot copy/paste strings from an image.
08-15-2017 01:43 AM
Sorry, here is the data.
08-15-2017 10:07 AM
(Thanks. Sorry it was a little late last night)
I cannot analyze your code because it is just way too convoluted, but here is my suggestions. Use multiple loops. One tight loop that just reads from UDP and places the unfiltered data in a queue. This way the VI is virtually always ready to receive data. (Remember that UDP is connectionless and if packets arrive when you are not listening, it gets discarded).
You are still doing all this with 100x too much code. To convert the 400 bytes of data to a SGL array, all you need to do unflatten it using little endian byte order. (except for the filtering, this replaces your entire gigantic while loop!! :o). I haven't looked in detail at the rest of you code, but I am sure that similar simplifications can be done in many places.
So, in summary, you need to have a simple receive loop that places the data in a queue to be processed asynchronously.
08-15-2017 08:43 PM
Thanks very much. I will remember your suggestions .Though, I have not use the queue yet, I would have a try, hoping to make my program simpler in some way.
Last, really thank you for your advice and efforts again!