01-30-2017 04:00 PM - edited 01-30-2017 04:02 PM
Hello all, I'm trying to communicate with a machine that uses serial (RS232), in a LabVIEW program. Here are some important points:
Here it is in device manager:
Here it is in NI MAX:
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):
(edit: I forgot to say, for the VISA resource name, I'm using ASRL3::INSTR.)
and it always gives me this error:
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:
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.
Solved! Go to Solution.
01-30-2017 04:05 PM - edited 01-30-2017 04:10 PM
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.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
01-30-2017 04:32 PM
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!
01-30-2017 04:50 PM
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.
01-30-2017 04:52 PM
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.
01-30-2017 04:58 PM
I was wondering why it was so amazingly slow. All bets are off when you are t-shooting. Sometimes code can get pretty strange. 😉
01-30-2017 04:59 PM
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.