You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IFB_dev/FC_InterfaceBoard/FC_InterfaceBoard.ino

189 lines
4.0 KiB
Arduino

#include "FC_InterfaceBoard.h"
#define CR "\r"
#define FIN "\n"
#define CRLF "\r\n"
#define RcvOK "OK\r"
#define RcvErr "ER\r"
#define MODE_DEBUG false
String IdeSerial; // for 485_0
HardwareSerial* Serials[] = {&Serial1, &Serial2};
const int BUFF_SIZE = 512;
char Buff_Eth_Rd[BUFF_SIZE] = {0};
char Buff_485_1_Wr[BUFF_SIZE] = {0};
char Buff_485_1_Rd[BUFF_SIZE] = {0};
char Buff_485_2_Wr[BUFF_SIZE] = {0};
char Buff_485_2_Rd[BUFF_SIZE] = {0};
String latest_sent_msg;
int numOf485 = 0;
int returnTime = 0;
// ---------- 485
bool Wait_485_1;
int Wait_485_1_cnt;
int RS485_1_Addr[2] = {0,0};
bool RS485_1_Rcv_PV[2] = {false, false};
bool RS485_1_Rcv_SV[2] = {false, false};
int RS485_1_Rcv_size = 8;
int RS485_1_Values_PV[8*2];
int RS485_1_Values_SV[8*2];
bool Wait_485_2;
int Wait_485_2_cnt;
int RS485_2_Addr[2];
bool RS485_2_Rcv_PV[2];
bool RS485_2_Rcv_SV[2];
int RS485_2_Rcv_size = 8;
int RS485_2_Values_PV[8*2];
int RS485_2_Values_SV[8*2];
//bool rcv_10_PV = true;
//int Size_PV = 8;
//int Values_10_PV[8];
//bool rcv_10_SV = true;
//int Size_SV = 8;
//int Values_10_SV[8];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Start!");
// modules setup (init.)
Ethernet_setup();
GPIO_setup();
RS485_setup();
// Timer set
MsTimer2::set(10, timer_10ms);
MsTimer2::start();
}
void loop() {
// wait for a new client:
//webReponse();
client = server.available();
if (client) {
String Buf_eth;
String message;
String cmd;
String cmdData;
unsigned int data[32];
int dataSize;
// check if client is connected
Serial.println("Client Connected!!!");
// Do What Message Command
while(client.connected()){
// read data check
if(client.available() > 0) {
char c ;//= client.read();
while((c = client.read())) {
if((c == '\n') && (Buff_Eth_Rd[strlen(Buff_Eth_Rd)-1] == '\r')){
write_buff_c(Buff_Eth_Rd, c);
break;
}else{
write_buff_c(Buff_Eth_Rd, c);
}
}
// 데이터를 수신했으므로 타임아웃 타이머 초기화
lastDataReceivedTime = millis();
}
message = read_buff(Buff_Eth_Rd);
// If data read
if(message != ""){
cmd = demuxCMD(message, &cmdData);
message = "";
//Serial.println(cmd);
if(cmd=="ALL?"){
client.print(Prcss_ALL_Read());
}
// GPIO
else if(cmd=="DO!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_DO_Write(data, dataSize));
}else if(cmd=="AO!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_AO_Write(data, dataSize));
}
// MC9
else if(cmd=="SV_1!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_SV_Write(data, dataSize));
}else if(cmd=="AT_1!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_AT_Write(data, dataSize));
}
// Init
else if(cmd=="State?"){
}else if(cmd=="RngAO!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_RngAO(data, dataSize));
}else if(cmd=="ChMC9_1!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_ChMC9(data, dataSize));
}
// Extra
else{
client.print(cmd + " " + RcvErr + FIN);
}
message = "";
}
// Debug
if(MODE_DEBUG){
//Serial.print("Received command: ");
//Serial.println(command);
}
// check Timeout
if (millis() - lastDataReceivedTime > timeoutPeriod) {
Serial.println("Client Disconnected... (Timeout)");
client.stop();
}
Periodic_run();
}
Serial.println("Client Disconnected...");
}
Periodic_run();
}
void AnalogIn_Print(){
for (int i = 0; i < Size_AI; i++) {
Serial.print(Values_AI[i]);
Serial.print("\t");
}
Serial.println();
}