Morning to everyones.
I'm trying to use Serial Port #1 instead of Serial Port #0 on my Arduino Mega rev3 board, but evrything work well only if I keep USB cable plugged in my Mega Board, else if I disconnect USB cable serial communication doesn't work.
When I use Serial Port, instead, everything works.
Here is my code:
Whit Serial Port #1:
void serialEvent1() {
//Prendo i millisecondi attuali
long last_millis = millis();
long mem_millis;
//Inizializzo conteggio millisecondi di timeout
int count_millis = 0;
byte val;
byte bytesread;
byte CODE[20];
byte tempbyte;
byte checksum;
boolean Rxok;
if(Serial1.available() > 0) {
if((val = Serial1.read()) == 2) {
//Serial.println("serialEvent1");
checksum=0 ;
bytesread = 0;
while(bytesread < 16) {
if( Serial1.available() > 0) { //controllo se i dati sono disponibili ad essere letti
val = Serial1.read();
//quindi assegno a 'val' il valore dell'i-esimo carattere
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { //se leggo un carattere 'header' o un carattere di stop
break; // fermo la lettura
}
if ((val >= '0') && (val <= '9')) {
val -= '0';
} //traduco in esadecimale il carattere appena letto
else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
if (bytesread & 1 == 1) { //se ho letto un solo carattere fin'ora
CODE[bytesread >> 1] = (val | (tempbyte << 4)); //assegno alla seconda parte del byte in posizione bytesread-esima il valore esadecimale del carattere letto
if (bytesread >> 1 !=7) { //se ho letto l'ultimo byte della scheda calcolo il checksum
checksum ^= CODE[bytesread >> 1]; //facendo la XOR sull'ultimo byte letto
};
}
else {
tempbyte = val; //altrimenti assegno il valore letto alla variabile tempbyte
};
bytesread++; //mi preparo a leggere il prossimo byte
}
mem_millis = millis();
if(mem_millis != last_millis){
last_millis = mem_millis;
count_millis++;
if(count_millis > (MAX_MILLIS_TIMEOUT - 1)){
errRX1++;
break;
}
}
}
if (bytesread == 16) {
(CODE[7]==checksum) ? Rxok = true : Rxok = false ;
}
}
}
if (Rxok){
rxNODE11=CODE[0];
rxCODE11=CODE[1];
rxDATA11=CODE[2];
rxDATA12=CODE[3];
rxDATA13=CODE[4];
rxDATA14=CODE[5];
rxDATA15=CODE[6];
Rxok1 = true;
}
}
void sendMSG1(byte address,byte code,byte data1,byte data2,byte data3,byte data4,byte data5){
byte temp;
byte checksum;
UCSR1A=UCSR1A |(1 << TXC1);
digitalWrite(pinSerial1,HIGH);
delay(1);
checksum=0;
Serial1.write(0x02);
temp = address >> 4 ;
Serial1.print(temp ,HEX);
temp = address & 0x0F;
Serial1.print(temp ,HEX);
checksum ^= address;
temp = code >> 4 ;
Serial1.print(temp ,HEX);
temp = code & 0x0F;
Serial1.print(temp ,HEX);
checksum ^= code;
temp = data1 >> 4 ;
Serial1.print(temp ,HEX);
temp = data1 & 0x0F;
Serial1.print(temp ,HEX);
checksum ^= data1;
temp = data2 >> 4 ;
Serial1.print(temp ,HEX);
temp = data2 & 0x0F;
Serial1.print(temp ,HEX);
checksum ^= data2;
temp = data3 >> 4 ;
Serial1.print(temp ,HEX);
temp = data3 & 0x0F;
Serial1.print(temp ,HEX);
checksum ^= data3;
temp = data4 >> 4 ;
Serial1.print(temp ,HEX);
temp = data4 & 0x0F;
Serial1.print(temp ,HEX);
checksum ^= data4;
temp = data5 >> 4 ;
Serial1.print(temp ,HEX);
temp = data5 & 0x0F;
Serial1.print(temp ,HEX);
checksum ^= data5;
temp = checksum >> 4 ;
Serial1.print(temp ,HEX);
temp = checksum & 0x0F;
Serial1.print(temp ,HEX);
while (!(UCSR1A & (1 << TXC1)));
delay(1);
digitalWrite(pinSerial1,LOW);
}
Whit Serial Port:
void serialEvent() {
//Prendo i millisecondi attuali
long last_millis = millis();
long mem_millis;
//Inizializzo conteggio millisecondi di timeout
int count_millis = 0;
byte val;
byte bytesread;
byte CODE[20];
byte tempbyte;
byte checksum;
boolean Rxok;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) {
//Serial.println("serialEvent1");
checksum=0 ;
bytesread = 0;
while(bytesread < 16) {
if( Serial.available() > 0) { //controllo se i dati sono disponibili ad essere letti
val = Serial.read();
//quindi assegno a 'val' il valore dell'i-esimo carattere
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { //se leggo un carattere 'header' o un carattere di stop
break; // fermo la lettura
}
if ((val >= '0') && (val <= '9')) {
val -= '0';
} //traduco in esadecimale il carattere appena letto
else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
if (bytesread & 1 == 1) { //se ho letto un solo carattere fin'ora
CODE[bytesread >> 1] = (val | (tempbyte << 4)); //assegno alla seconda parte del byte in posizione bytesread-esima il valore esadecimale del carattere letto
if (bytesread >> 1 !=7) { //se ho letto l'ultimo byte della scheda calcolo il checksum
checksum ^= CODE[bytesread >> 1]; //facendo la XOR sull'ultimo byte letto
};
}
else {
tempbyte = val; //altrimenti assegno il valore letto alla variabile tempbyte
};
bytesread++; //mi preparo a leggere il prossimo byte
}
mem_millis = millis();
if(mem_millis != last_millis){
last_millis = mem_millis;
count_millis++;
if(count_millis > (MAX_MILLIS_TIMEOUT - 1)){
errRX1++;
break;
}
}
}
if (bytesread == 16) {
(CODE[7]==checksum) ? Rxok = true : Rxok = false ;
}
}
}
if (Rxok){
rxNODE11=CODE[0];
rxCODE11=CODE[1];
rxDATA11=CODE[2];
rxDATA12=CODE[3];
rxDATA13=CODE[4];
rxDATA14=CODE[5];
rxDATA15=CODE[6];
Rxok1 = true;
}
}
void sendMSG1(byte address,byte code,byte data1,byte data2,byte data3,byte data4,byte data5){
byte temp;
byte checksum;
UCSR0A=UCSR0A |(1 << TXC0);
digitalWrite(pinSerial1,HIGH);
delay(1);
checksum=0;
Serial.write(0x02);
temp = address >> 4 ;
Serial.print(temp ,HEX);
temp = address & 0x0F;
Serial.print(temp ,HEX);
checksum ^= address;
temp = code >> 4 ;
Serial.print(temp ,HEX);
temp = code & 0x0F;
Serial.print(temp ,HEX);
checksum ^= code;
temp = data1 >> 4 ;
Serial.print(temp ,HEX);
temp = data1 & 0x0F;
Serial.print(temp ,HEX);
checksum ^= data1;
temp = data2 >> 4 ;
Serial.print(temp ,HEX);
temp = data2 & 0x0F;
Serial.print(temp ,HEX);
checksum ^= data2;
temp = data3 >> 4 ;
Serial.print(temp ,HEX);
temp = data3 & 0x0F;
Serial.print(temp ,HEX);
checksum ^= data3;
temp = data4 >> 4 ;
Serial.print(temp ,HEX);
temp = data4 & 0x0F;
Serial.print(temp ,HEX);
checksum ^= data4;
temp = data5 >> 4 ;
Serial.print(temp ,HEX);
temp = data5 & 0x0F;
Serial.print(temp ,HEX);
checksum ^= data5;
temp = checksum >> 4 ;
Serial.print(temp ,HEX);
temp = checksum & 0x0F;
Serial.print(temp ,HEX);
while (!(UCSR0A & (1 << TXC0)));
delay(1);
digitalWrite(pinSerial1,LOW);
}
Any suggestions will be appreciate.
Thank you.
Bye