Python Forum
Trouble with Pyserial and Arduino - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trouble with Pyserial and Arduino (/thread-13261.html)



Trouble with Pyserial and Arduino - LiTing - Oct-07-2018

My Python Programme is sending the wrong data to my arduino. Do anyone have any ideas?
The code is as follows:
s.write(str.encode('S'))

I want to send a letter R via bluetooth to my arudino


RE: URGENT HELP FOR PYTHON BLUETOOTH PYSERIAL - j.crater - Oct-07-2018

Where to start...
Don't use all caps when writing a thread title!
If the line you posted is all the code you have, it sure won't work. If you have more, post complete runnable code in Python code tags (you can find help here).
Which letter do you want to send? You have 'S' in the str.encode(), but said you want to send 'R'.


RE: URGENT HELP FOR PYTHON BLUETOOTH PYSERIAL - LiTing - Oct-07-2018

Sorry, I want to send capital R to my arduino.

from tkinter import *
import serial
import time

s=serial.Serial('COM14', 9600, timeout=1)
    time.sleep(5)
    print("connected!")
    s.write(str.encode('R')) 
    print("Sent Message!")
These are my codes


RE: URGENT HELP FOR PYTHON BLUETOOTH PYSERIAL - Larz60+ - Oct-07-2018

Try communicating to device using putty.
Make sure you have same baudrate (you are using 9600) on both sides of connection, and proper com port number (you have comport 14 now which is unusual unless many devices already connected), uoy may also (although unusual) have to configure parity and stop bits.
This will isolate the problem to either improper hardware connection or code.
If you can't get it to work with putty ( https://www.putty.org/ ) you will never get it to work with python.
This is an important step to take when dealing with serial communications.
One final note, at some point you should capture ValueError and SerialException

So your code would look like:
from tkinter import *
import serial
import time
import sys

try: 
    s=serial.Serial('COM14', 9600, timeout=1)
    time.sleep(5)
    print("connected!")
    s.write(str.encode('R')) 
    print("Sent Message!")
except ValueError:
    print('Value Error message here')
except SerialException:
    print('Serial Exception thrown')
I also noticed that your code line:
s=serial.Serial('COM14', 9600, timeout=1)
was improperly indented (which would have shown up is you use code tags!