From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Port Communication Graphing using PIC32

Solved!
Go to solution

I am sending serial data to labview from a PIC32 specifically the PIC32MX340F512H. I am trying to graph the position of the potentiometer. I have values that are input ranging from 0-1023. When I try graphing it, the graph plots 1 byte each time which is giving me an unwanted graph. For example, for the number 1023 it will plot 1, then 0, then 2, and then 3. How do I fix this problem?

 

Note: I am a beginner with LABVIEW and have minimal experience. 

 

0 Kudos
Message 1 of 9
(4,269 Views)

What does your incoming data look like?

 

You are reading 4 bytes at a time.  Are you sending ASCII characters "0" "1" ...  "9"?  Are you they padded with zeroes?  ("1024" is for bytes, but "12" is only 2)

 

What does your device's code look like?  Are you sending a termination character?  (For a 4 digit number string, you should be reading at least 5 bytes so that you can the 4 characters and the termination character.  If you probe the error wire or put an indicator on it, is it telling you anything?

 

What is going on with your shift registers?  You have a shift register you are sending data to at the right side of the loop, but you aren't using the left side shift register, just using the plain tunnel.  Either get rid of the ordinary tunnel or get rid of the shift register.

0 Kudos
Message 2 of 9
(4,261 Views)

I am sending ASCII characters but they are read as strings which I converted to numbers from 0-1023 by labview. 

 

The device code is attached. I am not sending any termination character. I am not sure which error wire you're referring to. 

 

Yes my program got a little messed up I made this program from multiple tutorials on youtube. I was going to send that to the visa close object box. 

0 Kudos
Message 3 of 9
(4,254 Views)
Solution
Accepted by topic author hashirahmed

I'm not familiar with the sprintf function, so I had to to google it and found this.

 

https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

 

The most interesting part of that said "

Return Value

If successful, the total number of characters written is returned excluding the null-character appended at the end of the string, otherwise a negative number is returned in case of failure."

 

So it sounds like it is sending a termination character, but that character is a null-character which is decimal 0 or hex 00.

 

Your Serial Configure in LabVIEW doesn't have the termination connectors wired in, so it uses the default values of enable and the linefeed character (decimal 10, hex 0A).

 

I think you are having issues with the read not terminating until you get the 4 bytes you read because it didn't get a linefeed character.  But you get a null character in there and that is disrupting the conversion from the valid ASCII characters you do get when things get out of sync.

 

Don't learn programming by watching Youtube videos.  You are better off googling for written programming references.  I know there are better examples out there for out to send data through a microcontroller serial port.

 

The NI forums have a user group for Arduino.  LabVIEW Interface for Arduino (LIFA)  I know your device is not an Arduino, but the code that can be programmed on that looks similar to your controller.

 

Find a version that sends a better defined termination character.  Or if you can be sure it sends the null character as a termination character, you can wire a 0 to the termination character input of the VISA Configure.  And you should read more than 4 bytes.  At least 5, if not more, just so you can be sure that you get the full 1024 and the termination character when you do the VISA Read.

Message 4 of 9
(4,206 Views)

Here's an idea.  Try putting a \n in the format string in C so it looks like "%d\n".  Then when you send the buffer, try to send it with strlen(buff)-1 because I think you need to cutoff the null character at the end of the string, but it would now follow the linefeed character that LabVIEW wants.

Message 5 of 9
(4,198 Views)

Thank you for the help. I didn't know about the termination character that was being sent. Labview wasn't picking up the 0 at the end of sprintf which is probably cause my UART was set to send everything except that. I just decided to send the '\n' after each value in my PIC code and now it reads it perfectly. I also added the concatenated string to append the values together. 

What I don't understand is what does the Visa property node do? When I added this it would not show any values.

0 Kudos
Message 6 of 9
(4,176 Views)

Which VISA property node?  I don't see any in the code you've posted so far.  And there are lots of different VISA property nodes you could have added.

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

I saw numerous people use the instr visa property node with the number of bytes serial property going to the byte count of the read visa block.

0 Kudos
Message 8 of 9
(4,148 Views)

Okay.  The Bytes at Port property node you'll see used in the NI Serial Basic Read and Write example.  It is not a good example.  I find that Bytes at Port should not be used in perhaps 95% of programs using serial communication.  When you have a defined termination character, or a well defined communication protocol, don't use it.

 

The most common mistake is people will write a command, immediately check bytes at port, and read that many bytes.  Without a sufficient delay between the write and bytes at port check, they have not give the device enough time to read the command, act on it, and send back a response.  They'll get 0 bytes at port and proceed to read 0 bytes, which means they get nothing.

0 Kudos
Message 9 of 9
(4,138 Views)