Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Using serialEvent() with arduino mega2560 on: March 24, 2014, 06:04:12 pm
Hi,
And thanks for all the replies.

I have now done the following;
Created a separate string for each serialEvent() to read data into and moved all the post processing to the main loop so that all the serialEvents()'s only read available data into a string.

This approach did make an improvement but was still creating strings with data written on top of each other but the frequency with which this is happening is now less - about every 20 times through the loop errors would occur. I then cheated slightly and told the post processing to ignore strings over a certain length and this has created the output i want although it is not strictly ideal as some data gets ignored but it is still getting enough to be acceptable.

I will continue to work on this to make it more accurate.

Again, thanks for all your help.

Cheers.
2  Using Arduino / Programming Questions / Re: Using serialEvent() with arduino mega2560 on: March 23, 2014, 11:12:48 pm
Hi ,
zoomkat: The data streams are delimited by line feeds and carriage returns so i collect data until a line feed is reached and don't start data collection until i receive an "L" (start of the Stream). This data is put into the String called "inString".

Caltoa: I have removed all delays apart from the one in the serialEvent() that makes sure there is something to receive and i have reduced this to  500uS. Incoming stream is at 9600 baud. This did help a little bit but still getting strings written on the end of other strings. Sometimes i get three complete strings stored in "instring".

One thing i haven't tried is to flush the hardware buffer  before reading instead of after reading it which makes more sense.

Cheers.

3  Using Arduino / Programming Questions / Re: Using serialEvent() with arduino mega2560 on: March 23, 2014, 08:22:10 pm
Hi Caltoa,
And thanks for the reply.

Year i did spot the =+ and have changed it. I will check out the links. I thought it may have a been a timing issue with respect to to much data coming in at once, so i tried putting in some delays in the serialEvent() functions but this had no effect - in fact it seemed to make it worse.

The flush function is supposed to prevent multiple reads but i am not sure if i am making the correct use of it.

Cheers.
4  Using Arduino / Programming Questions / Re: Using serialEvent() with arduino mega2560 on: March 23, 2014, 07:19:48 pm
Hi,
And thanks for the reply although i dont quite understand what you mean. As i understand it the serialEvent() functions run in-between each run of the main loop.  If there is serial data in one or more of the hardware serial buffers  serialEvent1(), serialEvent2() and serialEvent(3) will be triggered one after the other, updating the value of inString as they do so and in this case logging each time.

Have i got this wrong?

Cheers.
5  Using Arduino / Programming Questions / Using serialEvent() with arduino mega2560 on: March 23, 2014, 06:42:13 pm
Hi,
I am using arduino mega2560 R3 with 3 hardware serial inputs and the serialEvent() function. This seems to work fine when the input serial streams only occur on one serial input at a time. When serial input is occurring on all three hardware serial ports at the same time i get slightly garbled output e.g serial streams will be put on the end of the serial stream from a previous read etc.

I have included the code excerpt below and some example output when two of the three hardware serial ports are receiving at the same time.

Any help is much appreciated.

Cheers.

Code:
void serialEvent1() {
  if (Serial1.available() > 0) {
    inString =+ "1,";   
    // get the new byte:
    char inChar = (char)Serial1.read();
    while (inChar != 'L') {inChar = (char)Serial1.read();}
    // add it to the inputString:
    while (inChar != '\r') {
      while (!Serial1.available()) delay(1);
      inString += inChar;
      inChar = (char)Serial1.read();
    }
   
    Serial1.flush(); // stops multiple reads
   
    if (inString.length() > 20) {
      logTag();
      chkIfNewTag();
      inString = "";
    }
    else {
       inString = "";
    }
  }
}

void serialEvent2() {
  if (Serial2.available() > 0) {
    inString += "2,";
     // get the new byte:
     char inChar = (char)Serial2.read();
     while (inChar != 'L') {inChar = (char)Serial2.read();}
     // add it to the inputString:
     while (inChar != '\r') {
       while (!Serial2.available()) delay(1);
       inString += inChar;
       inChar = (char)Serial2.read();
     }
     
     Serial2.flush(); // stops multiple reads
     
     if (inString.length() > 20) {
       logTag();
       chkIfNewTag();
       inString = "";
     }
     else {
       inString = "";
     }
   }
}

