Pages: 1 [2]   Go Down
Author Topic: CSV file read stops after 9 lines?  (Read 1694 times)
0 Members and 1 Guest are viewing this topic.
New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is this system intended to do? How many relay commands will there be? Right now your test example suggests you'll be turning each relay on only once - is that true for the final product? If you have a small (preferably known) number of actions, you can just read them into an array as fast as the serial port can deliver them. If there are too many then PaulS' suggestion of software flow control (e.g. XON/XOFF) may well help.

Finally, if you're prepared to assume that your relays are on adjacent pins (as you did setting their pinmode), you can simplify your actRelay routing to this:

Code:
void actRelay(int* array)
{
digitalWrite(array[0]+22, HIGH);
delay(array[1]);
digitalWrite(array[0]+22,LOW);
delay(array[2]);
}
 
Logged

Ottawa, Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again everyone,

Here is my new code; I've tightened things up a bit.  I also googled and searched this forum for flow control on the serial buffer; and found that according to those in the know: there is no flow control on the Arduino's serial interface.  Please correct me if I'm wrong (and my wish is that someone please corrects me).  I would love to see code that would perform this in order to incorporate it in my current proof of concept sketch.  I've found a few things on Handshaking, but they all seem to relate to Arduino to some external device serial comms, rather than a constant stream from DTE to the Arduino.

The new code permits the execution up to relay 13 at 1200 baud.  If I remove the lines that actually print to the serial port (for visual feedback of where the CSV file is at); the entire test CSV up to relay 16 does cycle through; I can see the relay indicator LEDs light up sequentially.  If I bring the baud rate to 9600, the sequence stops at 9 as it did before; even without the serial printing.

Code:
int data[3];
int arr_ndx;
int relaypin;

void setup(){
  // initialize serial communication:
  Serial.begin(1200);
  arr_ndx = 0;
  // initialize the relay pins:
  for (relaypin = 22; relaypin < 38; relaypin++) {
    pinMode(relaypin, OUTPUT);
  }
}
void loop(){
  if (Serial.available()){
    data[arr_ndx] = Serial.parseInt();// check for comma separated data
    arr_ndx++;
    if(Serial.available() && Serial.read() ==  '\n'){  // end of line test
      arr_ndx = 0;
      actRelay(data);//send data to actRelay func
/*      Serial.print("Relay:");
      Serial.println(data[0]);
      Serial.print("Time ON:");
      Serial.println(data[1]);
      Serial.print("Time OFF:");
      Serial.println(data[2]);*/
    }

  }
}

void actRelay(int* array)
{
  digitalWrite(array[0]+22, HIGH);
  delay(data[1]);
  digitalWrite(array[0]+22,LOW);
  delay(data[2]);
}

Thanks Wildbill for the snippet that reduced the size and complexity of the actRelay function. 

I appreciate all your help on this.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't believe you've ever shared the contents of this mysterious file you are reading from. We can't even try to duplicate your problem without that.
Logged

Ottawa, Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The file is just a text file containing lines as shown in my very first post.  I run through a sequence of numbers corresponding to the relay numbers: 1 through 16 followed by relay on-time and off-time.  It looks something like this:

Code:
1,50,50
2,50,50
3,50,50
4,50,50
5,50,50
6,50,50
7,50,50
8,50,50

...and so on and so on.  The Serial.parseInt() helps separate the values within the sketch.  I can confirm that changing the delays changes the number of lines the sketch can go through (compounded by whether I allow the on-screen printing of the results).  I understand that the relay activation is not an amazing feat, but this project is a proof of concept.  These relays will eventually perform tasks and the sequence and timing will be much different.  Coming up against these challenges now permits me to understand the behavior of the Arduino and programming in C++.

The final proof will be using a Leonardo connected to an SD card reader containing the CSV file, it will transmit this file's data via Dallas 1-wire comms (to keep the wiring down to 2 physical wires only), and having a Mega 2560 receive the data from the Leonardo to activate the relays.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't used XON/XOFF in a long time, let alone in any arduino related application, but it should be easy enough to test. Can you send control-s (character 19) before you call the relay function and control-q ( character 17) afterwards using Serial.write?
Logged

Ottawa, Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will try that.

My serial terminal program (on Linux Kubuntu 12.04) does accept software flow control: Xon/Xoff.  I'm not at the lab right now, but I'll try later today and report back.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I loaded test.txt with 20 lines of CSV text with three numbers (all numbers <= 255), and this got them all.
Code:
#include <SD.h>
char netBuffer[32];
byte myTest[3];

void setup() {
  Serial.begin(9600);
  // disable w5100
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  
  Serial.print("Starting SD...");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");
  
  File fh = SD.open("test.txt",FILE_READ);
  int chPos = 0;
  int lineNo = 0;
  
  if(!fh)
  {
    Serial.println("SD open fail");
    return;    
  }

  while(fh.available())
  {
    char ch = fh.read();
    if(ch == '\n') {
      chPos = 0;

      sscanf(netBuffer,"%u,%u,%u",&myTest[0],&myTest[1],&myTest[2]);  

      Serial.print(netBuffer);

      Serial.print("  A = ");
      Serial.print(myTest[0]);
      Serial.print(" B = ");
      Serial.print(myTest[1]);
      Serial.print(" C = ");
      Serial.println(myTest[2]);
      
      lineNo++;
    }
    else if(ch == '\r') {
      // do nothing
    }
    else if(chPos < 31) {
      netBuffer[chPos] = ch;
       chPos++;
      netBuffer[chPos] = 0;
    }
  }
  fh.close();
}

void loop() {
}
edit: Removed extra blank lines.
« Last Edit: November 19, 2012, 07:48:19 am by SurferTim » Logged

Ottawa, Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Morning WildBill,

Tried your suggestion on sending character 19 and character 17 through Serial.write, but no avail.  I can see that my terminal program is accepting the code and doing something with it; if I don't turn on Xon/Xoff flow control, I will get characters appearing on the terminal screen - the opposite happens when I do activate flow control (this presumes that if I don't see the characters on screen; they must be processed by the application).  The issue, as far as the buffer over run, still exists.

SurferTim has the final solution: eventually my CSV will reside on an SD card connected to an Arduino Leonardo.  It will transmit the lines of the CSV to the Mega, which will activate the relays.  Reading the CSV line by line through the SD library will provide a slow smooth data flow to the Mega that will not over run its buffer.  The exercise was good: I wanted to understand flow control for future use.  I still want to know how to achieve it; if there's still someone out there that can show me.

Thank you all so far for your suggestions.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SurferTim has the final solution: eventually my CSV will reside on an SD card connected to an Arduino Leonardo.  It will transmit the lines of the CSV to the Mega, which will activate the relays.  Reading the CSV line by line through the SD library will provide a slow smooth data flow to the Mega that will not over run its buffer.  The exercise was good: I wanted to understand flow control for future use.  I still want to know how to achieve it; if there's still someone out there that can show me.
Which flow control? The SD to the Leonardo, the Leonardo to the Mega, or the Mega to the relays?
Logged

Pages: 1 [2]   Go Up
Jump to: