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.
175 lines
3.4 KiB
C++
175 lines
3.4 KiB
C++
#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
|
|
|
|
|
|
// ========== ========== 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_500ms_2 = false;
|
|
bool T_1000ms = 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_PV[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();
|
|
|
|
// 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 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();
|
|
}
|
|
|
|
// If data read
|
|
if(message != ""){
|
|
cmd = demuxCMD(message, &cmdData);
|
|
message = "";
|
|
|
|
if(cmd=="AI"){
|
|
client.print(Prcss_AI());
|
|
|
|
}else if(cmd=="AO"){
|
|
dataSize = demuxNum(cmdData, data);
|
|
client.print(Prcss_AO(data, dataSize));
|
|
|
|
}else if(cmd=="PV"){
|
|
client.print(Prcss_PV());
|
|
|
|
}else if(cmd=="SV"){
|
|
dataSize = demuxNum(cmdData, data);
|
|
client.print(Prcss_SV(data, dataSize));
|
|
|
|
}else if(cmd=="RS"){
|
|
client.print(Prcss_RS(cmdData));
|
|
|
|
}//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();
|
|
}
|
|
|
|
|
|
|