This question already has answers here:
Python to automatically select serial ports (for Arduino)
(4 answers)
Closed 6 years ago.
I am using an Arduino for sensing using Python 2.7 on Windows XP, but the non-static nature of the USB-to-serial port translation is giving me a headache. With a physical serial port there is no issue hard coding the port position, but the Arduino is moving around based on what is or is not plugged in at the time of object instantiation. Is there some way in Python for me to just get the port address during each object initialization and pass it to PyVISA or pySerial?
I also suggest a handshake but do it the other ay round.
Just READ for input from the all Serial ports before starting your program. As you turn up the Device you can make it send something like an ON signal. when your code detects the ON signal on that port then do a handshake.
In pySerial there is a quite hidden way to check for VID/PID on all serial ports (at least on Windows).
Just find the VID/PID of the Arduino in port properties adn put it into the python code.
Of course this won't work if you have multiple Arduino connected (same VID/PID)
import serial.tools.list_ports
for port in list(serial.tools.list_ports.comports()):
if port[2].startswith('USB VID:PID=1234:5678'):
#here you have the right port
I recommend a handshaking signal and scanning all the ports. For example, send "whoru" from your python script to the arduiono and have code on the arduiono that responds with "arduino" when it detects "whoru" on the serial port. This way, you scan the ports, send the handshake, and when you get the proper response you know which port the arduino is on.
Related
I have an accounting program in windows 10 that I want to extract data from it using python code.
I want to do that by sniffing on the serial port that the accounting program writing data to it.
I have two problems:
1-I used process manager to see the internal serial port that the programs are using but I can't find the serial port that the accounting program is using to write data to it.
2- I used virtual ports to try to sniff on it using python serial library, when I try to access the red/write com ports I have PermissionError(13, 'Access is denied.', None, 5).
I'm expecting that the accounting program only start using serial port after some trigger like start printing order or something else.
and after I find the serial port that the program is using how to sniff on it using python?
I would appreciate it if someone can make the idea clear.
This question already has answers here:
Directly send signals to USB using Python
(2 answers)
Closed 10 months ago.
I am trying to create a USB device that vibrates whenever a key is pressed on the computer it is connected to. I have a working Python script that detects key presses, but I need it to send a signal to a USB device (which hasn’t been made yet). How would I go about this?
This may help a little bit: Directly send signals to USB using Python
To minimize what might be helfup:
Controlling components like LEDs from devices like Arduino and Raspberry Pi are done using GPIO pins. In contrast, USB is designed for data transfer and not for maintaining more constant high or low signals like GPIO. It also consumes different voltage and current levels than GPIO pins and could potentially damage components meant for GPIO.
However, you can get USB to GPIO adapters for this purpose (see here for an example discussion on these options).
In terms of Python, you can use packages such as PyUSB or the libusb Python wrapper to control/transfer data/communicate with USB devices (such as the USB to GPIO adapters). The companies providing the adapters might also have designed their own easy-to-use Python package to wrap around a lower-level driver (probably written in C). See this USB controlled LED device as an example with its own Python driver. The drivers are just software programs that take in relatively simple commands from the user for what they want a device to do. They then encapsulate the lower-level complexities required for following a protocol to communicate the user's intention to the USB device and controlling the USB port at the lowest possible software level.
Using pyserial you can read from a serial device and also write to it.
import serial
ser = serial.Serial('/dev/ttyUSB0') # open serial port
ser.write(b'hello') # this would depend on the protocol for the USB device
ser.close() # close port
Having trouble with reading from a serial port.
i have a bit of hardware that shows a display with date and time and other details and on the PC you through some software you can bring up a live mirror of the display. what i want to do is log some data at intervals.
using pyserial its trivial to connect to the port, but when reading it using the simplest settings with timeout=none and read size =1 and leaving the port listening indefinitely there is no output from the equipment.
ive tested using virtual port pairs and i can read and write no problem.
Any ideas why there is no output from the serial port?
And i know its the right port because when i open it then it blocks the mirrored display on the PC.
Cheers
I'm new to this kind of things so I need help from you guys. When playing around with serial ports, I figured why not echo from one terminal and cat/listen from the other using one of the /dev/ttyS* serial ports on my ubuntu 15.04 laptop. When i do cat /dev/ttyS0 it gives me input output error. then i tried doing it using pyserial on python. when i do
import serial
ser = serial.Serial('/dev/ttyS0')
it gives me SerialException: Could not configure port: (5, 'Input/output error')
After looking around on stack overflow, I found this related post Pyserial: could not configure port: (5, 'Input/output error)
where one of the guy said you need a physical connection to make this happen. I'm guessing that is to create some kind of loop. So my question is what kind of physical connection is required? Who a usb stick do it, or do i need something dedicated to work with the serial port. And if someone would provide good explanation on how serial port programming works, that would be great.
Feel free to edit the question where you see fit. Guys please no need to complain about how dumb this question sounds and all that. After all lots of people come here to learn, and that is what i am doing
You need to have one or two physical com or serial ports on your pc to achieve this. if you have one port you can create a loopback plug by connecting the Rx and Tx pins together.
If you don't have a physical serial port and want to test it you can use socat to create a virtual serial port.
Physically you need a cable that swaps the transmit and receive pins. This is known as a "null-modem cable" because it can connect two computers together without a modem in between.
You probably also need to add yourself to the "dialout" group in order to use the serial ports.
putty is a really good program for testing with serial ports -- you can have two putty instances talking to each other, and then try to connect putty on one side with Python on the other side.
Finally, sometimes there is a modem daemon that will run and think that it should own all the serial ports. It usually gives up and lets them go after awhile, but you might have to find it and kill it if you don't want random processes polling for modems on your serial connection.
Is there anyway to do this without getting a "COM PORT IN USE" error? I have a a service that listens to gps GPRMC sentences on a com port. But I don't have this device on my testing computer. So I wanted to write a python script to simulate GPRMC sentences on the port while my other python script listens to the same port and parses.
Writing to a serial port does not leave a message on the serial port to be read by the same device. This just isn't how a serial port works and is not how most OSes are written to allow as a buffer behavior. What you really need is a virtual serial port.
Check out this section of a wikipedia article on COM port redirectors and see if any of it will fulfill your needs. Otherwise I recommend searching for COM port emulator, serial port virtualization, etc. until you find software that will work for your use case and operating system. This might be hard, especially if timing is important to your simulations.
Edit: To make this slightly more clear, let's talk about what the pySerial library is actually doing to communicate with python. pySerial is just communicating to the OS's API for the serial port. The OS will, generally, model this as a location to write information to and a location to read information from (buffered in just about all modern computing systems). What's important to understand is that from the point of view of the OS (how the serial port is modeled), the write location can ONLY be written to and the read location can only be read from. This may or may not be how the actual serial hardware interfaces with the machine, in most serial port hardware and interface designs that I've worked with, this is the case for the sake of simplicity and reduced cost. Because of this, you are down to two basic choices.
Give the OS a virtual serial port that you can read to AND write from somehow
Possibly simpler, put a null modem adapter on one of your computer's serial ports and, using a serial cable, connect the two ports. You can now have your service on one port and your simulated device script on the other.