I am doing a project where I have to acquire data from an Arduino Uno connected to various sensors using Python on Windows (and eventually in Raspberry Pi 2 Model B).
As Python would constantly be listening to Arduino, I was thinking how should I let Python be always ready to read in a user input (any keyboard keys or even better, with a push-button if this is possible) after which Python will tell Arduino Uno to re-acquire data from all the sensors (i.e. refresh data from the sensors) and print it on the Python Console. I am currently using PyCharm with pySerial for communications between Arduino and Python. My code (without considering user input) is as of below:
import sys
import serial
import time
arduino = serial.Serial('COM3', 9600, timeout=1)
time.sleep(3) # wait for Arduino to initialize
def readData(): # reads inputs from Arduino
try:
datastring = arduino.readline()
print datastring
except:
pass
while True:
readData()
strin = 'p'
arduino.write(strin.encode()) # tell arduino a phase shifter setting has been finished
strin = 's'
arduino.write(strin.encode())
arduino.close()
time.sleep(0.5) # waits for 0.5 s
# print('Data to be transfered: %s'%ASCIIdata)
I understand that related topics have been posted before, however, I have tried the solutions online to no avail and I am finding it quite difficult to code as I am very new to Python. (I am reading documentation and examples and trying to code at the moment.)
Currently, I have done some research on this topic and have tried the following methods but to no avail:
Using msvcrt, getch and using nodelay(1) to allow my program to run even when no user input is received. I used "stdscr", and curses, to which I used "stdscr = curses.initscr()", however, in PyCharm, as it is an IDE, it returned an error "Redirection is not supported." My attempt at implementing this is as of below:
def readData(): # reads inputs from Arduino
try:
datastring = arduino.readline()
print datastring
except:
pass
while True:
stdscr = curses.initscr()
stdscr.nodelay(1)
user_input = stdscr.getch()
if user_input == -1:
readData()
I understand there is multithreading in Python and a number of leads lead me to think that it should be the way to go if I want to listen to user inputs continuously while still acquiring data from Arduino. May I ask if this is the correct direction in which I should pursue?
I have heard about select() and poll() though the former isn't supported on Windows. I will try select() when I port my program over to Raspberry Pi which runs on Linux.
May I ask if anyone can point me to a direction on how to implement a program that reads data continuously from Arduino in PyCharm, and but a user input will cause everything to stop (like an interrupt) and have Python (PyCharm) ask Arduino to pass it a set of fresh sensor data?
Thank you! :)
Related
I am writing a program to communicate between my laptop (Python) and an Arduino. The Arduino code, with which I have no issue, reads the serial data form my laptop and returns a reply. The code below works when I am calling the function which starts the serial communication from within the same file. However, when I import the file as a module in another file, using lal the same commands, it does not work!
To provide more detail, although Python thinks it has connected and even prints out the correct port number, it does not really connect. I know this because in the scenario that does work, when the serial communication is open, the Arduino IDE cannot speak to the Arduino as the Arduino is busy. However in the scenario which is not working, even after Python thinks it has opened serial communication, the Arduino IDE can still communicate with the Arduino.
Is there a way to pass the ser variable when called from a function in an imported module?
def connect():
for n in range(0,21):
try:
ser = serial.Serial('COM'+str(n), 115200 ,timeout=0.1)
status=1
port=n
return ser,port,status
except:
pass
time.sleep(0.05)
return 0, 0, 0
if __name__ == "__main__":
ser,port,status=connect()
n=0
while n<3:
num = input("Enter a word: ") # Taking input from user
ser.write(bytes(num, 'utf-8'))
time.sleep(0.05)
data = ser.readline()
print(data) # printing the value
n+=1
ser.close()
print('closed')
I have found the reason my code was not working! Notice in the code I posted, I use the input function to get a user input which is sent to the Arduino. This effectively results in a delay. In the scenario that was not working, I did not use the input function and so my code went straight from serial.serial to serial.write. The Arduino runs at 16 MHz and just couldn't keep up! All I needed to do was add a delay and now it works!
I'm having a weird issue with pyserial, using Python 3.6.9, running under WSL Ubuntu 18.4.2 LTS
I've set up a simple function to send GCODE commands to a serial port:
def gcode_send(data):
print("Sending: " + data.strip())
data = data.strip() + "\n" # Strip all EOL characters for consistency
s.write(data.encode()) # Send g-code block to grbl
grbl_out = s.readline().decode().strip()
print(grbl_out)
It sort of works, but every command I send is 'held' until the next is sent.
e.g.
I send G0 X0 > the device doesn't react
I send G0 X1 > the device reacts to G0 X0
I send G1 X0 > the device reacts to G0 X1
and so on...
My setup code is:
s = serial.Serial(com, 115200)
s.write("\r\n\r\n".encode()) # Wake up grbl
time.sleep(2) # Wait for grbl to initialize
s.flushInput() # Flush startup text in serial input
I can work around the delay for now, but it's quite annoying and I can't find anyone else experiencing the same. Any idea what could be causing this?
There might be a lot of problems here, but rest assured that the pyserial is not causing it. It uses the underlying OS's API to communicate with the UART driver. That being said you first have to test your code with real Linux to see whether WSL is causing it. I.e. whether a Linux and Windows UART buffers are correctly synced.
I am sorry that I cannot tell whether a problem is in your code or not because I do not know the device you are using, so I cannot guess what is happening on its end of communication channel. Have in mind that Windows alone can act weirdly in best of circumstances, so, prepare yourself for some frustrations here. Check your motherboard or USB2Serial converter drivers or whatever hw you are using.
Next thing, you should know that sometimes, communication gets confusing if timeouts aren't set. Why? Nobody really knows. So try setting timeouts. Check whether you need software Xon/Xoff turned on or not, and other RS232 parameters that might be required by the device you are communicating with.
Also, see what is going on with s.readline(), I wouldn't personally use it. Timeouts might help or you can use s.read(1024) with timeouts. I do not remember right now, but see whether pyserial supports asynchronous communication. If it does, you can try using it instead of standard blocking mode.
Also, check whether you have to forcefully flush the serial buffer after s.write() or add a sleep after it. It might happen that the device doesn't get the message but the read request is activated. As the device didn't receive the command it doesn't respond. After you send another command, IO buffer is flushed and the previous one is delivered and so forth. Serial communication is fun, but when it hits a snag it can be a real P in the A, believe me.
Ow, a P.S. Check whether the device sends "\r\n\r\n" or "\r\n" only, or "\r" or "\n" in response. s.readline() might get confused. For a start, try putting there two s.readline()s one after another and print out each output. If the device sends double EOL then the one s.readline() is stopping on the empty line and your program receives an empty response, when you send another command s.readline() goes through the buffer and returns a full line that is already there but not read before.
Here it goes. The code promissed in the comment. Big portions of it removed and error checks too.
It is a typing terminal for using PyS60 Python console on Nokia smartphones in the Symbian series via bluetooth. Works fantastically.
from serial import *
from thread import start_new_thread as thread
from time import sleep
import sys, os
# Original code works on Linux too
# The following code for gettin one character from stdin without echoing it on terminal
# has its Linux complement using tricks from Python's stdlib getpass.py module
# I.e. put the terminal in non-blocking mode, turn off echoing and use sys.stdin.read(1)
# Here is Win code only (for brevity):
import msvcrt
def getchar ():
return msvcrt.getch()
def pause ():
raw_input("\nPress enter to continue . . .")
port = raw_input("Portname: ")
if os.name=="nt":
nport = ""
for x in port:
if x.isdigit(): nport += x
port = int(nport)-1
try:
s = Serial(port, 9600)
except:
print >> sys.stderr, "Cannot open the port!\nThe program will be closed."
pause()
sys.exit(1)
print "Port ready!"
running = 1
def reader():
while running:
try:
msg = s.read()
# If timeout is set
while msg=="":
msg = s.read()
sys.stdout.write(msg)
except: sleep(0.001)
thread(reader,())
while 1:
try: c = getchar()
except Exception, e:
running = 0
print >> sys.stderr, e
s.write('\r\n\x04')
break
if c=='\003' or c=='\x04':
running = 0
s.write('\r\n\x04')
break
s.write(c)
s.close()
pause()
I currently have an arduino code, which is connected to three sensors: temperature, pressure and humidity. I would like to make a code in python that of an order (by int or strg), this sends the type of sensor that I want to read, example: if I enter by keyboard 1, it constantly sends me temperature data; if income 2, send me pressure data; and thus be able to enter any digit at any time.
Pd: Sorry my bad english,I don't know if i could explain my problem
I have a similar code in arduino with switch case, and it works perfectly. but I can not make it work in python since when I put raw_input (), the program stops waiting for input and stops reading the sensor data.
Python
import serial
import time
ser=serial.Serial('/dev/ttyUSB0',baudrate=115200)
while 1:
ser.setDRT(False)
#c=raw_input()
#ser.write(c)
med=a.readline()
print med
this just works fine to read data from one sensor type assigned by default
If you have tasks, that needs to run in parallel, you can use threads. One thread gets the sensor data and the other waits for input.
Python has a very easy to use builtin module for threading.
Official python docs: https://docs.python.org/3.7/library/threading.html
Tutoialspoint: https://www.tutorialspoint.com/python/python_multithreading.htm
A very simple implementation example might look like this:
import threading
def wait_input():
while True:
user_input = input()
# do something with user_input
def get_sonsordata()
while True:
med=a.readline()
print(med)
input_thread = threading.Thread(target=wait_input)
input_thread.start()
sensor_thread = threading.Thread(target=get_sonsordata)
sensor_thread.start()
I'm having a strange problem using pyserial to comunicate to a microcontroller device of my own design.
What I wanto to do is send a command to the uC and receive and process the answer.
The problem is that when I use readline() it always returns on timeout, instead of when it detects a "\n". If I set timeout to cero, readline() returns empty.
import serial
import time
ser=serial.Serial()
ser.baudrate=9600
ser.timeout=5
ser.port=0
ser.open()
while True :
cmnd=raw_input(">: ")
if cmnd == "quit":
break
ser.write(cmnd+"\n")
ck=time.clock()
resp=ser.readline()
ck=time.clock()-ck;
print ">>"+resp
print "tiempo: "+str(ck)
I did find a solution of sorts: If I call time.sleep(0.1) right before calling readline(), it returns immediatelly (+ the 0.1 seconds of the sleep command). I can live with this, but I'd like to know if there is a more "elegant" solution for this problem. Here is how the code looks with the workaround:
import serial
import time
ser=serial.Serial()
ser.baudrate=9600
ser.timeout=5
ser.port=0
ser.open()
while True :
cmnd=raw_input(">: ")
if cmnd == "quit":
break
ser.write(cmnd+"\n")
ck=time.clock()
time.sleep(0.1)
resp=ser.readline()
ck=time.clock()-ck;
print ">>"+resp
print "tiempo: "+str(ck)
I'm using windows 8.1 and python 2.7. Also, I'm not testing it in real hardware, but using a virtual pair of serial ports created with "Virtual Serial Ports" by HDD software and simulating the microcontroller on Proteus ISIS VSM.
I'm trying to build a car (Wild Thumper) that i can drive from my Raspberry Pi. Currently i'm using my Raspberry Pi over SSH. It should send to data to my Arduino so it knows when it has to go forward or when to turn.
I've tried making scripts that get called by jQuery (apache on Pi) and send an integer over the serial port but it requires a delay and this is not ideal. (example forwardStart.py:)
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.open()
# here a delay is needed
ser.write('4') # go forward
ser.close()
To solve this i tried looking for a single python script that read my keyboard and send the correct integer. However, all keylisteners require display and can't be used over SSH.
Can anybody help me with the Python script or another idea that would works?
Thanks!
You should start reading from here. The idea would be something like
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.open()
# here a delay is needed
try:
while 1:
try:
key = sys.stdin.read(1) # wait user input
actionKey = key2action(key) # translate key to action
ser.write(actionKey) # go forward
except IOError: pass
finally:
ser.close()
Note: this code will fail, it's more like pseudo-code to illustrate the idea.