void serialEvent3() {
  if (Serial3.available() > 0) {
    inString += "3,";
     // get the new byte:
     char inChar = (char)Serial3.read();
     while (inChar != 'L') {inChar = (char)Serial3.read();}
     // add it to the inputString:
     while (inChar != '\r') {
       while (!Serial3.available()) delay(1);
       inString += inChar;
       inChar = (char)Serial3.read();
     }
     
     Serial3.flush(); // stops multiple reads
     
     if (inString.length() > 20) {
       logTag();
       chkIfNewTag();
       inString = "";
     }
     else {
       inString = "";
     }
   }
}

void logTag() {
  tagCnt++;
  //Serial.println(tagCnt,DEC);    //testing
  DateTime now = RTC.now();
  sprintf( timeString, "%d:%d:%d", now.hour(), now.minute(), now.second());
  // if the file is available, write to it:
  //tagString = "LR 0000 0000001234567890";
  //dataFile = SD.open(dateString,FILE_WRITE);
  //Serial.println(dateString);
  if (dataFile) {
    dataFile.print(timeString);
    dataFile.print(",");
    dataFile.println(inString);
    dataFile.flush();
    // print to the serial port too:
    Serial.println(inString);
    //inString = "";
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println(F("data logging error"));
    //for (;;){}
  }
  delay(20);
}


Example output from serial terminal
3,LA 00000 LA 00000 0 900 226000106155
1,LR 0000 0000000174825LR 0000 0000000174825496
3,LA 00000 0 900 226000106LA 00000 0 900 226000106155
1,LR 0000 0000000LR 0000 0000000174825496
3,LA 00000 LA 00000 0 900 226000106155
1,LR 0000 0000000174825LR 0000 0000000174825496
3,LA 00000 0 900 226000106LA 00000 0 900 226000106155
1,LR 0000 0000000LR 0000 0000000174825496
1,LR 0000 000000017482596
3,LA 00000 0 900 226000106155
1,LR 0000 0000000174825496
1,LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
3,LA LA 00000 0 900 226000106155
1,LR 0000 000000017482549 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000LR 0000 00000LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000 00000001LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000 000000017482LR 0000 0000000174825496
1,LR 0000 0000000174825496
3,LA 00000 0 900 22LA 00000 0 900 226000106155


6  Using Arduino / Project Guidance / Re: seeedstudio gprs v2.0 shield works with arduino but not with mega 2560 on: November 19, 2013, 04:22:01 pm
Hi Paul,
Thanks for the information - much appreciated.

