Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return not working
#1
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:
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'
Reply
#2
Looks like Mode is not set. You can see it in the second print().
I don't recognize the programming language in the Arduino code and can't say way is that
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
I don't really understand the arduino code, but fileRelay explicitly returns None. That's what a bare return does, it returns None.

chipRead returns a value, but not always. If info is neither 'EOL' or '0', then chipRead returns None, because there is no return statement. You probably want to return the value of your recursive call to chipRead in that case.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Through some trial and error, i managed to fix the problem.
Ichabod801 was on the right track. The error was how i returned the answer in chipRead()
For some strange reason, if you loop a def statement by calling it from itself, it returns nothing.

The working code is now:

def chipRead(ser,debug,complete,var):
   while(var == False):
       info = str(ser.read())
       info = info[2]
       
       if (info != "0"):
           complete = complete + info;
       else:
           complete = str(complete)
           if (debug):
               print("chipRead: Passing sentence: \""+complete+"\" To calling function...")
           var = True
   return complete
Thanks for all of your help in fixing this problem!
Reply


Forum Jump:

User Panel Messages

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