LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Get data correctly throudh serial port

Solved!
Go to solution

 

Hi, 

my name is Miguel and I am new with Labview and this forum.

My objective is create a program which receive data from a microcontroller through serial port. From the microcontroller I am sending multiple bytes [10, 20, 30,100,200,255]. The problem is when Labview receives data, the values are represented in different order, I can see in the array follow order [20,30,100,200,255,10]. 

How can it occurs? 

program.PNG

 

Another question I have is why the delay has to be so high.

 

Thanks in advance,

 

Regards

0 Kudos
Message 1 of 9
(7,206 Views)

Hello,

 

In order to work out why you are receiving data in this way, I would use I/O Trace:

 

http://digital.ni.com/public.nsf/allkb/282C5D41E2BA04F2862574BA007803B9

 

Once you have the capture, with the VISA Read function in the trace, right click and select properties, under buffer you will see exactly what VISA received.

 

As for the delay, serial communication is inefficient for high speed communication, it communicates binary data upon one line in comparison to other communication protocols which have parallel lines. Therefore it takes time to actually send the data, which should correspond to the baud rate.

 

What is your baud rate? What is the microcontroller?

 

Best regards,

 

Ed

0 Kudos
Message 2 of 9
(7,184 Views)

How do you control syncrhonization between your microcontroller and the LabVIEW read?  Is the microcontroller sending these bytes continuously?  Perhaps the 10 was already sent by the time you opened up the Com port in LabVIEW and started reading.  Then you get the rest of the package, have to wait some time, and then see the 10 at the beginning of the next package.

 

A mistake I see in your LabVIEW code is that you have a Serial Configure inside the loop which means it will run on every single iteration.  That belongs before the while loop.

 

A good communication scheme would have the LabVIEW code send a command to request data, and the microcontroller send the data once it sees the command.  If the message is binary type data (seems like it is since you have bytes of any value from 0-255), is to disable the termination character.  (Which happens to be a line feed by default, and is why you see the x10 as the last byte).  If the data is variable in length, as opposed to always a fixed number of bytes, is to prepend the message with the number of bytes.  So you send a write command.  Read 1 byte  (no wait needed).  When that byte arrives, you feed that value into another VISA read which proceeds to read that X number of bytes.  Now you have a complete message and you aren't starting the read in the middle because the microcontroller didn't send anything until it new that LabVIEW was read to receive it.

0 Kudos
Message 3 of 9
(7,159 Views)

Hello Ed,

thanks for your answer.

Doing what you say, I have seen in a first moment the program receive one data and after this the program receive other data. In the follow picture you can see it.

Capture.PNG

 

I am using 9600 bauds, but in the real application I will use a high baud rate with the MSP430F5438A.

 

Regards,

Miguel

 

0 Kudos
Message 4 of 9
(7,127 Views)

Hello,

I have extracted the serial port initialization before the while loop, but the program responds on the same way. In a first moment I can receive the value 10 in the correct position, but when the next package is received, this changes its position to the last.

 

Regards,

Miguel

0 Kudos
Message 5 of 9
(7,126 Views)

Hello,

 

Could you attach your VI and the I/O Trace instead also? The VI mainly to verify the changes and get an up to date version, for the I/O Trace you can save this as a file in the top left of the window and then attach said file.

 

Best regards,

 

Ed

0 Kudos
Message 6 of 9
(7,118 Views)

Hi Ed,

no problem, I attach the files.

Please, if you find big errors tell me.

 

Thanks,

 

Regards,

Miguel

Download All
0 Kudos
Message 7 of 9
(7,112 Views)
Solution
Accepted by topic author mivami

The big problem is that you didn't read what I wrote about needing to disable the termination character.

 

See the attached VI and see if it works better.

 

Note, you could still have a problem.  If the port opens in the middle of a message, the response will come back misaligned, and remain that that way.  That is why you need to implement a better communication protocol where you send a command to request the data, and the microcontroller responds to that command and sends it.

 

Also, this assumes every message is 6 bytes long.  If your response is variable in length, then you should do what I said and prepend the message with a byte that tells how many actual data bytes follow.

0 Kudos
Message 8 of 9
(7,106 Views)

Hi Raven,

I am so sorry, I though that the termination character you said was in the microcontroller code. 

Thanks for your answer, the program is running correctly now. 

In the next version, I implement the initial command in order to send the data, thaks for the information.

 

Best regards,

Miguel

0 Kudos
Message 9 of 9
(7,086 Views)