TypeError: cannot unpack non-iterable NoneType object

I try to use python to do data logging from an arduino nano. The code works only for several loops, then a error poped up. I am wondering what cause the problem.

The error message:

('5', 1874.8682350429337)
------
('3', 1167.271553440443)
------
('3', 1233.2207809135466)
------
Traceback (most recent call last):
  File "/Users/renzha/Library/Application Support/JetBrains/PyCharmCE2021.1/scratches/stream arduino and save files.py", line 262, in <module>
    t, V = request_single_voltage (arduino)
TypeError: cannot unpack non-iterable NoneType object

Process finished with exit code 1

python code

while True:
    for n in range (0, 11):
        names = ['test', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'end' ]
        for name in names:
            
            VOLTAGE_REQUEST = 200


            arduino = serial.Serial (port, baudrate=115200)


         
            arduino.write (bytes ([VOLTAGE_REQUEST]))

     
            raw = arduino.read_until ()


            def parse_raw(raw):
                         raw = raw.decode ()
                if raw [-1] != "\n":
                    raise ValueError (
                        "Input must end with newline, otherwise message is incomplete."
                    )
                if raw [0] == "0":
                    pass
                try:
                    t, V = raw.rstrip ().split (",")

                    return int(t), int(V)*5/1023
                except ValueError:
                    pass

            def request_single_voltage(arduino):
                """Ask Arduino for a single data point"""
                # Ask Arduino for data
                arduino.write (bytes ([VOLTAGE_REQUEST]))

                # Read in the data
                raw = arduino.read_until ()

                # Parse and return
                return parse_raw (raw)

arduino code

const int voltagePin = A0;
//const int HANDSHAKE = 0;
const int VOLTAGE_REQUEST = 200;

int inByte;
unsigned long time_us;
unsigned long currentTime;
unsigned long startTime;

void printVoltage() {
  // Delay to get to an even millisecond
  delayMicroseconds(1000 - (millis() % 1000));
  startTime = millis();

  for (int i = 0; i < 8000; i++)
  {
    // Delay to get to an even millisecond
    //delayMicroseconds(1000 - (millis() % 1000));
    time_us = millis() - startTime;

    // Read value from analog pin
    int value = analogRead(voltagePin);

    // Write the result
    Serial.print(time_us);
    Serial.print(',');
    Serial.println(value);
  }
}


void setup() {
  // Initialize serial communication
  Serial.begin(115200);

}


void loop() {

  // Check if data has been sent to Arduino and respond accordingly
  if (Serial.available() > 0) {
    // Read in request
    inByte = Serial.read();

    // If data is requested, fetch it and write it, or handshake
    switch (inByte) {
      case VOLTAGE_REQUEST:
        printVoltage();
        break;
        //case HANDSHAKE:
        //if (Serial.availableForWrite()) {
        //Serial.print(""); //Please input:
    }
    //break;
  }
}

Possibly a Serial communication challenge

You should check what you read, back from arduino, maybe you timed out or saturated the buffer etc…

My guess would be that your python 'parse_raw' function is detecting some kind of invalid input. Perhaps after raw = arduino.read_until () you should display the raw text. Then you can tell if the data received from the Arduino is wrong to the parsing is wrong.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.