Reading Serial from 2 sources?

Hi there,

Project I wish to get to: Read RFID card -> Play iTunes -> Send back track name to Arduino with LCD
Where I'm at: As seperate scripts,
I have Arduino with LCD printing text sent from an AppleScript using SerialPort X.
In another script I have Arduino writing to AppleScript based on a RFID card.

Now, I'm trying to combine the scripts.

The script for RFID (based on one from bildr.org) is using Serial.read to get the RFID card tagand putting it in an int array to compare the RFID tag to the known tags.
The script to read an incoming text string from the AppleScript (code from PaulS's posts on forum) is also wanting to use Serial.read and putting in a char array.

Obvious conflict.

I'm not sure how to solve this. I'll post the code below (sorry, it's a bit of a miss, it is a work in progress).
I've left both of the times it's trying to Serial.read in there to show you. I'm aware that this won't work but I'm stuck.

#include <LiquidCrystal.h>

int RFIDResetPin = 13;

//Register your RFID tags here
char tag1[13] = "4500B8A5025A";
char tag2[13] = "4500B8C30F31";
char tag3[13] = "4500B8A4C49D";

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup(){

  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  //ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
  pinMode(6, OUTPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
}



void loop(){


  char tagString[13];
  int index = 0;
  boolean reading = false;
  



  //RFID TRYING TO READ
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
    
    
    
    
    
    //TRYING TO READ AppleScript STRING
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
  
  
  
  
  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    Serial.println(inData);
    lcd.setCursor(0, 0);
    lcd.print(inData);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  
  
  
  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //reset the RFID reader
}






void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1)){ // if matched tag1, do this
    lightLED(2);
    Serial.print("a");

  }else if(compareTag(tag, tag2)){ //if matched tag2, do this
    lightLED(2);
    Serial.print("b");

  }else if(compareTag(tag, tag3)){
    lightLED(2);
    Serial.print("c");

  }else{
    Serial.println(tag); //read out any unknown tag
  }

}

void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}

Any help on this one would be greatly appriciated as I'm really new to this and learning as I go. I'm really enjoying it, but keep getting stuck! :blush:

Many thanks

Change one of them to SoftSerial.

However it looks like you only receive from one place and send to another, is that correct? If so you can possibly separate the Rx and Tx signals


Rob

Thanks Graynomad,

I'm managed to get it working using Software serial.

You're right I am only wanting to send the RFID information and receive the information from the Mac, so I'll look into that.

Heres the current code:

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 9

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

int RFIDResetPin = 13;

//Register your RFID tags here
char tag1[13] = "4500B8A5025A";
char tag2[13] = "4500B8C30F31";
char tag3[13] = "4500B8A4C49D";

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup(){
  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);
  //ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
  pinMode(6, OUTPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
  mySerial.begin(9600);
}



void loop(){


  char tagString[13];
  int index = 0;
  boolean reading = false;
  


  //RFID TRYING TO READ
  // Read all serial data available, as fast as possible
  while(mySerial.available() > 0)
  {
    int readByte = mySerial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
  }
    
    
    
    
  while(Serial.available()){ 
    //TRYING TO READ AppleScript STRING
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
  
  
  
  
  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    Serial.println(inData);
    lcd.setCursor(0, 0);
    lcd.print(inData);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  
  
  
  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //reset the RFID reader
}






void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1)){ // if matched tag1, do this
    lightLED(2);
    Serial.println("a");

  }else if(compareTag(tag, tag2)){ //if matched tag2, do this
    lightLED(2);
    Serial.println("b");

  }else if(compareTag(tag, tag3)){
    lightLED(2);
    Serial.println("c");

  }else{
    Serial.println(tag); //read out any unknown tag
  }

}

void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}