Python Forum
Trouble with Pyserial and Arduino
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with Pyserial and Arduino
#1
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
Reply
#2
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'.
Reply
#3
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
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,059 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,358 Mar-29-2020, 08:12 PM
Last Post: mRKlean

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020