Control serial devices in Python

I have a serial device that plugged in the usb port, and I want to use python to send command to the device. I can’t seem to find any tutorials. I am trying to avoid Node-RED as the rest of my workflow relies on Python. One way I could think of is to have my device’s python API loaded on the shell and receive command using socket or paramiko. I could use direct serial.write too if that is easier. I’d love to hear any suggestions.

Welcome to the Opto forums!

If your device is an authentic FTDI, then you will find it in /dev
Probably as something /dev/TTYUSB
Once you see it there, you know what device use in Python.
Should be as simple as that.

Hi Beno, thank you so much for your quick response! I guess my question was a bit vague. In fact, I am using OptoMMP python package on a computer to connect to the opto host. I was hoping to make a connection from my computer to the groove RIO, ideally the computer sends a command, while the groove listen and execute the python script accordingly. Wonder what’s the best way of doing this.

Thanks for the extra information…
Im still just a little confused… You have Python running on the RIO talking to the serial device on the RIO, so that bit is working?
You are now looking for peer-to-peer (PC to RIO) via Python suggestions?

Yes, I have python on RIO, I am still trying to install the FTDI driver, but it should work. and yes, the PC end has to be Python, but I am quite open for the RIO (NODE-RED or Shell command etc.). Thank you in advance.

Quick reply as I tag in @torchard to this thread…

There is nothing to install for the FTDI, that’s why Opto recommends it. The kernel drivers etc are all there, just plug and play.

Would using the scratchpad work for this?
From the PC write your command to a specific string element, and and have the RIO script watching that same element. That way it’s all using the same OptoMMP package you’re already working with.

Hi Terry,

Yes, I think that should work, and I would love to keep using optommp too, but I could find very limited documentation about how to use the scratchpad over the RIO end. For now, I am using ssh to run pyserial in the RIO shell to control instruments, I wonder how to associate the scratchpad command to the pyserial command that I would like to send, or ideally to execute a python function. Can you help explain more about that. I appreciate it.

If I’m understanding your application correctly, one way that I would try is to have a delayed loop in the Python script that does a GetScratchPadStringArea from the RIO’s local scratchpad area, then do some if/else or switch comparison on that data to determine which pyserial command to execute.
Once the code is executed don’t close the script, just return to looping over the Get command until a new value is written to that scratchpad index.