I am trying to send the followings ASCII command:
close1
using PySerial, below is my attempt:
import serial
#Using pyserial Library to establish connection
#Global Variables
ser = 0
#Initialize Serial Port
def serial_connection():
COMPORT = 3
global ser
ser = serial.Serial()
ser.baudrate = 38400
ser.port = COMPORT - 1 #counter for port name starts at 0
#check to see if port is open or closed
if (ser.isOpen() == False):
print ('The Port %d is Open '%COMPORT + ser.portstr)
#timeout in seconds
ser.timeout = 10
ser.open()
else:
print ('The Port %d is closed' %COMPORT)
#call the serial_connection() function
serial_connection()
ser.write('open1\r\n')
but as a result I am receiving the following error:
Traceback (most recent call last):
, line 31, in <module>
ser.write('open1\r\n')
, line 283, in write
data = to_bytes(data)
File "C:\Python34\lib\site-packages\serial\serialutil.py", line 76, in to_bytes
b.append(item) # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer is required
Not sure how I would be able to resolve that. close1 is just an example of an ASCII command I want to send there is also status1 to see if my locks are open or close, etc.
Thanks in advance
This issue arises because Python 3 stores its strings internally as unicode but Python 2.x does not. PySerial is expecting to get a bytes or bytearray as a parameter to write. In Python 2.x the string type would be fine for this but in Python 3.x the string type is Unicode and hence not compatible with what pySerial write needs.
In order to use pySerial with Python 3 you need to use a bytearray. So your code would look need to look like this instead:
ser.write(b'open1\r\n')
Related
I am currently trying to use pyserial to read the values from my handheld tachometer, the specific model is the DT-2100.
I am using python 3 and my current code looks like this:
# Imports
import serial
import time
import io
# Coding section
# Setting Parameters
port = "COM3"
baud = 38400
data = []
info = 0
# Setting the port location, baudrate, and timeout value
ser = serial.Serial(port, baud, timeout=2)
# Ensuring that the port is open
if ser.isOpen():
print(ser.name + ' is open...')
# trying to read a single value from the display
#input("Ensure that the DT-2100 is turned on...")
info = ser.write(b'CSD')
ser.write(b'CSD')
info_real = ser.readlines()
print()
print("The current value on the screen is: ", info)
print()
print("The real value on the screen is: ", info_real)
This is what I get back after running the code:
COM3 is open...
The current value on the screen is: 3
The real value on the screen is: []
Process finished with exit code 0
The main issue is that I should be getting the value that is displayed by the tachometer, which for this test was 0, but between my two attempted methods I got 3 and nothing.
Any help is greatly appreciated.
The zip file you linked to contained an xls file which seemed to detail all the commands.
All the commands seem to be wrapped in: <STX> cmd <CR>, so you are missing those.
The CSD command would need to be like this: ser.write(b'\x02CSD\r')
Similarly the reply is also wrapped in the same way and you would need to remove those bytes and interpret the rest.
We are trying to communicate from Python to our Arduino, but are encountering an issue when writing to the serial port from python
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
time.sleep(2)
user_input = 'L'
while user_input != 'q':
user_input = input('H = on, L = off, q = quit' )
byte_command = user_input.encode()
print(byte_command)
ser.writelines(byte_command) # This line gives us the error.
time.sleep(0.5) # wait 0.5 seconds
print('q entered. Exiting the program')
ser.close()
The following is the error that we receive:
return len(data)
TypeError: object of type 'int' has no len()
Your code works on my computer. I think the function you're trying to use (writelines) was added not that long ago to pyserial so maybe you are running on an outdated version.
In any case, as far as I know, writelines is inherited from the file handling class and you don't really need to use it for what you're trying to do. Actually I don't think it's even well documented
Just change it to:
ser.write(byte_command)
If you prefer you can see what version of pyserial you have and/or update.
To check your version run: pip3 list | grep serial
If you don't have version 3.4 you can update with: pip3 install pyserial --upgrade
Considering how writelines works for files (see, for instance here) your error might actually be related to the core Python you have (for your reference I'm running Python 3.7.3).
writelines accepts a list of strings so you can't use it to send a single string. Instead use write:
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
time.sleep(2)
user_input = 'L'
while user_input != 'q':
user_input = input('H = on, L = off, q = quit')
byte_command = user_input.encode()
print(byte_command)
ser.write(byte_command) # This line gives us the error.
time.sleep(0.5) # wait 0.5 seconds
print('q entered. Exiting the program')
ser.close()
My raspberry pi is connected to microcontroller over serial pin. I am trying to read the data from the serial port. The script reads the data for few seconds. However, it terminates throwing following exception
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)
I have used following python code
#!/usr/bin/python
import serial
import time
serialport = serial.Serial("/dev/ttyAMA0", 115200, timeout=.5)
while 1:
response = serialport.readlines(None)
print response
time.sleep(.05)
serialport.close()
Here is the code you should be using if you are seriously trying to just transfer and print a file:
for line in serialport.readlines().split('\n'):
print line
------------------------------------------------------------
I believe you are having problems because you are using readlines(None) instead of readline() Readline() reads it a line at a time, and will wait for each one. If reading a whole file it will be slower than readlines. But readlines() expects a whole file all at once. It is obviously not waiting for your serial transfer speed.
--------------------------------------------------
My data-logging loop receives a line every two minutes and writes it to a file. It could easily just print each line like you show in the OP.
readine() waits for each line. I have tested it to wait up to 30 minutes between lines with no problems by altering the program on the Nano.
import datetime
import serial
ser = serial.Serial("/dev/ttyUSB0",9600) --/dev/ACM0 is fine
while True :
linein = ser.readline()
date = str(datetime.datetime.now().date())
date = date[:10]
time = str(datetime.datetime.now().time())
time = time[:8]
outline = date + tab + time + tab + linein
f = open("/home/pi/python/today.dat","a")
f.write(outline)
f.close()
Maybe changing to this approach would be better for you.
I am trying to communicate an Arduino using python. I was able to connect it using the serial module. This is the code:
import serial
while True:
print "Opening port"
arduinoData = serial.Serial("com7", 9600)
print "The port is open"
while (arduinoData.inWaiting()==0): #I wait for data
print "There is no data"
print "Reading data"
arduinoString = arduinoData.readline()
print arduinoString
It seems that is hanging when I want to read the data, in the line that says arduinoString = arduino.readline().
What could be the problem?
instead using the while loop inside of the main while loop you can use an if else statement. Also, to read the data you can use the read function with arduinoData.inWaiting() as the paramater like this : arduinoData.read(arduinoData.inWaiting()). I hope this code will help you:
arduinoData = serial.Serial("com7", 9600)
while True:
if arduinoData.inWaiting() > 0: # check if there is data available
print "Reading data"
arduinoString = arduinoData.read(arduinoData.inWaiting()) '''read and decode data'''
print arduinoString
else:
print "There is no data"
The structure of your code is strange. I had a similar issue by creating the Serial object in a function without making it global. Maybe you should put this line outside the loop :
arduinoData = serial.Serial("com7", 9600)
Also, your initialization seems a bit light. I usually use more parameters but it depends of your hardware.
ser = serial.Serial(
port = 'com4', \
baudrate = 19200, \
parity=serial.PARITY_NONE, \
stopbits=serial.STOPBITS_ONE, \
bytesize = serial.EIGHTBITS, \
timeout = 0.25)
A workaround for your readline() issue coud be using the read() function instead and checking if it contains data.
Hope it will help !
Alright, you are getting the AttributeError: 'Serial' object has no attribute 'ser' error because in reality ser does not exist in the arduinoData object. It's my fault because I was thinking of the class that I created in my program containing ser which is just the another serial object. To fix this just replace arduinoData.ser with arduinoData
To add on, you should probably declare arduinoData outside of the while loop. you should do this because every time the you create a serial object it takes time to connect to the Arduino. For this, your program might not be able to read the data.
I hope this answer will help you.
I wrote a Python script in which I can send fixed bytes through a COM port.
ser.write(bytes('0123456789',encoding='ascii'))
I want to parameterize the number of bytes that the script can send.
Can anyone please suggest me how I can do this?
Thanks.
By "random" I think you mean "arbitrary", i.e. how to send an arbitrary number of bytes...
You can generate a sequence of repeating digits like this:
>>> ''.join([str(i%10) for i in range(21)])
'012345678901234567890'
The number of bytes required can be passed to the script as a command line argument. Its value will be available in the sys.argv[] list.
import sys
import serial
try:
num_bytes = int(sys.argv[1])
except (IndexError, ValueError):
sys.stderr.write('Usage: {} num-bytes\n'.format(sys.argv[0]))
sys.exit(1)
ser = serial.Serial() # set up your serial port
digits = ''.join(str(i%10) for i in range(num_bytes))
num_written = ser.write(bytes(digits, 'ascii'))
Invoke the command like this:
$ python3 send_bytes.py 42