LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Port Reading

Solved!
Go to solution

Hello,

 

Please I need your help guys.

I am trying to read measurements from a serial port via USB-RS232 converter.

 

The problem that the script is working without any error,however it is writing always  0. 

However the result is expected to be different from 0 . (Please see attachment )

 

Any hints or explanation would be very appreciated.

Thank you in advance.

Greeting.

0 Kudos
Message 1 of 10
(4,313 Views)
Solution
Accepted by topic author Tanieta

Hello,

 

You're sending the char "1" to a serial device and it's not returning any bytes.

So:

1. The cable may be incorrect

2. The settings (baud rate, termination char) may not be correct.

3. The device may not understand the conmand and because of that it doesn't reply

4. Maybe it's better to use the number of bytes input in the serial read vi. This way, the function will only exit after expiring the timeout.

 

Software developer
www.mcm-electronics.com





PORTUGAL
0 Kudos
Message 2 of 10
(4,303 Views)

Hello Jorge_Amaral,

 

Thank you for your reply..

 

I've checked the 3 last point that you gave... I've tried all the possibilities.

 

But it seems that, I need to use a USB-RS232 converter from the manufacturer.. 

maybe it is this the problem 😕 

 

 

0 Kudos
Message 3 of 10
(4,293 Views)

Hello again,

 

From my experience you don't need to buy the cable from the supplier. If you find the manual you can do it yourself.

Are you completly sure that the command that you are sending is correct? "1" seems very strange command.

Software developer
www.mcm-electronics.com





PORTUGAL
0 Kudos
Message 4 of 10
(4,282 Views)
Solution
Accepted by topic author Tanieta

Without knowing what device it is and seeing the Programming Manual for it, debugging this is really shooting in the dark.

 

As Jorge already mentioned the command "1" looks suspicious. But more importantly there is no hardware handshaking on serial lines that tells the device "this is the end of the message, start to decode and interpret it". While a command parser can be made to operate on input streams and recognize commands as soon as they make sense without any specific "end of message" indication, such a parser tends to get pretty involved and cumbersome very quickly, so they are seldom implemented that way.

Therefore when talking with a serial device you almost always have to append an "end of message" indication such as a <carriage return> or a <line feed> or a combination of both.

 

Most people get misled when trying to talk to a serial device through a terminal app, thinking that they never had to append a termination character to the message, not realizing that the very activation of the Enter key not really tells the terminal application to send the string of, but rather causes one or both of the above mentioned characters to be sent to the device.

 

You can easily add this character to your command in LabVIEW. Either using a Build Array and one of the according constants in the String palette (you need to know that the Carriage Return constant always will add the carriage return character 13 or 0xD, while the Line Feed constant will add the linefeed constant 10 or 0xA). The End of Line behaves differently depending on the LabVIEW platform it is executed (on Windows it adds <carriage return> + <line feed>, on Unix it only adds a <line feed> and on Mac it adds a <carriage return>.

 

Alternatively you can enable Backslash Code Display on the string constant with the right click menu and enter directly \r for <carriage return> and \n for <line feed>.

 

And once you have a working communication in highlight mode, get immediately rid of the Bytes at Serial Port property. It will not work like that as the Bytes at Serial Port will be evaluated before the entire string has been received by the device, processed and an answer has been sent back. People who use the Bytes at Serial Port property are tempted to add delays after the VISA Write to avoid this problem but that is really like repairing a water leak with paper: BAD BAD BAD! 

 

The device usually will terminate its response in the same way as it expects the command to be terminated. VISA Configure Serial Port has two specific input parameters, one is a boolean called "Enable Termination Char (T)" which is by default set to true and the other is an integer called "termination char" whose default value is 0xA for the <line feed> character. If your device terminates the message with the <line feed> character you are already done, otherwise change the "termination char" input to the ASCII value for the character the device uses. Now you can simply place a VISA Read after the VISA Write, pass a number of bytes to read to it that is guaranteed larger than the longest message the device will ever return, and adjust the timeout on the VISA Configure vi to a reasonable value if you find the default of 10 seconds unsuitable and the VISA Read will always return after one of these conditions has been true:

1) an error occured

2) the termination character has been received

3) the number of requested bytes has been received

4) the timeout has expired

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 10
(4,277 Views)

Hello again,

 

THank you very for your help, that was really very helpful to understand many things.

 However please, I 've tried to set the commands according to your hints and what the device need,

but it was not successful.

Please could just tell me what is the right command format that I should set ... I am very confused.

I've attached 2  screenshots for the device commands and what I setted in Labview (on windows)..

 

Many many thanks in advance.

Greetings

 

 

Download All
0 Kudos
Message 6 of 10
(4,245 Views)
Solution
Accepted by topic author Tanieta

You're sending a carriage return and line feed while the command page says send a carriage return.

 

You are also sending a plus sign.  I don't think you are supposed to send that.  I think it is just a symbol in the manual to tell you to "add" a carriage return to the end of the message.

 

It is also very unusual to send quotes in a serial message.  I'm wondering if the command manual is showing quotes just as an indicator for what the actual command is.  Try eliminating the quotes as well.

 

So for your  constant showing in \code mode  try  AAms\13

 

Since the response ends with a carriage return, then be sure to initialize the serial port with the termination character enables and that it is 13 decimal or 0D hex.  Get rid of Bytes at Port and read a large number of bytes with the VISA Read such as 50.

0 Kudos
Message 7 of 10
(4,241 Views)

Hello

 

Thank you very much for your reply.

for the Bytes at Port if I remove it from the VI,

 I always get the error:

VISA:  (Hex 0xBFFF0015) Timeout expired before operation completed.

and with that I've tried the rise the timeout from 10sec to 20sec.. but it remains the same problem...

That's why I kept it on my VI 

0 Kudos
Message 8 of 10
(4,234 Views)
Solution
Accepted by topic author Tanieta

In addition to what RavensFan said:

 

The quotation marks are almost certainly only meant to indicate that you need to enter that text literally and will not be expected to be actually sent to the device.

 

Are you sure you set the device address to AA? I think it is more likely that the device uses by default an address of 00 or something close to it.

And while the ms command may be supported it is not mentioned in the part of the manual that you posted. So I would first try with the example as in the manual.

 

And no \13 is not the right code for carriage return. If you want to enter the numeric value of an ASCII code you have to use hex values, that means you need to enter \D and when the string control is correctly set to be in backslash mode, LabVIEW will automatically change this into \r, the preferred format for backslash code display.

 

Last but not least, why does everyone think that sending an image is perfect? Are you afraid someone will steal your VI? This way it is impossible to check if you entered the command correctly as the backslash only means the right thing if you enabled the option in the string, but that is not visible in a picture.

 

And of course will you receive a timeout as long as you are not sending the exact perfect command. Most serial devices will simply ignore anything they don't understand and/or wait for the correct termination character which you haven't sent so far.

With the Bytes at Serial port you will simply always see 0 as result since the device doesn't send anything back, with the VISA Read you get a timeout error until the VISA Read sees a termination character or the amount of requested bytes. Your specific device adds to that an address (the first two numbers in the command) and as long as those two characters are not matching the address the device is configured for it will simply ignore the entire command.

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 10
(4,227 Views)

Thank you all for your help.

the problem was that I am setting the address to AA however it should be 00 , means 00ms.

I removed the bytes at port, and I wrote \D instead of \r and everything is fine 😄

 

I have no problem to post my code just i thought it is better to show images 😄

 

Many thanks again for your clarifications.

 

Cheers

 

0 Kudos
Message 10 of 10
(4,217 Views)