Mar-17-2018, 06:28 PM
Hello all
I'm totally new to python and try to walk before I can crawl.
The intention of the below code is to save serial input (received from an Arduino) to a file for as long as the application runs. The application can be interrupted by a keypress.
The question is obviously where I went wrong? Running this on a Windows 8 system, ActivePython 3.4.1.
The keyboard library was downloaded from https://github.com/boppreh/keyboard, extracted and copied to C:\Python34\Lib\site-packages.
I'm totally new to python and try to walk before I can crawl.
The intention of the below code is to save serial input (received from an Arduino) to a file for as long as the application runs. The application can be interrupted by a keypress.
import sys
import serial
import keyboard
try:
ser = serial.Serial('COM4', baudrate=57600)
ser.flushInput()
while True:
ser_bytes = ser.readline()
print(ser_bytes)
file = open("testfile.csv", "a")
file.write(str(ser_bytes))
file.close()
if keyboard.is_pressed('esc'):
break;
ser.close
except:
print("Unexpected error:", sys.exc_info()[0])
print("Unexpected error:", sys.exc_info()[1])The output (containing the error)Output:C:\Users\sterretje\Documents\3_Development_Python>C:\Python34\py.exe serial_test.py
b'a,bc,def\r\n'
Unexpected error: <class 'AttributeError'>
Unexpected error: 'module' object has no attribute 'is_pressed'The arduino currently sends 'a,bc,def\r\n', the 'b' is probably left over rubbish from arduino's bootloader or something python specific; that's currently not relevant.The question is obviously where I went wrong? Running this on a Windows 8 system, ActivePython 3.4.1.
The keyboard library was downloaded from https://github.com/boppreh/keyboard, extracted and copied to C:\Python34\Lib\site-packages.

)