diff --git a/FC_InferfaceBoard/FC_InferfaceBoard.ino b/FC_InferfaceBoard/FC_InferfaceBoard.ino index 08c503f..15f6ff5 100644 --- a/FC_InferfaceBoard/FC_InferfaceBoard.ino +++ b/FC_InferfaceBoard/FC_InferfaceBoard.ino @@ -6,23 +6,38 @@ #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 + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: -byte mac[] = { - 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 }; +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 }; IPAddress ip(192, 168, 20, 177); IPAddress gateway(192, 168, 20, 1); IPAddress subnet(255, 255, 255, 0); -// telnet defaults to port 23 -EthernetServer server(23); -boolean alreadyConnected = false; // whether or not the client was connected previously +// SCPI defaults to port 5025 +EthernetServer server(5025); +EthernetClient client; + +// HTTP defaults to port 5025 +EthernetServer web(80); + +unsigned long lastDataReceivedTime; +unsigned long timeoutPeriod = 5000; // 타임아웃 시간 (5초) + +bool busy = false; +bool connection = false; +char buffer[1024]; -int Size_AI = 8; +int Size_AI = 16; int AI_VALUES[] = { - 0, 0, 0, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +int msCnt = 0; +unsigned long timer = 0; void setup() { // put your setup code here, to run once: @@ -35,14 +50,12 @@ void setup() { SC_AI_setup(); SC_MC9_setup(); - - // - MsTimer2::set(50, AnalogIn); - MsTimer2::set(1000, AnalogIn_Print); + // Timer set + MsTimer2::set(10, periodic_10ms); MsTimer2::start(); // Report address - Serial.print("MAC:"); + Serial.print("MAC>>"); for (int i = 0; i < 6; i++) { if (mac[i] < 16) { Serial.print("0"); @@ -53,37 +66,81 @@ void setup() { } } Serial.println(); - Serial.print("IP:"); + Serial.print("IP >>"); Serial.println(Ethernet.localIP()); - - } void loop() { // wait for a new client: - EthernetClient client = server.available(); - //SC_AI(AI_VALUES, Size_AI); - // check if client is connected + + webReponse(); + client = server.available(); + + if (client) { + // check if client is connected + Serial.println("Client Connected!!!"); + + // Do What Message Command + while(client.connected()){ + + int buffCnt = 0; + + // check for command byte + if (client.available() > 0) { + char c; + + String command; + String cmd; + unsigned int data[32]; + int dataSize; + + busy = true; + // Read message by byte + while ((c = client.read())) { + // Read data until LF or CR + if((c != 10) && (c != 13)){ + command += c; + }else{ + break; + } + } + + cmd = demuxCMD(command, data, &dataSize); + + if(cmd=="AI"){ + client.print(Prcss_AI()); + }else if(cmd=="AO"){ + client.print(Prcss_AO(data, dataSize)); + }else{ + client.print(RcvErr); + } + + + // Debug + if(MODE_DEBUG){ + //Serial.print("Received command: "); + //Serial.println(command); + } + + busy = false; + // 데이터를 수신했으므로 타임아웃 타이머 초기화 + lastDataReceivedTime = millis(); + + }// if end, client.available() + //Serial.println("end client available"); + + // 타임아웃 확인 + if (millis() - lastDataReceivedTime > timeoutPeriod) { + Serial.println("Client Disconnected... (Timeout)"); + client.stop(); + } - // check for command byte - if (client.available() > 0) { - cli();//stop interrupts - Serial.print(" "); - byte command = client.read(); - if (command == 'A'){ - client.print("AI"); - SC_MC9_read(AI_VALUES, Size_AI); - } - if (command == 'B'){ - client.print("AO"); - SC_MC9_write(AI_VALUES, Size_AI); - } - sei();//allow interrupts } - } - //delay(50); + Serial.println("Client Disconnected..."); + + } } @@ -94,17 +151,69 @@ void TCPIP_setup(byte mac[], IPAddress ip, IPAddress gateway, IPAddress subnet){ Ethernet.begin(mac, ip, gateway, subnet); // start listening for clients server.begin(); + web.begin(); } -void AnalogIn(){ - SC_AI(AI_VALUES, Size_AI); +void periodic_10ms(){ + + + msCnt += 10; + if(!busy){ + + SC_AI(AI_VALUES, Size_AI); + + // periodic_1s + if (msCnt > 1000){ + msCnt = 0; + periodic_1s(); + } + } + } -void AnalogIn_Print(){ +void periodic_1s(){ - for (int i = 0; i < 8; i++) { + //AnalogIn_Print(); +} + + +void AnalogIn_Print(){ + for (int i = 0; i < Size_AI; i++) { Serial.print(AI_VALUES[i]); - + Serial.print("\t"); } Serial.println(); +} + + + +void webReponse(){ + EthernetClient webClient = web.available(); + if (webClient) { + Serial.println("Web client connected"); + while (webClient.connected()) { + if (webClient.available()) { + // 웹 브라우저에서 요청이 도착한 경우 + String request = webClient.readStringUntil('\r'); + //Serial.println(request); + webClient.flush(); + + // 웹 브라우저에 출력할 문구 작성 + String response = "HTTP/1.1 200 OK\r\n"; + response += "Content-Type: text/html\r\n\r\n"; + response += "
"; + response += Prcss_AI(); + //response += "