Go Down

Topic: Multi serial with Arduino Mini (Read 1 time) previous topic - next topic

schrist

Hi,

I try to have two ports for working and use the standard port only for flash my code.

Code: [Select]

NewSoftSerial SerialRFIDreader(2, 3);
NewSoftSerial SerialRS485(4,5);
byte c;


void setup() {               
    // initialize the digital pin as an output.

  SerialRFIDreader.begin(4800);
  SerialRS485.begin(4800);
  Serial.begin(4800);

    Serial.println("En route");
    SerialRS485.print("Bonjour");

}

void loop()
{
//  while (SerialRFIDreader.available() > 0)
  if (SerialRFIDreader.available() > 0)
  {
    c = SerialRFIDreader.read();
        Serial.println(c);
  }
  Serial.println("C");
  if (SerialRS485.available() > 0)
    {
      Serial.println("D");
      c = SerialRS485.read();
}

}



I send many charaters on the pin 4, but, become never "D". I think SerialRS485.available() is never bigger then zero... but why ?

Is possible to work so with three ports ?

robtillaart


do you have tx and rx connected correctly (or crossed?)  might need to swap pins. 
Is the 4800 baud correct?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

schrist

Hi,

Yes, I've right connected tx and rx (crossed first time... :smiley-red:) .
In a first tim, I've tried with 9600 bauds -> same result.

Is a bug in newsoftserial ?

robtillaart

You can use many instances of NSS in parallel as long as there is not too much traffic as the Arduino is single threading.

just another test sketch.
Code: [Select]

NewSoftSerial SerialRFIDreader(2, 3);
NewSoftSerial SerialRS485(4,5);
byte c;


void setup() {               
    // initialize the digital pin as an output.

  SerialRFIDreader.begin(4800);
  SerialRS485.begin(4800);
  Serial.begin(4800);

   Serial.println("En route");
}

void loop()
{
  Serial.print("RFID : ");
  Serial.print(SerialRFIDreader.available(), DEC);
  Serial.print("\t\t485 : ");
  Serial.println(SerialRS485.available());
  delay(1000);
}


Do you have a link to the datasheet of the RFIDReader?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

schrist

I will try this this evening.
I'll try to disconnect the rfidreader.

I can send with all the ports (I tested yesterday)

PaulS

Quote
You can use many instances of NSS in parallel as long as there is not too much traffic as the Arduino is single threading.

But, only one instance at a time can receive. NewSoftSerial uses an interrupt to catch the incoming data. When the interrupt fires, the data that is incoming is for the active instance.

Switching which instance is active occurs whenever a method on any instance is called. Changing the active instance changes which interrupt handler is attached, which changes which instance can receive data.

Multiple instances can send easily enough, but only one instance can receive at a time.

It looks like you think you can receive from two instances at once, which is not the case. To do that, you need two (or more) hardware serial ports, which use two (or more) interrupts - one for each hardware port. The only Arduino with multiple hardware serial ports is the Mega.

schrist

OK, I begin to understand........


I like read on both ports, but it is not important that is simultaneous.

I can imagine to read on one, work with and after read on the second.

So, You say "Changing the active instance". Can you explain how to do that ?


Thanks

PaulS

Quote
You say "Changing the active instance". Can you explain how to do that ?

I already did.
Quote
Switching which instance is active occurs whenever a method on any instance is called.

schrist

In my sample:
Code: [Select]

  if (SerialRFIDreader.available() > 0)
  {
    c = SerialRFIDreader.read();
        Serial.println(c);
  }
  Serial.println("C");
  if (SerialRS485.available() > 0)
    {
      Serial.println("D");
      c = SerialRS485.read();
}


SerialRFIDreader.available() is a call of method of one instance ?
and
SerialRS485.available() is a call of method of another instance ?

So, the active instance must change ?

PaulS

Quote
SerialRFIDreader.available() is a call of method of one instance ?
and
SerialRS485.available() is a call of method of another instance ?

So, the active instance must change ?

Yes. Notice, though that data that arrived for the non-active instance was lost, so calling the available method on the inactive instance will always return 0.

Calling them in loop(), as you are doing, will result in neither instance ever being able to receive data.

schrist

Yes,
You are right : only one instance can receive datas.
But I can send without problem to the two instances.

I search how to do...

PaulS

Quote
But I can send without problem to the two instances.

But I can send without problem from the two instances.

schrist


Go Up