I try to make communication between my device and python code via COM4 a communication port: pyserial communication.
So at first I try to send and write a hello then to read the output of my device,
But the problem for me is how to read the hello that is sent firstly. I mean that I want to read the string hello on COM4 from my device
import serial
ser = serial.Serial(
port='COM4',\
baudrate= 230400) # open serial port
print(ser.name) # check which port was really used
#ser.write(b'hello\n') # write a string
#str=ser.readline()
while True:
print(ser.read(30).decode())
ser.close() # close ports
The code of my device is written in C language.
I would be very grateful if you could guide me please.
This process differs based on the operating system. Here's a good library that supports Linux and Windows:
http://www.teuniz.net/RS-232/
Related
I'm trying to test writing messages to serial port using pyserial and reading it again using two usb to serial adapters connected back to vack "USB-Serial --> Serial-USB" to verify it is writing to the serial port as it is meant for communication with hardware,
so I have open console witch is reading all the time
import serial
port = 'COM6'
read_ser = serial.Serial(port)
while True:
x=read_ser.read()
print(x)
and for writing I use
import serial
port = 'COM5'
ser = serial.Serial(port)
# ser.write(str.encode('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'))
ser.write('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'.encode())
And nothing is shown in the reading console
I tried both adapters and read from external serial hardware successfully.
I'm using python 3.7 on windows 10
I figured it out
The problem was with the pin layout as I used gender changer to connect the two serial adapters
using serial cable instead solved the problem.
import serial
while True:
device = serial.Serial('/dev/ttyACM0')
data = device.readline()
print(data + '\n')
I have a device that communicates to a raspberry pi using USB port and pyserial in python. What should I do so that when user inputs "reset" on the command line the device will be reset?
Late reply, but you would need to create some handler code on the device that calls NVIC_SystemReset() whenever it sees reset come in over the serial port.
You haven't specified what "a device" means. Assuming the device is an Mbed device connected to the Raspberry pi and your Python code is running on the Raspberry pi.
Most Mbed devices can be reset by sending BREAK condition on the serial interface. Check pySerial API for this https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.send_break
Also, check how it is done in this tool for Python 2.7 and 3.x https://github.com/ARMmbed/htrun/blob/master/mbed_host_tests/host_tests_plugins/module_reset_mbed.py#L60
I am trying to interface with my com ports, specifically an XBee connected to thi using this code.
from xbee import XBee
from serial import Serial
PORT = 'COM3'
BAUD = 9600
ser = Serial(PORT, BAUD)
xbee = XBee(ser)
# Send the string 'Hello World' to the module with MY set to 1
xbee.tx(dest_addr='\x00\x01', data='Hello World')
# Wait for and get the response
print(xbee.wait_read_frame())
ser.close()
However, this error keeps arising.
SerialException: could not open port 'COM3': WindowsError(5, 'Access is denied.'). It goes away when I restart my computer, buts it keeps returning. I'd prefer to understand why its happening so I don't need to keep restarting my computer. Would really appreciate any help, thanks. I am working through the IDLE interface with python 2.7 just in case that is relevant.
A serial port can be "open" in only one application at a time. Once application "A" opens the port, application "B" will get an Access Denied error when it tries to open the same port. In your case, you need to figure out what other application is holding the port and close it first.
I am relatively new to Python. I wrote a script and need to add triggers that need to be send via an usb serial port to another pc. The problem is that the triggers (in this code example the 2) never show up on the software on the other pc. When I check it with the print() command, it does print a value, but the printed value is the same number for two different triggers. I have read other posts, I searched the internet, and I tried various things, but I didn't manage to resolve this iusse. This is the code I use for interfacing with the serial port (COM3).
#this part of code is defined at the beginning:
import serial
ser = serial.Serial(port=2, baudrate=9600)
ser.close()
#this part of the code later on to interface with the serial port:
ser.open()
ser.write(chr(2))
ser.close()
Maybe anyone here has any suggestions on where the problem could be? Thanks!
If you haven't already, check that the port settings are correct, 9600,8,N,1 for example. These must match the settings of the remote serial port.
It might be useful to check that the serial connection does work with a terminal emulation program such as minicom (linux), or PuTTY (Windows). Once you have verified that a connection can be made and data transferred, you can be certain that you are connecting to the correct local port, that the port settings are correct, and that your serial cable is working properly.
I want to emulate an Arduino serial communication in Windows. I wrote this pySerial script to represent the connection:
from serial import Serial
from time import sleep
serial_conn = Serial(<some port>)
serial_conn.baudrate = 9600
for i in range(1,10):
serial_conn.write(<dummy data>)
sleep(1)
The problem is the following: I tried to use available serial ports (like COM1 or COM3 for example) but I can't sniff the port with a serial monitor tool. It's because I need hardware to open the port? Or maybe the problem are the tested ports? (maybe Windows uses the COM1 for comm as Linux uses the first serial too). Should I try with a virtual serial ports tool? If that's the point, can you recomend me someone and the usage?
In Windows hardware and virtual serial ports have the same enumeration scheme, so they will be COM.
The problem is that only one program at time (theorically) can use a Serial port, so if the port is used by your Python program, it won't be available to the terminal.
You should setup a fake COM, and this mean a custom driver... too much difficult.
Socket, file and standard input can be read/written one byte a time, so you can test your parser using them.