Jan-22-2017, 10:42 AM
Hello
I have been working on a program that works as an arduino communicator.
The idea is for the arduino to write to a file on the computer instead of an SD card.
For some reason, the def statement is returning none, even though it clearly says to return a variable that is not empty.
I am running python version 3.5.2 on windows 7 32bit.
Here is the Python script:
I have been working on a program that works as an arduino communicator.
The idea is for the arduino to write to a file on the computer instead of an SD card.
For some reason, the def statement is returning none, even though it clearly says to return a variable that is not empty.
I am running python version 3.5.2 on windows 7 32bit.
Here is the Python script:
debug = True complete = "" import serial,time def fileRelay(): fileName = str(chipRead(ser,debug,complete)) print(fileName) Mode = str(chipRead(ser,debug,complete)) print(Mode) sentence = str(chipRead(ser,debug,complete)) print(sentence) file = open(fileName,Mode) file.write(sentence) file.close() print("FileWrite relay complete!") return def chipRead(ser,debug,complete): info = str(ser.read()) info = info[2] if (info != "EOL"): if (info != "0"): complete = complete + info; chipRead(ser,debug,complete) else: complete = str(complete) if (debug): print(complete) return complete else: complete = str(complete) if (debug): print(complete) return complete print("\tStarting arduino...") try: ser = serial.Serial("COM7",9600) except: print("Could not find arduino") input() quit() time.sleep(2) print("\tArduino Initialised. Executing script") while True: print("\n\tListening on port COM7\n") fileRelay()and here is the arduino code (Only being used to test):
char Mode[] = {"w0"}; char fileName[] = {"arduino.test0"}; char out[] = {"This is a test from the arduino IDE test program0"}; void setup() { Serial.begin(9600); } void loop() { delay(3000); Serial.print(fileName); Serial.print(Mode); Serial.print(out); }The output i get is:
Error: Starting arduino...
Arduino Initialised. Executing script
Listening on port COM7
arduino.test
None
w
None
This is a test from the arduino IDE test program
None
Traceback (most recent call last):
File "C:/Users/Andrey/AppData/Local/Programs/Python/Python35-32/My Codes/arduino file.py", line 56, in <module>
fileRelay()
File "C:/Users/Andrey/AppData/Local/Programs/Python/Python35-32/My Codes/arduino file.py", line 17, in fileRelay
file = open(fileName,Mode)
ValueError: invalid mode: 'None'