Cheers.
7  Using Arduino / Project Guidance / seeedstudio gprs v2.0 shield works with arduino but not with mega 2560 on: November 19, 2013, 05:19:50 am
Hi,
I have been using the seeedstudio gprs v2.0 shield with the arduino UNO R3 in a project that reads serial data, logs teh data,  then texts a user with some of the information and this works really well. However the project was upgraded to use 4 serial inputs so i changed to a seeeduino mega 2560 (same as arduino mega 2560) however i cant seem to communicate with the gprs shield from the mega (i have asked on the seeedstudio forum but haven't got a response as yet so thought i would try here). The gprs shield uses pins 7 & 8 for software serial so i thought the code would be the same for the mega to talk to the gprs shield - but i get no response. Is there a different pinout/software serial  configuration on the  mega to access the software serial pins on the GPRS unit? I know i had to make a small code change to talk to the logger shield (uses SPI pins) from the mega compared to the UNO. Would it be the same case to enable mega to talk to gprs unit over software serial?

Any help is much appreciated.

Cheers.
8  Using Arduino / Project Guidance / using GPRS to upload to cloud on: October 16, 2013, 03:37:06 pm
Hi,
I have a project that logs data into daily files and texts a user on a daily basis with a summary of the information, it also texts the user if system voltage drops for any reason. This system works well. However being able to see the contents of the file would be very useful.

The system uses a logger shield, and a GPRS shield and makes use of simple AT commands to do the txting. Is it possible to upload the daily files to the cloud using GPRS and AT commands. The files are quite small (~2 - 40 KB txt files). If it is possible are there any special coding/AT command requirements.

Any help is much appreciated.

Cheers.
9  Using Arduino / Programming Questions / Re: How to add strings to an array on: May 27, 2013, 04:37:09 pm
Hi TanHadron,
Thanks for the reply. I decided to go with the static approach as it seems simpler and easier to understand however when i used your code example i got the following error (error: cannot convert ‘String’ to ‘const char*’ for argument ‘2’ to ‘char* strcpy(char*, const char*) for the line strcpy(tagStrings[tagCount], tag);

Do i need to first convert the string to an array of char?

Any help is much appreciated.

Cheers.
10  Using Arduino / Programming Questions / How to add strings to an array on: May 26, 2013, 12:47:37 am
Hi,
I need to create an array of strings to hold tag data but i cant quite figure out how to do it (i am relatively new to arduino and C programming).

The code below is what i have done so far (this code reads lines (tag) from a file - if the tag is different it adds it to the array). Any help with this is much appreciated.

Cheers.

Code:
void distinctEntries() {
  dataFile = SD.open(lookFor); //open file for read
  tag = String();
  prevTag = String();
  char* tagStrings[] = {};
  int i = 0; 
 
  if (dataFile) {
    while (dataFile.available()) {
      char ch = dataFile.read();
     
      while (ch != '\n') {
        tag += ch;
        ch = dataFile.read();       
      }
     
      tag = tag.substring(9);    //dont need the first 9 characters
     
      if (tag != prevTag) {         //this is also spose to check the other entries in the array but not implemented yet
        strcpy(tagStrings[i],tag);    //or something that actually works
        i++;
      }
     
      prevTag = tag;
      tag = String();   
    }
   
    // close the file:
    dataFile.close();
  }
  else {
  // if the file didn't open, print an error:
    Serial.println("error opening file");
  }
 
}
11  Using Arduino / Programming Questions / GSM shield example code sending email on: April 10, 2013, 04:04:34 pm
Hi,
I want to use the arduino GSM shield (Code: A000043) to send email with a data file as an attachment. Is there any example code available to achieve this.

I have arduino version 1.0 - will the GSM library work with this?

Cheers.
12  Using Arduino / Programming Questions / Re: multiple softwaresSerial inputs on: March 24, 2013, 09:24:43 pm
Hi Simpson_Jr,
And thanks for the detailed response. I will make the suggested changes and see if that makes any difference.

With reference to the odd characters after the tag (LR 0000 0000000174825496) these should not be there only a carriage return. If i remove references in void loop() to the LCD the tag displays correctly in the serial monitor (see below);
Code:
Tag reader
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496
LR 0000 0000000174825496

With reference to the LCD; The lcd does not blank after displaying "testing" for a second it just keeps displaying "testing" which indicates that the LCD is not receiving any further data even though it should be at least, receiving the lcd.write(12) command to blank the screen.

Any help is much appreciated.

Cheers.
13  Using Arduino / Programming Questions / Re: multiple softwaresSerial inputs on: March 24, 2013, 03:01:29 pm
Hi,
And thanks for the replies. I have taken the advice and moved to the .listen() method but are still having a few issues getting tag data to appear on the LCD.

The following code;
Code:
#include <SoftwareSerial.h>

SoftwareSerial rfid = SoftwareSerial(5, 6,true);
SoftwareSerial lcd = SoftwareSerial(2,3);

void setup() {
 
  Serial.begin(9600);
  Serial.println("Tag reader");

  lcd.begin(9600);
  lcd.write(12);
  lcd.print("testing");
 
  delay(1000);
 
  rfid.begin(38400);
}

void loop() {
  rfid.listen();    //listen on the reader port
  while (rfid.available() > 0) {
    char inByte = rfid.read();
    Serial.write(inByte);
    lcd.write(12);      //cls
    lcd.write(inByte);
  }
}

Gives the following output on the Serial monitor nad nothing on the lcd after "testing".
Code:
Tag reader
LR 0000 0000000174825496
S*$&&&&$&&&&.lLL’ª¢Ê²jRþ¤@``aáÁÁƒ‚‚‚‚Šº¢Â’ª¢Ê²j¦ü3nlLLLL’ª¢Ê²j¦üR 0000 0000000174825496
IÁÁÁÁÁÁƒ‚‚‚‚Šº¢Â’ª¢Ê²j¦*$&&&&$&fLLLL\L’R 0000 00000001748“‚‚‚‚‚‚‚‚‚‚Šº¢Â6&$&&&&&&¦¦’R 0000 00000001748%‚‚‚‚‚‚‚‚‚‚‚Šº¢Â*$&&&&$&fLLLܘL’ª¤@````AáÁƒ‚‚‚‚Šº¢7fLLLLLL’©˜¸00 00000001748%‚‚‚‚‚‚‚‚‚‚‚Šº¢Â*$&&&&$&&&&.lÌL’¤@`aáÁÁÁÁÁÁÁÁÅÝÑá*$&&&&$&fLLLLLL“’R 0000 00000001748%‚‚‚‚‚‚‚‚‚‚Šº¢Â*$&&&&$&fLLLܘL’R 0000 00000001748µ‚‚‚‚‚‚‚‚‚‚Šº¢Â6&&&$&&&&&&¦¦’R 0000 00000001748%‚‚‚‚‚‚‚‚‚‚‚Šº¢Â*$&&&&$&&&&&lÌL’­ÁÁÁÁÁÁÁÁÁÁÁÁÅÝÓâ*$&&&&$&&&&&&îì’¤@`aaÁÁÁÁÁÁÁÁÅÝÑá*$&&&&$&&&&.lÌL’¤@````AáÁÁÁÁÁÁÅÝÑá*$&&&&$&&.lLLLL’¤@`aaáÁÁƒ‚‚‚‚Šº¢Â7$&fLLLLLL’R 0000 00000001748—‚‚‚‚‚‚‚‚‚‚Šº¢Â:$&&&&$&fLLLLLL’©˜¸00 00000001748%‚‚‚‚‚‚‚‚‚‚‚Šº¢Â*$&&&&$&&&&&lÌL’þ
LR 0000 0000000174825496
)*$&&&&$&&&&.lÌL’ª¢Ê²jRþR 0000 0000000174825496


And this code;
Code:
#include <SoftwareSerial.h>

SoftwareSerial rfid = SoftwareSerial(5, 6,true);
SoftwareSerial lcd = SoftwareSerial(2,3);

void setup(){
  Serial.begin(9600); 
  Serial.println("Tag reader");
  lcd.begin(9600);
  lcd.write(12);
  lcd.print("testing");
 
  delay(1000);
 
  rfid.begin(38400);
}

void loop() {
  rfid.listen();    //listen on the reader port
  while (rfid.available() > 0) {
    char inByte = rfid.read();
    Serial.write(inByte);
    lcd.listen();
    lcd.write(12);      //cls
    lcd.write(inByte);
   
    rfid.listen();
  }
}

gives this output in the serial monitor and nothing on the LCD after "testing".
Code:
Tag reader
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
14  Using Arduino / Programming Questions / multiple softwaresSerial inputs on: March 20, 2013, 09:25:52 pm
Hi,
I am trying to get 2 software serial inputs to work. One inputs tag data from an rfid reader and the other outputs this data to a serial LCD. I cant use the hardware serial port as this will be used for a uart SD card. For now it used to display tag data for testing.

I realize that i have to close one software serial port before i open another by using the .end() method however this does not seem to be working .

The code i am using is below. It looks like when the LCD port is opened the rfid port does not accept any data from the tag reader. The first reference to the LCD works fine and displays "testing" as it should but the second reference to the LCD displays nothing. If i take out any references to the LCD it works fine although a bit slow when the .end() is used which is a bit odd.

Can anybody see what i have done wrong here.

Any help is much appreciated.

Cheers.

Code:
#include <SoftwareSerial.h>

SoftwareSerial rfid = SoftwareSerial(5, 6,true);
SoftwareSerial lcd = SoftwareSerial(2,3);

String tagString = "";    // string to hold input

void setup(){
  // setup the software serial pins
  pinMode(5, INPUT);
  pinMode(2, INPUT);
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
 
  Serial.begin(9600);  // Hardware serial for Monitor 9600bps
  Serial.println("Tag reader");

  lcd.begin(9600);       //open lcd port
  lcd.write(12);
  lcd.print("testing");
  lcd.end();       //close the port
 
  delay(1000);
}


void loop() {
  rfid.begin(38400);    //open the reader port at 38400 baud
  while (rfid.available() > 0) {
    char inByte = rfid.read();   
    if (inByte == 'L') {
      while(true) {
        inByte = rfid.read();
        tagString += inByte;
        if (inByte == '\n') {
          rfid.flush();       //flush the input buffer
          rfid.end();        //close the reader port
          lcd.begin(9600);      //open lcd port again to display the tag string
          lcd.write(12);        //cls
          lcd.print(tagString);
          lcd.end();             //close teh port
          rfid.begin(38400);      //open the rfid port
          tagString = "";
          break;
        }
      }
    }
  }
  rfid.end();
}

 
15  Using Arduino / Project Guidance / Re: multiple software serial inputs on: March 20, 2013, 08:48:54 pm
Hi,
And thanks for the replies. Yes it looks like i can only have one port open at a time and must make use of the .end() method to close the port before opening another port.

I will move this thread to the code section as it probably more of a coding issue.

Thanks for all your help.
Pages: [1] 2