Serial Reading with Python - python

I have a rectifier outputting voltage and amperage information every 3 seconds. I installed python 2.7 and pySerial without a hitch. I am trying to get the following code to read data being sent at baud 2400, 7 data bits, even parity, and one stop bit. Other RS232 programs get it without a hitch. When I start the program from cmd I get a blinking underscore. What am I doing wrong?
import time
import serial
ser = serial.Serial(
port='COM3',
baudrate=2400,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS
)
while True:
message = ser.readline()
print(message)
time.sleep(3)

Related

Interfacing Nextion with Raspberry Pi 3b+

I am having trouble interfacing my Nextion display via Raspberry Pi. I have a Nextion project that contains one text field called t0. And I want to change the text displayed from Raspberry.
Here is what I tried:
import serial
import time
import struct
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate =9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
k=struct.pack('B', 0xff)
time.sleep(1)
command = 't0.txt=\"hello\"'
ser.write(command.encode())
ser.write(k)
ser.write(k)
ser.write(k)
I am certain that connections are OK hence it is not hardware related.
Thank you for support.
Instead of this:
command = 't0.txt=\"hello\"'
I changed the command line to this:
command = 't0.txt="hello"'
Works for me good luck.

Write ASCII command with Pyserial

I am using a kunbus RevPi connect + to control a pump with a USB serial converter. To control it, I need to send ASCII command so I try to use the Pyserial library.
import serial
import time
ser = serial.Serial('/dev/ttyUSB2', baudrate = 9600, bytesize=8, parity='N', stopbits=1, timeout=1)
print (ser.portstr)
ser.write(b"1H[CR]")
I haven't error message but the pump doesn' start.
Can you help me please?

Serial missing data in RPI3 using Python

I'm using raspberry pi 3 and this code to send a request to a device and receive the response from.
#!/usr/bin/python3.7
import socket # Import socket module
import thread
import time
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
input = '5A03010d0a75'
print "Sending request... "+input
ser.write(input.decode("hex"))
print "Request sent."
output=""
while True:
output += ser.read(1)
#time.sleep(0.1)
print "Reading... "+output.encode('hex')
It handles the response but there are missing bytes, it should receive a 56 bytes length string instead of 53.
This is the output:
a5030119010000010001000a20120118180130090100020505030117501701051421000301040120010516039833630004060104c200007d
There are 3 missing bytes
The serial configuration is what the manufacturer says in the documentation.
This device works well with my other application made in Delphi.
EXTRA
This is a comparison from my delphi app and this py script:
Delphi app
A5030119010000010001000A20120118180130090100020505030117501701051421000301040120010516039833630004060104C200007D
Python script
a503011901000001000100 1201181801300901000205050301175017010514210003010401 010516039833630004060104c200007d
The solution was to set the max byte to the serial.read() method
This should be related to the device work behavior
#!/usr/bin/python3.7
#sudo python /home/testing.py
import serial
import time
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=5
)
input = '5A03010d0a75'
print "Sending request... "+input
ser.write(input.decode("hex"))
print "Request sent."
output=""
time.sleep(1)
while ser.inWaiting() > 0:
output += ser.read(10) #setting it to 10 will fix this problem
print "Reading... "+output.encode('hex')

Python send data Serial to Microcontroller STM32F4

So this is the script I am using to send each 2 seconds for four time the Time in microsecond to the microcontroller stm32f4 but unfoturnately it only sends some numbers(from 1-->4) which are not the same as when I do a print,it is like random numbers .
import time
import serial from datetime
import datetime from time
import gmtime, strftime
ser = serial.Serial(
port='/dev/ttyACM0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
ser.writeTimeout = 0
ser.isOpen()
TM1 = int(round(time.time()*1000000))
ser.write(str(TM1).encode())
#ser.write( str(TM1)+" \r\n")
time.sleep(2)
TM2 = int(round(time.time()*1000000))
ser.write(str(TM2)+" \r\n")
time.sleep(2)
TM3 = int(round(time.time()*1000000))
ser.write(str(TM3)+" \r\n")
time.sleep(2)
TM4 = int(round(time.time()*1000000))
ser.write(str(TM4)+" \r\n")
I cannot see anything obviously wrong at first sight. I have a almost identical snippet of code running here that works. My guess would be that the settings of the serial port do not match. Double check that the baudrate, parity and stopbit settings match.
Second guess would be a mess-up with encodings. Have you set your default encoding in python to utf-8? If so you could try
ser.write(str(TM1).encode('ascii'))
Posting the output you get would help as well.
Edit: to avoid the microcontroller skipping some characters. You could use a small function like this. (I used that to send commands to a sensor that had the same issues. When I sent a command like this `ser.write("start logging") it would receive something like "sart lgging".
def write_safe(cmd):
for x in cmd:
ser.write(x)
sleep(0.05)
ser.write('\r\n')

Error in working with pyserial module of python

import time
import serial
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyS0',
#port='/dev/ttyACM0',
baudrate=115200,
parity=serial.PARITY_ODD,
#stopbits=serial.STOPBITS_TWO,
#bytesize=serial.SEVENBITS
)
ser.isOpen() # returns true
time.sleep(1);
ser.write("some_command \n")
ser.close()
I have a embedded board. It has a serial port which is connected to my computer. I am running above script to access this serial port and run some board specific commands.
My problem
I open my serial port (using minicom in linux) separately and then run above script, It works. If I don't open serial port separately, Script doesn't work.
try
ser.write("some_command \n".encode())
alternatively try
ser.write(bytes(b"some_command \n"))

Categories

Resources