Showing results for 
Search instead for 
Did you mean: 

Reading time interval of serial port

I am trying to read data from serial port by a loop using NI sample code.


I found if the loop time interval is set to 1ms, no data can be read. If set to 50ms, some of the data can be read. If set to 500ms, almost all the data can be read.


The data will arrive every 3 seconds.


Any suggestion, thanks.

0 Kudos
Message 1 of 15

Please post your VI so we can see how you are doing things.  Your description of having a loop and a wait leaves nearly an infinite number of possibilities as to what else you are doing that affect the answer to your question.  We can guess, but that is just going to waste your time and our time.  With an actual VI, then we can make real suggestions.

Message 2 of 15

If you are referring to the basic example, then you are correct, that won't work well for you.  You need to have a loop that will continuously read until all the data is read.  See snippet of basic example below.


Message 3 of 15

Here is a modified version of the advanced example.

The snippet below shows the changed code.

I also attach the modified code for your convenience..  Smiley Wink



the snippet is not as clean as I intended, but should be ok  (leftmost of snippet).

Message 4 of 15

The screenshot is my code, is there any problem.

Does I need to detect if the read byte from com port is 0, then no read.

But I am not sure how the data is lost, thanks.

0 Kudos
Message 5 of 15

Why are you initializing the Com port and closing it on every iteration of the outer while loop?


If data is arriving while the port is closed and before you have initialized it on the next iteration, then you will have lost data.

0 Kudos
Message 6 of 15


@alex. wrote:

The screenshot is my code, is there any problem.

Does I need to detect if the read byte from com port is 0, then no read.

But I am not sure how the data is lost, thanks.


On top of what Ravens Fan mentionned, why do you use Local Variables?  I partially see another loop to the right.  That may spell trouble..

Why did you not implement your code similar to what I posted?


Aside from the above and probably aside from problems you are getting is the Stacked Sequence Structure.  What is going on in the other sequence?


I suggest you post the actual code, there may be several things lurking in there Smiley Wink


Aside #2: Good coding practice:  It is highly recommended not to run wires behind objects, such as a Case Statement.

Message 7 of 15

The outside loop not run often, only for start/stop button. The local variable and right side loop is only for start/stop and final quit.


The sequence is only for data transfer. The vi is changed from the basic com port vi sample.


By monitor the inside loop of com port reading, the data lost is from there.


0 Kudos
Message 8 of 15

You still won't post your code and you seem to ignore the recomendations given to yoou. Did you eve bother to run Ray's example? You seem to only care if the number of bytes read is greater or equal to 19. Is this 'lost' data of yours simply discarded by your program? Its also silly to have the termination character enabled and also use the Bytes at Serial Port no matter that the example does the same thing. What kind of troubleshooting have you done on your own?

Message 9 of 15

Please see the attached vi.  I tried to modify Modded Advanced Serial Write and and get the attached one.

I also tried to Modded Advanced Serial Write and, seems also has data lost.

When do the troubleshoot, I try to put the all the orignal readed string into an array, but if set the loop time to 1ms, the loop is empty.

0 Kudos
Message 10 of 15