Hi....
First to say Hello to everyone...As this is my first post like to say that I like the site...Very useful with a lot of info...examples...and so on... Keep like that!!!
Now about Serial1...
Recently I started to write some code that will allow multiple Arduinos to work in net and act like inputs and outputs where one is Master and others are Slaves...Every with unique software address...
Idea was to make communication over RS485 so I bought Serial-to-RS485 adaptors ..I chose MEGA for Master as it has 4 HW serial ports and NANOs for Slaves as it is small and cheap...
As NANO has only 1 HW serial I used SW serial library...
Wrote a code , but at beginning i wrote code with SW serial for both - MEGA and UNO.
Every thing worked fine!
Then I decided to switch to HW serial on MEGA - to SERIAL1.
That is the point where I spotted something unusual - which I still dont understand.
As it is known RS485 board has 2 control inputs - for sending and receiving...(which i connected together)
When you want to receive something both have to be 0 and when you want to send something both have to be 1. I decided to make pin 2 on MEGA as control pin and connect it to send/receive inputs on RS485.
So I send Data. 5 variables with Println function (13 and 10 at the and of every variable) @ 19200 bps.
Put Control pin to 1 , send 5 variables , make little DELAY , put Control pin to 0,
I red somewhere that it has to be a little DELAY before you put Control pin to 0 - to allow last Data to be sent completely.
In SW serial , I put 1mS DELAY and it works fine...Same Delay in HW serial1 doesnt work correctly - sends only few bytes...I tried to increase DELAY and at cca 10mS it starts to work correctly.
Q: why so big delay is needed?
CODE:
Master ( MEGA)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
const byte Epin = 2;
const byte led_pin = 13;
int txadr ;
int txcmd = 200;
int txdat1 = 1;
int txdat2 = 0;
int txcsm = 0;
boolean led_state = false;
void setup() {
mySerial.begin(19200);
Serial1.begin(19200);
pinMode(Epin, OUTPUT);
digitalWrite(Epin,LOW);
pinMode(led_pin,OUTPUT);
}
void loop() {
for(int a = 1; a<=30; a++){
txadr = a;
txcsm = txadr^txcmd^txdat1^txdat2;
ser1();
delay(50);
swser();
delay(1000);
}
txdat1++;
}
////////////////////////////////////////////////////
void ser1(){
rx_led();
digitalWrite(Epin,HIGH);
delay(1);
mySerial.println(txadr);
mySerial.println(txcmd);
mySerial.println(txdat1);
mySerial.println(txdat2);
mySerial.println(txcsm);
delay(1);
digitalWrite(Epin,LOW);
}
/////////////////////////////////////////////////////////
void swser(){
rx_led();
digitalWrite(Epin,HIGH);
delay(1);
Serial1.println(txadr);
Serial1.println(txcmd);
Serial1.println(txdat1);
Serial1.println(txdat2);
Serial1.println(txcsm);
delay(1); ///////////this DELAY I am talking about!!!
digitalWrite(Epin,LOW);
}
void rx_led(){
led_state = !led_state;
digitalWrite(led_pin,led_state);
}
/////////////////////////////////////////////////////////
Slave (NANO);
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
const byte Epin = 4;
const int led_pin = 13;
/////////////////////////////////////////////////
void setup() {
// put your setup code here, to run once:
mySerial.begin(19200);
Serial.begin(57600);
pinMode(Epin, OUTPUT);
digitalWrite(Epin,LOW);
pinMode(led_pin,OUTPUT);
}
////////////////////////////////////////////////////
void loop() {
while(!mySerial.available()){
}
delay(30);
while(mySerial.available()){
Serial.print(mySerial.read());
Serial.print(",");
}
Serial.println();
}
To be clear - this is a sketch only to show what happens....
NANO receive on SW serial and send received bytes to PC ( as monitor to see what camed )
MEGA send it on HW serial 1 and than on SW serial...
I put RX and TX wires on HW serial1 it doesnt work correctly - I put RX and TX on SW Serial it works!!!
SAME CODE!!???
Boogi.