I am communicating in my python code to arduino, transmitting a number of bytes serially. The below code is my main function that works until after the first or second 'ser.wrte' function. Confusingly the program works correctly on mac with no errors but has the above issue with any laptop running windows (versions 7 and 10 have been tested with no success) is there any code-based reason for this, or is it due to a windows related serial port issue?
#Communicates to arduino via serial port
def pump():
print('****************')
if len(seq) == 0:
print("EmptySeqError: please store patterns in the sequence before sending it!")
return
#######################
port = "COM17"
macport = "/dev/cu.usbmodem1411"
baudrate = 9600
#######################
try:
ser = serial.Serial(port, baudrate, timeout=0.1) #, write_timeout=0.1)
except serial.SerialException:
print('SerialException: cannot create serial object')
return
else:
if ser.isOpen():
print('serial port {} is opened successfully!'.format(ser.name))
for json in seq:
MSB = int(json['Hex Code MSB'], 16)
LSB = int(json['Hex Code LSB'], 16)
V = int(json['Voltage'], 16) #For voltage transmission
Vlsb = V & 0xff #Voltage LSB
Vmsb = (V>>8) & 0xff #Voltage MSB
print(V, 'in hex =', hex(V))
print(Vlsb, 'in hex =', hex(Vlsb))
print(Vmsb, 'in hex =', hex(Vmsb))
#X = int(json['Xhigh'], 16)
#Y = int(json['Ybase'], 16)
#Z = int(json['Zsec'], 16)
HD = int(json['High Delay'], 16)
LD = int(json['Low Delay'], 16)
print("**************")
print("(MSB/LSB) number of bytes written: ", ser.write(bytes([MSB, LSB])))
print("(Voltage) number of bytes written: ", ser.write(bytes([Vmsb,Vlsb])))
print("(HD/LD) number of bytes written: ", ser.write(bytes([HD, LD])))
print("serial is closing...")
sys.stdout.flush()
ser.close()
#End of sending
Related
I have two usb to serial cables connected to each other on 2 different usb ports on the same pc.
The ground and A and B cables are all connected to eachother. the voltage cable is isolated.
When I send ASCII characters using a serial terminal i can communicate both ways.
But when I run a python script for a modbus slave and a master It works sometimes and sometimes it doesnt. I cannot find a pattern in when it works and when not.
I can see when it works all the sent bytes are received by the slave but when it doesnt work the slave receives atleast 1 byte but not the rest.
Then it obviously gives a CRC error and crashes.
How come some bytes are missing through modbus and not ASCII serial terminals?
Is this a hardware problem? How can I find out? I didnt connect extra resistors.
I can show the code for the slave and master, Thanks
As you can see I have three USB-to-serial cables connected in parallel, the orange and yellow cables are the A and B cables and the black ones are the grounds cables.
The cables are these http://www.ftdichip.com/Products/Cables/USBRS485.htm
This is the slave code but I tried different libraries and got the same results so I doubt its the code:
#!/usr/bin/env python
# -*- coding: utf_8 -*-
"""
Modbus TestKit: Implementation of Modbus protocol in python
(C)2009 - Luc Jean - luc.jean#gmail.com
(C)2009 - Apidev - http://www.apidev.fr
This is distributed under GNU LGPL license, see license.txt
"""
import sys
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serial
#PORT = 0
PORT = '/dev/ttyUSB2'
def main():
"""main"""
logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")
#Create the server
server = modbus_rtu.RtuServer(serial.Serial(port=PORT, baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0))
try:
logger.info("running...")
logger.info("enter 'quit' for closing the server")
server.start()
slave_1 = server.add_slave(1)
slave_1.add_block('0', cst.HOLDING_REGISTERS, 0, 100)
while True:
cmd = sys.stdin.readline()
args = cmd.split(' ')
if cmd.find('quit') == 0:
sys.stdout.write('bye-bye\r\n')
break
elif args[0] == 'add_slave':
slave_id = int(args[1])
server.add_slave(slave_id)
sys.stdout.write('done: slave %d added\r\n' % (slave_id))
elif args[0] == 'add_block':
slave_id = int(args[1])
name = args[2]
block_type = int(args[3])
starting_address = int(args[4])
length = int(args[5])
slave = server.get_slave(slave_id)
slave.add_block(name, block_type, starting_address, length)
sys.stdout.write('done: block %s added\r\n' % (name))
elif args[0] == 'set_values':
slave_id = int(args[1])
name = args[2]
address = int(args[3])
values = []
for val in args[4:]:
values.append(int(val))
slave = server.get_slave(slave_id)
slave.set_values(name, address, values)
values = slave.get_values(name, address, len(values))
sys.stdout.write('done: values written: %s\r\n' % (str(values)))
elif args[0] == 'get_values':
slave_id = int(args[1])
name = args[2]
address = int(args[3])
length = int(args[4])
slave = server.get_slave(slave_id)
values = slave.get_values(name, address, length)
sys.stdout.write('done: values read: %s\r\n' % (str(values)))
else:
sys.stdout.write("unknown command %s\r\n" % (args[0]))
finally:
server.stop()
if __name__ == "__main__":
main()
EDIT: I just changed to baudrate to 115200 and the errors are way less. Is there anything else i can do?
I need to communicate and pass values to a serial connected device using RS232 Protocol. I need to pass commands through the 8 bytes of data and then be able to receive the response afterwards.. Im not sure how to write this in PySerial so if anyone can help out it would be great (9600 Baud, 8 data bits, No parity, and 1 stop bit.)
import serial
ser = serial.Serial('/dev/ttyUSB0') # open serial port
print(ser.name) # check which port was really used
ser.write(b'hello') # write a string
ser.close() # close port
The Timer Manager Command structure consists of one start byte, one command byte, five bytes of data, and a one byte checksum. Each message packet is formatted as follows:
BYTE 0 BYTE 1 BYTE 2 BYTE 3 BYTE 4 BYTE 5 BYTE 6 BYTE 7
200 COMMAND DATA1 DATA2 DATA3 DATA4 DATA5 CK SUM
Im looking to receive the following back from the machine:
If command was successfully received, the Timer Manager will respond with:
BYTE 0 BYTE 1 BYTE 2
6 0 6
The actual data that I want to send is this
Data i need to pass to the timer is structured this way:
BYTE 0 BYTE 1 BYTE 2 BYTE 3 BYTE 4 BYTE 5 BYTE 6 BYTE 7
200 31 4 0 0 0 0 235
Is this passed via bytearray ?
ser.write( bytearray(200,31,4,0,0,0,0,235) );
I generally have something like this to do binary IO over a serial port:
from timeit import default_timer as clk
from serial import Serial, SerialException
class TimeManager(object):
def __init__(self, port, baudrate=9600):
self.ser = Serial(port, baudrate=baudrate)
self.ser.open()
self.ser.flushInput()
self.ser.flushOutput()
def send(self, tx):
tx = bytearray(tx)
try:
self.ser.write(tx)
self.ser.flush()
except SerialException as e:
if e.args == (5, "WriteFile", "Access is denied."):
# This occurs on win32 when a USB serial port is
# unplugged and replugged. It should be fixed by
# closing and reopening the port, which should happen
# in the error handling of our caller.
raise IOError(errno.ENOENT, "Serial port disappeared.",
self.ser.portstr)
else:
raise
def receive(self):
rx = bytearray()
delay = 10e-3 # s
timeout = 1 # s
end_time = clk() + timeout
while True:
time_remaining = end_time - clk()
if time_remaining < 0:
break
rx += self.ser.read(self.ser.inWaiting())
if 0 in rx:
break
time.sleep(delay)
if time_remaining <= 0:
raise IOError(errno.ETIMEDOUT, "Communication timed out.")
return rx
tm = TimeManager("/dev/ttyS0")
My device sends null terminated messages (the if 0 in rx: line). You'd have to figure out a similar condition for your messages.
First of all, due to the fact that you use RS232, you must set the ASCII characters you wanna send in variables. And then, when you got in a variable all the sentence you want to send, sent it decoding it into bytes.
It would be something like this.
def sendserial(sendstring):
ser.port(yourport)
try:
ser.open()
except Exception as e:
flag=1
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
ser.write(bytes(sendstring,'iso-8859-1'))
#iso 8859-1 is the only encode that works for me
time.sleep(0.5)
numOfLines = 0
while True:
resp = bytes.decode(ser.readline())
result = ord(str(response))
if result == ord(ACK)
#I set previously the ACK var to the ASCII symbol that the machine returns
response = 'Y'
else:
response = 'N'
numOfLines = numOfLines +1
if (numOfLines>=1):
break
ser.close()
except Exception as e1:
print('Communication error...:' + str(e1))
else:
pass
return(response)
I'm attempting to read the values of some GPIO. Here's the code:
import serial
import codecs
import time
ser = serial.Serial(port = 'COM4', baudrate = 9600, \
parity = serial.PARITY_NONE, \
stopbits = serial.STOPBITS_ONE, \
bytesize = serial.EIGHTBITS, \
timeout = 0, \
)
print('connected to: ',ser.name)
ser.close()
def SSend(input):
ser.write(codecs.decode(input, "hex_codec")) #send as ASCII
print('sent: ', input)
def ReadIO():
#open the port
try:
ser.open()
except:
print('error opening serial port')
exit()
#flush the buffers
ser.flushInput()
ser.flushOutput()
#write data to read from GPIO
#causes SC18IM to return a byte containing each of the 8 I/O values
SSend(b'4950')
time.sleep(0.1) #allow time for the data to be received
#read the data
serialData = False
serialData = ser.readline()
ser.close()
return serialData
while 1:
print(ReadIO())
time.sleep(0.5)
This prints the following:
sent:
b'4950'
b''
(I am expecting back either 0x00 or 0x20 instead of an empty byte)
I know my hardware is good as is what I'm sending because it get back what I expect when using Realterm and have successful write commands in my script elsewhere.
I had some luck using this
#read the data
serialData = False
for c in ser.readline():
print('in loop')
print(c)
serialData = c
ser.close()
However, I don't really understand why it worked and it only appears to work intermittently.
Thanks for reading.
readline() assumes that there is some end-of-line symbol, like \n or \r. You should read data byte-wise:
serialData = ''
while ser.inWaiting() > 0:
c=ser.read(1)
# or c=ser.read(1).decode('latin1')
serialData += c
I'm trying to send strings between two Raspberry Pis via UART. So far, one Pi is able to transmit a string and the second pi is only able to receive. I'm trying send a "command" from one Pi to the other, then the second Pi will receive the "command" and then send "sensor data" back to the first Pi. However, this doesn't seem to work. I'm unable to both send and receive data.
Any help would be greatly appreciated.
The Raspberry Pi that is sending the command:
import serial
ser = serial.Serial(
port = '/dev/ttyAMA0',\
baudrate = 115200,\
bytesize = serial.EIGHTBITS,\
timeout = 0)
dat = ''
while True:
#asks user for input
command = raw_input()
#terminates command with null
ser.write(command + '\0')
#reads data per char
for c in ser.read():
#appends string
dat += c
#terminates at null char
if c == '\0':
print(dat)
dat = ''
break
ser.close()
This RPi that receives command and then sends sensor data:
import serial
ser = serial.Serial(
'/dev/ttyAMA0' ,\
baudrate = 115200 ,\
bytesize = serial.EIGHTBITS ,\
timeout = 0)
dat = ''
sen1 = 'sen1\x00'
sen2 = 'sen2\x00'
com1 = 'hello'
com2 = 'this thing works!'
com3 = 'error!'
while True:
#reads data per char
for c in ser.read():
#appends string
dat += c
#terminates at null char
if c == '\0':
#decides which sensor to choose
if dat == sen1:
print(com1)
ser.write(com1 + '\0')
dat = ''
break
elif dat == sen2:
print(com2)
ser.write(com2 + '\0')
dat = ''
break
else:
print(com3)
ser.write(com3 + '\0')
dat = ''
ser.close()
The cause is most likely your zero timeout. You've set read() to non-blocking as a result, so your sender Pi's for loops will most likely read nothing (or only a byte) and will terminate before everything's arrived. Also iterating over ser.read() doesn't really make sense since it returns only a byte anyway by default.
Increase timeout to some non-zero value.
ser = serial.Serial(
'/dev/ttyAMA0' ,\
baudrate = 115200 ,\
bytesize = serial.EIGHTBITS ,\
timeout = 0.1)
Assuming you want the response to a command received, before another command is sent, this should work for the sender:
while True:
#asks user for input
command = raw_input()
#terminates command with null
ser.write(command + '\0')
dat = []
lastRead = time.time()
while time.time() - lastRead < 0.1:
while ser.inWaiting() > 0:
dat.append(ser.read())
lastRead = time.time()
if dat and (dat[-1] == '\0'):
print(''.join(dat[:-1]))
If 100ms or more passes before another byte is available, the while loop breaks and the last received byte is checked to know if its a null byte. Something similar should work for your receiver:
while True:
dat = []
lastRead = time.time()
while time.time() - lastRead < 0.1:
while ser.inWaiting() > 0:
dat.append(ser.read())
lastRead = time.time()
dat = ''.join(dat)
# begin your comparisons here
I'm sending a list of values (e.g. 80,539,345,677) from Arduino to a Python app running on my RPi. I have not been successful in extracting the values and assigning them to respective variables or objects in the app.
Here's my code:
def read_values():
#if DEBUG:
print "reading arduino data"
ser = serial.Serial('/dev/ttyUSB0', 9600)
print "receiving arduino data"
ser_line = ser.readline()
print ser_line
ser.close()
ser_list = [int(x) for x in ser_line.split(',')]
ambientLight = ser_list[1]
print ambientLight
return ambientLight
What I'm getting from Python is:
reading arduino data
receiving arduino data
80,477,82,2
Traceback (most recent call last):
File "serialXivelyTest4c.py", line 77, in <module>
run()
File "serialXivelyTest4c.py", line 63, in run
ambientLight = read_values()
File "serialXivelyTest4c.py", line 27, in read_values
ser_list = [int(x) for x in ser_line.split(',')]
ValueError: invalid literal for int() with base 10: '8254\r80'
You can see that I'm getting values, but that they're being truncated. Can anyone please tell me where I'm going wrong here. Thanks so much.
I've never used an Arduino but here's how I read from serial with a different board. I used serial.
import streamUtils as su # see below
ser = su.connectPort("/dev/tty.SLAB_USBtoUART") # make sure you have the right port name
data = ""
while True:
try:
data = data + ser.read(1) # read one, blocking
time.sleep(1) # give it time to put more in waiting
n = ser.inWaiting() # look if there is more
if n:
data = data + ser.read(n) # get as much as possible
# I needed to save the data until I had complete
# output.
if data:
# make sure you have the whole line and format
else:
break
except serial.SerialException:
sys.stderr.write("Waiting for %s to be available" % (ser.name))
sys.exit(1)
sys.stderr.write("Closing port\n")
ser.close()
Here's the streamUtils.connectPort():
import serial
def connectPort(portname):
# connect to serial port
ser = serial.Serial()
ser.port = portname
ser.baudrate = 9600
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.bytesize = serial.EIGHTBITS
ser.timeout = 15 # need some value for timeout so the read will end
try:
ser.open()
except serial.SerialException:
sys.stderr.write("Could not open serial port %s\n" % (ser.name))
sys.exit(1)
return (ser)