Pyserial writing to arduino mega 2560 problems

1k views Asked by At

I feel this might be a problem on the python side, as the arduino m code works when I use the monitor.

Anyway, I'm basically sending an array of strings coming from a csv file to the arduino - using python 2.7 btw.

My problem is that the arduino stops receiving strings (at about 12 strings).

If anyone can see anything, that could be causing the problem I'd appreciate any help. I've attempted using various time.sleep(s) around the code as I've read many things - it takes awhile to initialize the port after serial.serial(). I've even tried waiting after all the data has been sent - before the comport must be read by the python code (that's my main method to check). I also have been using a software serial rx rx pins to a seperate usb to serial device (I don't rely on its output because it's cheap). I've also experimented with every Baud rate usable and no dice.

Here's the python code: `

import serial
import time
ser = serial.Serial('COM3', 9600, timeout=0)
file = open('C:\\samples.csv')
time.sleep(2)
while 1:
        line = file.readline()
        print line
        if not line:
                break
        ser.write(line)
        #time.sleep(4)
time.sleep(20)        
while 1:
        try:
                print ser.readline()
                time.sleep(1)
        except ser.SerialTimeoutException:
                print('Data could not be read')
                time.sleep(1)`

and here is the arduino code - the linkedlist library I have tested and it works:

#include <analogShield.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(18, 19); // RX, TX
#include <LinkedList.h>

unsigned int full = 65536;
unsigned int zero = 32767;

//SoftwareSerial mySerial(18, 19); // RX, TX
LinkedList<String> myLinkedList = LinkedList<String>();
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. 
  }


  //Serial.println("TESTING...");

  //
  delay(20000);
}

void loop() { // run over and over

  if (Serial.available()) {
    String s = Serial.readString();
    myLinkedList.add(s);
    mySerial.println(s);
    //delay(1);

  }
  else {
    Serial.println("THIS IS LIST "  );
    Serial.println(myLinkedList.size());
    for (int i = 0; i<myLinkedList.size();i++) {
      //unsigned int volt = myLinkedList.get(i);
      Serial.println(myLinkedList.get(i));
      //analog.write(0,volt);
      //delay(1);
      //delayMicroseconds(8);

      }

    while (true) {
      //analog.write(0,zero);  
    }
    }
}

`

1

There are 1 answers

1
snow On

So at 18th string the arduino code stops, because it goes into the while(true) loop. Maybe there is a small brake in transmission and your code goes into the else part, where the execution terminates. I would suggest going into the second part of the program upon receiving some special (string) command.

Always the same string, halfway through 18560.

18560 strings? That does not fit in mega 2560 RAM.