0

In my recent project, I trying to send serially data from Arduino to Python. Using pyserial it is done perfectly. But I have one problem here. I can't able to split my data. All are stored in one variable in Python coed but I want to split it.

Aduino Code:

#define sw1 6
#define sw2 5
#define sw3 4
#define sw4 3

int vote1 = 0;
int vote2 = 0;
int vote3 = 0;
int vote4 = 0;

void setup()
{
  Serial.begin(9600);

  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
  pinMode(sw3, INPUT);
  pinMode(sw4, INPUT);

  digitalWrite(sw1, HIGH);
  digitalWrite(sw2, HIGH);
  digitalWrite(sw3, HIGH);
  digitalWrite(sw4, HIGH);
}

void loop()
{
  if (digitalRead(sw1) == 0)
  {
    vote1++;
    Serial.print(vote1);
    Serial.print(("\t"));
    Serial.print(vote2);
    Serial.print(("\t"));
    Serial.print(vote3);
    Serial.print(("\t"));
    Serial.print(vote4);

    while (digitalRead(sw1) == 0);
  }
  if (digitalRead(sw2) == 0)
  {
    vote2++;
    Serial.print(vote1);
    Serial.print(("\t"));
    Serial.print(vote2);
    Serial.print(("\t"));
    Serial.print(vote3);
    Serial.print(("\t"));
    Serial.print(vote4);

    while (digitalRead(sw2) == 0);
  }
  if (digitalRead(sw3) == 0)
  {
    vote3++;
    Serial.print(vote1);
    Serial.print(("\t"));
    Serial.print(vote2);
    Serial.print(("\t"));
    Serial.print(vote3);
    Serial.print(("\t"));
    Serial.print(vote4);

    while (digitalRead(sw3) == 0);
  }
  if (digitalRead(sw4) == 0)
  {
    vote4++;
    Serial.print(vote1);
    Serial.print(("\t"));
    Serial.print(vote2);
    Serial.print(("\t"));
    Serial.print(vote3);
    Serial.print(("\t"));
    Serial.print(vote4);

    while (digitalRead(sw4) == 0);
  }
}

Here, I'm trying to send four different value from Arduino to Python using four button.

Python Code:

import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)

while 1:
    arduinoData = ser.readline().decode('utf-8')
    data = arduinoData.split("\t")

    a = data[0]
    b = data[1]
    c = data[2]
    d = data[3]

And I got error like below:

Traceback (most recent call last): File "/home/pi/serialConnection_2.py", line 11, in b = data[1] IndexError: list index out of range

If I change my python code like below:

import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)

while 1:
    arduinoData = ser.readline().decode('utf-8')
    data = arduinoData.split("\t")

    print(data)

Then I can able to receive all four value in form of list like below:

['']
['']
['']
['']
['']
['']
['']
['']
['']
['1 , 0 , 0 , 0']
['2 , 0 , 0 , 0']
['3 , 0 , 0 , 0']
['4 , 0 , 0 , 0']
['4 , 1 , 0 , 0']
['4 , 1 , 1 , 0']
['4 , 1 , 2 , 0']
['4 , 1 , 2 , 1']
['4 , 1 , 2 , 2']
['4 , 1 , 2 , 3']
['']
['']
['']

When ever I press button according that list's value is update properly. I don't know why I can't able to access list's members.

Hasan
  • 157
  • 8

1 Answers1

0

split in Python returns [''] when the input string is empty. Then you're trying to access an index inside data that doesn't exist (i.e. data[1], etc.) because your serial is receiving many empty strings, thus try checking for presence before treating the data:

arduinoData = ser.readline().decode('utf-8')
if arduinoData:
    data = arduinoData.split("\t")

    a = data[0]
    b = data[1]
    c = data[2]
    d = data[3]
Toribio
  • 3,963
  • 3
  • 34
  • 48