LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I communicate with this serial machine using LabVIEW?

Solved!
Go to solution

Hello all, I'm trying to communicate with a machine that uses serial (RS232), in a LabVIEW program. Here are some important points:

 

  1. I'm using a Serial/USB converter, since I don't have any RS232 ports on this PC (they often don't have them on laptops anymore!), and Windows (10) successfully found and installed the driver for the converter. It now shows up in Device Manager under Ports, as COM3.
  2. I am positive the hardware of the converter works. I have a python script (shown below) from a colleague which was used to communicate with the machine, fetching a value. If I run this script in Python, on this machine, with the converter and such, it fetches the correct value. So we know that that part is working.
  3. NI MAX sees the device as well, calling it ASRL3::INSTR "COM3".

Here it is in device manager:

com3_devman.PNG

 

Here it is in NI MAX:

NIMAX_com3.PNG

Here's the python script:

 

import os
import sys
import glob
import operator
import serial
import time

from string import *
from time import strftime

# Initialize serial
sii=serial.Serial('COM3',1200,timeout=0.5)
print( "BEGIN")

sii.write(b"T\r") 
time.sleep(1)    
temp=sii.read(100).decode("utf-8") 

sii.close()

print(temp)
print ("done")

 

I only include that to show an example of something that does work.

 

Here's the LabVIEW VI I'm trying to use (.vi attached):

serialread_vi.PNG

 

(edit: I forgot to say, for the VISA resource name, I'm using ASRL3::INSTR.)

and it always gives me this error:

 

NI_serialerror.PNG

 

So here's the problem. I'll run the Python script, it will work, and then I'll go to LabVIEW, try my VI (attached, same as image above), it doesn't work, gives that error, and then if I go back to try the Python script again, it gives this error:

 

serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)

So clearly the Port COM3 is getting used, but not given up, by LabVIEW or something. I really don't know much about Ports at all though.

 

Similarly, I can sometimes access it in the NI MAX VISA test panel, but usually at this point, if I try, I get this error:

 

visa_test_error.PNG

 

What can I try? Is there some way to "reset" a COM port? I've tried doing something I read, right clicking on the port in Device Manager, and disabling/enabling it, but it doesn't work (for example, after doing that, I get the same Python error). If I reboot, it works, but that's not a solution I can use.

 

Further, any idea why my LabVIEW VI wouldn't be working, when it appears I'm doing the same thing as the Python script? It appears to be sending "T\r" (T with a carriage return), which I'm doing in the VI. My one suspicion is that the script has that 'b' in front of it, making it a byte stream... I'm not really sure how to do this in LabVIEW. I know about the string to byte array VI, but that produces a byte array, and VISA write needs a string...

 

What can I do? This is driving me nuts. Thank you for any advice, it is much appreciated.

 

 

0 Kudos
Message 1 of 7
(5,423 Views)
Solution
Accepted by topic author optoelectro

I don't see you opening or closing the port so I'm unsure what VISA will do, oh and you never setup the baud rate, stop bits, etc that are needed.  Go to the Help >> Find Examples and search for the Simple Serial example.  This basically does what you need configuring the port, writing data, waiting, reading, then closing it.

 

If any application using the port isn't closing the port when it is done, it might be holding onto the resource and not allowing any other application to have it. Same with LabVIEW or if you have a MAX test panel open.

 

Edit:  Oh the mentioned example uses 0x0A or \n as the terminating character.  Right click the string control and choose \ Code Display then type in T\r and it should send the single character T then the single character CR.  You may also need to set a constant on the block diagram of the Configure function.  It looks like it also uses \n as a termination, which can be disabled if you aren't using synchronous messages, or are adding the termination with each write.

0 Kudos
Message 2 of 7
(5,420 Views)

Hi, I got it working, thank you! I feel like a fool now.

 

I basically just plugged T into where *IDN? was in the simple serial example. I foolishly didn't notice/think to use the fact that the python script specified the baud rate (1200), so that was pretty much the only thing I had to change, I think. Now it returns it correctly, thank you!

0 Kudos
Message 3 of 7
(5,390 Views)

Now that we've gotten by that, this seems like horribly inefficient code.  Why would you wait around for a hardcoded two seconds?  Does the equipment manual specify this?  If not, you're probably not completely understanding how to communicate with the equipment.  Normally, you want to wait around for as little time as you need, not as long a time as you'll ever need.

 

This is analogous to giving someone a task to do and say, "I'll ping you for results in two hours," instead of saying, "Give me a call when you are finished," because occasionally it takes almost two hours to complete, but usually it just takes 15 minutes.  Oh, and you need the results ASAP.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 7
(5,373 Views)

Thanks for the response! I was actually just using 2s as overkill when I wrote that example, but now I'm using 200ms. I might be able to use less, but speed isn't a huge issue here and the machine is actually fairly old. Thank you though.

0 Kudos
Message 5 of 7
(5,371 Views)

I was wondering why it was so amazingly slow.  All bets are off when you are t-shooting.  Sometimes code can get pretty strange.  😉

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 6 of 7
(5,364 Views)

The point was that you do not even need the wait.  The default timeout for the read is 10 seconds, set by the VISA Configure Serial Port.  So if your data comes in before that timeout, you will get the data in the simplest and fastest way without the wait.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 7
(5,363 Views)