It turns out that the string needed to be turned into a bytearray and to do this I editted the code to
Copyser.write("%01#RDD0010000107**\r".encode())
This solved the problem
Answer from Garvin on Stack OverflowIt turns out that the string needed to be turned into a bytearray and to do this I editted the code to
Copyser.write("%01#RDD0010000107**\r".encode())
This solved the problem
You have found the root cause. Alternately do like this:
Copyser.write(bytes(b'your_commands'))
python - Ser.write() function input - Raspberry Pi Stack Exchange
How to write to serial
Python: Writing to and Reading from serial port - Stack Overflow
serial - How to use pyserial to write two separate message? - Arduino Stack Exchange
Videos
I see from your print statements (Using print() as function) that you are using python3. In this case write and read functions handle bytes objects.
For example, instead of using:
ser.write("This is a test")
use
ser.write("This is a test".encode())
which converts "This is a test" to b'This is a test'.
This is one of the changes from Python2 to Python3.
I'm guessing that the problem has to do with python's changes in string handling. From Dive into python 3: "Python 2 had โstringsโ and โUnicode strings.โ Python 3 has โbytesโ and โstrings.โ
a piece of code who work with python to read rs232 just in case somedoby else need it
Copyser = serial.Serial('/dev/tty.usbserial', 9600, timeout=0.5)
ser.write('*99C\r\n')
time.sleep(0.1)
ser.close()
ser.read(64) should be ser.read(size=64); ser.read uses keyword arguments, not positional.
Also, you're reading from the port twice; what you probably want to do is this:
Copyi=0
for modem in PortList:
for port in modem:
try:
ser = serial.Serial(port, 9600, timeout=1)
ser.close()
ser.open()
ser.write("ati")
time.sleep(3)
read_val = ser.read(size=64)
print read_val
if read_val is not '':
print port
except serial.SerialException:
continue
i+=1
Sorry but I've been stuck on this 2 hours. I've checked every single stackoverflow link. I can send a string no problem, but when it comes to integers it just wont work.
Example. I call (Python): serial.write(b'20'). Arduino picks it up using (Arduino): Serial.read(). It gives out b'50\r\n'. I have tried everything to turn b'50\r\n' into an actual int. I have no idea what's happening. The code is literally as simple as those calls.
I will not use string because splitting a string then turning it into an int requires a page full of code.