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