#define RS485_OE_1 22 //RS485 CH#1 Output Enable => pin 22 for CH#1 #define RS485_OE_2 23 //RS485 CH#2 Output Enable => pin 23 for CH#2 #define Snd_485 HIGH #define Rcv_485 LOW void RS485_setup(){ Serial1.setTimeout(300); Serial1.begin(9600); pinMode(RS485_OE_1, OUTPUT); delay(10); digitalWrite(RS485_OE_1, Rcv_485); Serial2.setTimeout(300); Serial2.begin(9600); pinMode(RS485_OE_2, OUTPUT); delay(10); digitalWrite(RS485_OE_2, Rcv_485); } void send_485_1(){ if(!Wait_485_1){ String message = read_buff(Buff_485_1_Wr); if(message != ""){ //save sent message info. for received data processing latest_sent_msg_1 = message; message += CRLF; digitalWrite(RS485_OE_1, Snd_485); delay(5); Serial1.print(message); returnTime = millis(); Serial1.flush(); digitalWrite(RS485_OE_1, Rcv_485); delay(5); Wait_485_1 = true; Wait_485_1_cnt = 0; } } } void send_485_2(){ if(!Wait_485_2){ String message = read_buff(Buff_485_2_Wr); if(message != ""){ //save sent message info. for received data processing latest_sent_msg_2 = message; message += CRLF; digitalWrite(RS485_OE_2, Snd_485); delay(5); Serial2.print(message); returnTime = millis(); Serial2.flush(); digitalWrite(RS485_OE_2, Rcv_485); delay(5); Wait_485_2 = true; Wait_485_2_cnt = 0; } } } int recieve_485_1(){ // Timeout code if(Wait_485_1){ Wait_485_1_cnt++; if(Wait_485_1_cnt > 40){ // Timeout = periodic(20ms) x 25 = 500 msec //Serial.println("485 not responced... (Timeout)"); timeoutMC9_1(); latest_sent_msg_1 = ""; Wait_485_1 = false; Wait_485_1_cnt = 0; numOf485--; return -1; } } // Receive pv data while(Wait_485_1 && (Serial1.available() > 0)) { char c = Serial1.read(); write_buff_c(Buff_485_1_Rd, c); } String message = read_buff(Buff_485_1_Rd); if(message != ""){ numOf485--; if(latest_sent_msg_1.indexOf("DRS") != -1){ saveMC9_1(message); } else{ Serial.print("----- 485 rcv : "); Serial.println(message); } Wait_485_1 = false; Wait_485_1_cnt = 0; } return 0; } int recieve_485_2(){ // Timeout code if(Wait_485_2){ Wait_485_2_cnt++; if(Wait_485_2_cnt > 40){ // Timeout = periodic(20ms) x 25 = 500 msec //Serial.println("485 not responced... (Timeout)"); timeoutMC9_2(); latest_sent_msg_2 = ""; Wait_485_2 = false; Wait_485_2_cnt = 0; numOf485--; return -1; } } // Receive pv data while(Wait_485_2 && (Serial2.available() > 0)) { char c = Serial2.read(); write_buff_c(Buff_485_2_Rd, c); } String message = read_buff(Buff_485_2_Rd); if(message != ""){ numOf485--; if(latest_sent_msg_2.indexOf("DRS") != -1){ saveMC9_2(message); } else{ Serial.print("----- 485 rcv : "); Serial.println(message); } Wait_485_2 = false; Wait_485_2_cnt = 0; } return 0; } /* For Test from PC */ void recieve_485_0(){ while(Serial.available() > 0) { char c = Serial.read(); IdeSerial += c; } if(IdeSerial.endsWith("\n")){ IdeSerial.replace(CRLF,""); IdeSerial += sumMC9(IdeSerial); IdeSerial = "" + IdeSerial + CRLF; write_buff_first(Buff_485_1_Wr, IdeSerial); IdeSerial = ""; } } void send_temp(bool F){ String message = "TEST"; if(F) { message += CRLF; message += " with CRLF"; message += CRLF; } digitalWrite(RS485_OE_1, Snd_485); delay(5); Serial1.print(message); returnTime = millis(); Serial1.flush(); digitalWrite(RS485_OE_1, Rcv_485); delay(5); }