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_InferfaceBoard/FC_InferfaceBoard.ino

193 lines
4.0 KiB
Arduino

#include <MsTimer2.h> // Timer
#include <Ethernet2.h>
#include <avr/wdt.h> // ??
//#include "SC_AI.h" // Differential analog input collector
//#include "SC_MC9.h" // MC9 Tempreture Controller
#define RcvOK "OK\r\n"
#define RcvErr "ER\r\n"
#define MODE_DEBUG false
const int BUFF_SIZE = 512;
char Buff_Eth_Rd[BUFF_SIZE] = {0};
char Buff_485_Wr[BUFF_SIZE] = {0};
char Buff_485_Rd[BUFF_SIZE] = {0};
String latest_sent_msg;
int numOf485 = 0;
int returnTime = 0;
// ========== ========== Periodic Flags
bool T_10ms = false;
bool T_20ms = false;
bool T_50ms = false;
bool T_100ms = false;
bool T_200ms = false;
bool T_500ms = false;
bool T_1000ms = false;
bool T_2000ms = false;
bool T_5000ms = false;
// ========== ========== Communication
// ---------- Ethernet
// Ethernet Client
EthernetClient client;
// SCPI defaults to port 5025
EthernetServer server(5025);
// HTTP defaults to port 5025
EthernetServer web(80);
unsigned long lastDataReceivedTime;
unsigned long timeoutPeriod = 5000; // 타임아웃 시간 (5초)
bool State_eth = false;
// ---------- 485
String Buf_485;
bool Wait_485;
int Wait_485_cnt;
// ========== ========== Processing
// Read Datas
int Size_AI = 16;
int Values_AI[16];
int Size_PV = 8;
int Values_10_PV[8];
int Size_SV = 8;
int Values_10_SV[8];
int msCnt = 0;
unsigned long timer = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Start!");
// modules setup (init.)
Ethernet_setup();
GPIO_setup();
//MC9_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);
}
/*
//If endwith CRLF
if((c == 10) && (Buf_eth.endsWith("\r"))){
Buf_eth.remove(Buf_eth.length() - 1); //remove CR
message = Buf_eth;
Buf_eth = "";
break;
}else{
Buf_eth += c;
}
*/
}
// 데이터를 수신했으므로 타임아웃 타이머 초기화
lastDataReceivedTime = millis();
}
message = read_buff(Buff_Eth_Rd);
// If data read
if(message != ""){
cmd = demuxCMD(message, &cmdData);
message = "";
if(cmd=="AI?"){
client.print(Prcss_AI_Read());
}else if(cmd=="AO!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_AO(data, dataSize));
}else if(cmd=="PV?"){
client.print(Prcss_PV_Read());
}else if(cmd=="SV?"){
client.print(Prcss_SV_Read());
}else if(cmd=="SV!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_SV_Write(data, dataSize));
}//else if(cmd==""){
//}
else{
client.print(cmd + " " + RcvErr);
}
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();
}