diff --git a/FC_InferfaceBoard/FC_InferfaceBoard.ino b/FC_InferfaceBoard/FC_InferfaceBoard.ino index 15f6ff5..891bdb3 100644 --- a/FC_InferfaceBoard/FC_InferfaceBoard.ino +++ b/FC_InferfaceBoard/FC_InferfaceBoard.ino @@ -3,13 +3,16 @@ #include // ?? -#include "SC_AI.h" // Differential analog input collector -#include "SC_MC9.h" // MC9 Tempreture Controller +//#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 +String demuxCMD(String command, String* rightPart); +int demuxNum(String rightPart, unsigned int data[]); + // 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: @@ -33,9 +36,14 @@ bool connection = false; char buffer[1024]; int Size_AI = 16; -int AI_VALUES[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; +int Values_AI[16]; +int Size_PV = 8; +int Values_PV[8]; + +String Buf_485; +bool Wait_485; +int Wait_485_cnt; + int msCnt = 0; unsigned long timer = 0; @@ -47,8 +55,8 @@ void setup() { TCPIP_setup(mac, ip, gateway, subnet); // scitech korea Library - SC_AI_setup(); - SC_MC9_setup(); + GPIO_setup(); + MC9_setup(); // Timer set MsTimer2::set(10, periodic_10ms); @@ -77,7 +85,8 @@ void loop() { webReponse(); client = server.available(); - + //Prcss_SV(); + //delay(100); if (client) { // check if client is connected Serial.println("Client Connected!!!"); @@ -90,31 +99,49 @@ void loop() { // check for command byte if (client.available() > 0) { char c; + bool cr = false; String command; String cmd; unsigned int data[32]; + String message; 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; + + // Read data until CR or LF + if((c != 13) && (c != 10)){ + command += c; }else{ break; - } + } } - cmd = demuxCMD(command, data, &dataSize); + // -------------------- Process CMD -------------------- // + cmd = demuxCMD(command, &message); if(cmd=="AI"){ client.print(Prcss_AI()); + }else if(cmd=="AO"){ + dataSize = demuxNum(message, data); client.print(Prcss_AO(data, dataSize)); - }else{ - client.print(RcvErr); + }else if(cmd=="PV"){ + client.print(Prcss_PV()); + }else if(cmd=="SV"){ + client.print(Prcss_SV()); + + }else if(cmd=="RS"){ + client.print(Prcss_RS(message)); + + }//else if(cmd==""){ + + //} + else{ + client.print(cmd + " " + RcvErr); + } @@ -141,7 +168,7 @@ void loop() { Serial.println("Client Disconnected..."); } - + } @@ -154,32 +181,10 @@ void TCPIP_setup(byte mac[], IPAddress ip, IPAddress gateway, IPAddress subnet){ web.begin(); } -void periodic_10ms(){ - - - msCnt += 10; - if(!busy){ - - SC_AI(AI_VALUES, Size_AI); - - // periodic_1s - if (msCnt > 1000){ - msCnt = 0; - periodic_1s(); - } - } - -} - -void periodic_1s(){ - - //AnalogIn_Print(); -} - void AnalogIn_Print(){ for (int i = 0; i < Size_AI; i++) { - Serial.print(AI_VALUES[i]); + Serial.print(Values_AI[i]); Serial.print("\t"); } Serial.println(); diff --git a/FC_InferfaceBoard/GPIO.ino b/FC_InferfaceBoard/GPIO.ino new file mode 100644 index 0000000..89087ab --- /dev/null +++ b/FC_InferfaceBoard/GPIO.ino @@ -0,0 +1,14 @@ +int aipin[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15}; + +void GPIO_setup(){ + +} + +void read_analog(int arr[], int size){ + int i = 0; + + for(i = 0 ; i < size ; i++){ + arr[i] = analogRead(aipin[i]); + } + +} \ No newline at end of file diff --git a/FC_InferfaceBoard/MC9.ino b/FC_InferfaceBoard/MC9.ino new file mode 100644 index 0000000..e1db381 --- /dev/null +++ b/FC_InferfaceBoard/MC9.ino @@ -0,0 +1,65 @@ +// Sample communication Code for RS 485 Shield with Arduino Mega 2560 +#define RS485_OE_1 22 //RS485 CH#1 Output Enable => pin 22 for CH#1 +#define Snd_485 HIGH +#define Rcv_485 LOW + +void MC9_setup(){ + Serial1.setTimeout(300); + Serial1.begin(9600); + pinMode(RS485_OE_1, OUTPUT); + delay(10); + digitalWrite(RS485_OE_1, Rcv_485); +} + +void read_mc9_req(){ + if(!Wait_485){ + String command = "10DRS,03,0011C7\r\n"; + + // Send request to return pv data + digitalWrite(RS485_OE_1, Snd_485); delay(5); + Serial1.print(command); + Serial1.flush(); + digitalWrite(RS485_OE_1, Rcv_485); delay(5); + Wait_485 = true; + Wait_485_cnt = 0; + } +} + +void read_mc9(int arr[], int size){ + char c; + String message; + + // Timeout code + if(Wait_485){ + Wait_485_cnt++; + if(Wait_485_cnt > 200){ // Timeout = periodic(10ms) x 200 = 2sec + Wait_485 = false; + Wait_485_cnt = 0; + } + } + // Receive pv data + while(Wait_485 && (Serial1.available() > 0)) { + c = Serial1.read(); + Buf_485 += c; + Serial.print(c, HEX); + Serial.print(" "); + + if(c == '\n'){ + message = Buf_485; + Buf_485 = ""; + Wait_485 = false; + Wait_485_cnt = 0; + } + } + + if(message != ""){ + Serial.println(message); + } + +} + +void write_mc9(int arr[], int size){ + cli();//stop interrupts + Serial1.print("485 Write^^"); //Serial Write ADC_Value to RS-485 Bus + sei();//allow interrupts +} \ No newline at end of file diff --git a/FC_InferfaceBoard/Processes.ino b/FC_InferfaceBoard/Processes.ino index a87dde4..c906066 100644 --- a/FC_InferfaceBoard/Processes.ino +++ b/FC_InferfaceBoard/Processes.ino @@ -3,7 +3,7 @@ String Prcss_AI(){ String str = ""; for (int i = 0; i < Size_AI; i++) { char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 - sprintf(formattedNumber, "%04X", AI_VALUES[i]); // 4자리로 고정된 형식의 문자열 생성 + sprintf(formattedNumber, "%04X", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성 str += formattedNumber; // 형식화된 문자열 추가 //str += String(AI_VALUES[i]); str += ','; @@ -22,3 +22,27 @@ String Prcss_AO(unsigned int data[], int dataSize){ Serial.println(str); return RcvOK; } + +String Prcss_RS(String message){ + Serial.println(message); + return RcvOK; +} + +String Prcss_PV(){ + + String str = "PV "; + digitalWrite(RS485_OE_1, Snd_485); + Serial1.print("485 Read!!"); //Serial Write ADC_Value to RS-485 Bus + str += RcvOK; + return str; +} + +String Prcss_SV(){ + + String str = "SV "; + + + str += RcvOK; + return str; +} + diff --git a/FC_InferfaceBoard/TCPIP - Write Read.vi b/FC_InferfaceBoard/TCPIP - Write Read.vi index 5709c64..5ea2f96 100644 Binary files a/FC_InferfaceBoard/TCPIP - Write Read.vi and b/FC_InferfaceBoard/TCPIP - Write Read.vi differ diff --git a/FC_InferfaceBoard/Utils.ino b/FC_InferfaceBoard/Utils.ino index 4ae6dbb..c6f011b 100644 --- a/FC_InferfaceBoard/Utils.ino +++ b/FC_InferfaceBoard/Utils.ino @@ -1,11 +1,16 @@ -String demuxCMD(String command, unsigned int data[], int* dataSize) { +String demuxCMD(String command, String* rightPart) { - command.replace(" ", ""); - // "::"를 기준으로 문자열을 분리 int separatorIndex = command.indexOf("::"); String leftPart = command.substring(0, separatorIndex); - String rightPart = command.substring(separatorIndex + 2); + *rightPart = command.substring(separatorIndex + 2); + + leftPart.replace(" ", ""); + return leftPart; +} + + +int demuxNum(String rightPart, unsigned int data[]) { // 우측의 16진수 배열을 분리하여 추출 const char* delimiter = ","; @@ -28,9 +33,6 @@ String demuxCMD(String command, unsigned int data[], int* dataSize) { data[index] = strtoul(hexValue.c_str(), NULL, 16); index++; - // dataSize에 배열 크기 저장 - *dataSize = index; - // 왼쪽 부분인 "ABC"을 반환 - return leftPart; + return index; } diff --git a/FC_InferfaceBoard/periodics.ino b/FC_InferfaceBoard/periodics.ino new file mode 100644 index 0000000..97c7cff --- /dev/null +++ b/FC_InferfaceBoard/periodics.ino @@ -0,0 +1,34 @@ +void periodic_10ms(){ + msCnt += 10; + + if(!busy){ + // Call a function every 10ms + read_analog(Values_AI, Size_AI); + read_mc9(Values_PV, Size_PV); + + // Call a function every 50ms + if (msCnt % 20 == 0){ + + } + + // Call a function every 50ms + if (msCnt % 50 == 0){ + + } + + // Call a function every 100ms + if (msCnt % 100 == 0){ + + } + + // Call a function every 500ms + if (msCnt % 500 == 0){ + read_mc9_req(); + } + + // Call a function every 1000ms + if (msCnt > 1000){ + msCnt = 0; + } + } +} diff --git a/libraries/Scitech/SC_AI.cpp b/libraries/Scitech/SC_AI.cpp deleted file mode 100644 index e397b9c..0000000 --- a/libraries/Scitech/SC_AI.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - History: - 20230504 - V0.2 - 20230504 - V0.1 init. -*/ - -#include - -#include -int aipin[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15}; -void SC_AI_setup(){ - -} - - -void SC_AI(int arr[], int size){ - //cli();//stop interrupts - - int i = 0; - for(i = 0 ; i < size ; i++){ - arr[i] = analogRead(aipin[i]); - } - - - //sei();//allow interrupts -} - diff --git a/libraries/Scitech/SC_AI.h b/libraries/Scitech/SC_AI.h deleted file mode 100644 index 46f34ea..0000000 --- a/libraries/Scitech/SC_AI.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef SC_AI_h -#define SC_AI_h - -void SC_AI_setup(); -void SC_AI(int arr[], int size); - -#endif diff --git a/libraries/Scitech/SC_MC9.cpp b/libraries/Scitech/SC_MC9.cpp deleted file mode 100644 index 9dd62d7..0000000 --- a/libraries/Scitech/SC_MC9.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - History: - 20230504 - V0.2 - 20230504 - V0.1 init. -*/ - -#include - -#include -//#include - -void SC_MC9_setup(){ - Serial1.begin(9600); - pinMode(RS485_OE_1, OUTPUT); - delay(10); - digitalWrite(RS485_OE_1, HIGH); -} - - -void SC_MC9_read(int arr[], int size){ - - cli();//stop interrupts - Serial1.print("485 Read!!"); //Serial Write ADC_Value to RS-485 Bus - sei();//allow interrupts - -} - -void SC_MC9_write(int arr[], int size){ - cli();//stop interrupts - Serial1.print("485 Write^^"); //Serial Write ADC_Value to RS-485 Bus - sei();//allow interrupts -} \ No newline at end of file diff --git a/libraries/Scitech/SC_MC9.h b/libraries/Scitech/SC_MC9.h deleted file mode 100644 index 0af6355..0000000 --- a/libraries/Scitech/SC_MC9.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef SC_MC9_h -#define SC_MC9_h - -// Sample communication Code for RS 485 Shield with Arduino Mega 2560 -#define RS485_OE_1 24 //RS485 CH#1 Output Enable => pin 24 for CH#1 -#define RS485_OE_2 26 //RS485 CH#2 Output Enable => pin 26 for CH#2 -#define Snd_485 HIGH -#define Rcv_485 LOW - - -void SC_MC9_setup(); -void SC_MC9_read(int arr[], int size); -void SC_MC9_write(int arr[], int size); - -#endif