diff --git a/FC_InferfaceBoard/Data_process.vi b/FC_InferfaceBoard/Data_process.vi index 8f5b16f..549243a 100644 Binary files a/FC_InferfaceBoard/Data_process.vi and b/FC_InferfaceBoard/Data_process.vi differ diff --git a/FC_InferfaceBoard/FC_InferfaceBoard.ino b/FC_InferfaceBoard/FC_InferfaceBoard.ino index b4b35dd..be95fc5 100644 --- a/FC_InferfaceBoard/FC_InferfaceBoard.ino +++ b/FC_InferfaceBoard/FC_InferfaceBoard.ino @@ -1,13 +1,15 @@ +#include "Arduino.h" #include // Timer #include - #include // ?? +#include "Wire.h" // I2C +#include "DFRobot_GP8403.h" - - +#define CR "\r" +#define FIN "\n" #define CRLF "\r\n" -#define RcvOK "OK\r\n" -#define RcvErr "ER\r\n" +#define RcvOK "OK\r" +#define RcvErr "ER\r" #define MODE_DEBUG false String IdeSerial; @@ -50,6 +52,10 @@ String Buf_485; bool Wait_485; int Wait_485_cnt; +// ---------- I2C (Analog Output, DAC) +DFRobot_GP8403 AO_0(&Wire,0x58); +int voltOffset = 185; + // ========== ========== Processing // Read Datas @@ -64,8 +70,12 @@ int Size_DI = 8; int Values_DI; int Size_AI = 16; int Values_AI[16]; + +bool rcv_10_PV; int Size_PV = 8; int Values_10_PV[8]; + +bool rcv_10_SV; int Size_SV = 8; int Values_10_SV[8]; @@ -82,10 +92,13 @@ void setup() { GPIO_setup(); //MC9_setup(); RS485_setup(); + GP8403_setup(); // Timer set MsTimer2::set(10, timer_10ms); MsTimer2::start(); + + } void loop() { @@ -125,13 +138,18 @@ void loop() { // 데이터를 수신했으므로 타임아웃 타이머 초기화 lastDataReceivedTime = millis(); } + message = read_buff(Buff_Eth_Rd); // If data read if(message != ""){ cmd = demuxCMD(message, &cmdData); message = ""; + + //Serial.println(cmd); + if(cmd=="ALL?"){ + client.print(Prcss_ALL_Read()); - if(cmd=="DO!"){ + }else if(cmd=="DO!"){ dataSize = demuxNum(cmdData, data); client.print(Prcss_DO_Write(data, dataSize)); @@ -156,11 +174,16 @@ void loop() { dataSize = demuxNum(cmdData, data); client.print(Prcss_SV_Write(data, dataSize)); - }//else if(cmd==""){ + }else if(cmd=="ATon!"){ + client.print(Prcss_AT_Write(true)); + }else if(cmd=="AToff!"){ + client.print(Prcss_AT_Write(false)); + } + //else if(cmd==""){ //} else{ - client.print(cmd + " " + RcvErr); + client.print(cmd + " " + RcvErr + FIN); } message = ""; } diff --git a/FC_InferfaceBoard/GPIO.ino b/FC_InferfaceBoard/GPIO.ino index 86beb9a..f6f7422 100644 --- a/FC_InferfaceBoard/GPIO.ino +++ b/FC_InferfaceBoard/GPIO.ino @@ -5,6 +5,8 @@ void GPIO_setup(){ for (int i = 0; i < Size_DO; i++) { pinMode(DoPin[i], OUTPUT); } + + analogReference(EXTERNAL); } void read_analog(){ diff --git a/FC_InferfaceBoard/MC9.ino b/FC_InferfaceBoard/MC9.ino index 350b99c..fceb7d4 100644 --- a/FC_InferfaceBoard/MC9.ino +++ b/FC_InferfaceBoard/MC9.ino @@ -6,6 +6,51 @@ const int MC9_CH[] = {1000, 1008, 1016, 1024, 1100, 1108, 1116, 1124}; // 10DWR,02,0302,0001,0501,0001 // Auto Tune. 0302(Ch Sel) 0001(Ch.1) 0501(AT) 0001(On, Off=0000) +int saveMC9(String message){ + int addr; + String mode; + int data[8]; + int crc; + + if (parseMC9(message, addr, mode, data, crc)) { + if(latest_sent_msg == MC9_10_PV){ + rcv_10_PV = true; + for(int i = 0 ; i < Size_PV ; i++){ + Values_10_PV[i] = data[i]; + } + } + if(latest_sent_msg == MC9_10_SV){ + rcv_10_SV = true; + for(int i = 0 ; i < Size_SV ; i++){ + Values_10_SV[i] = data[i]; + } + } + return 1; + } else { + Serial.println("error 485 read"); + return 0; + } +} + +int timeoutMC9(){ + int addr; + String mode; + int data[8]; + int crc; + if(latest_sent_msg == MC9_10_PV){ + rcv_10_PV = false; + } + if(latest_sent_msg == MC9_10_SV){ + rcv_10_SV = false; + + }else { + Serial.println("error 485 read"); + return 0; + } + + return 1; +} + int parseMC9(const String& message, int& addr, String& mode, int data[8], int& crc) { char addrC[3], modeC[4], statusC[3]; int dataC[8], crcC; diff --git a/FC_InferfaceBoard/Processes.ino b/FC_InferfaceBoard/Processes.ino index c4c9d22..ec23fd0 100644 --- a/FC_InferfaceBoard/Processes.ino +++ b/FC_InferfaceBoard/Processes.ino @@ -1,9 +1,57 @@ +String Prcss_ALL_Read(){ + + String str = ""; + str += "AI?:"; + for (int i = 0; i < Size_AI; i++) { + char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 + sprintf(formattedNumber, "%04X", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성 + str += formattedNumber; // 형식화된 문자열 추가 + str += ','; + } + str += RcvOK; + + + str += "DI?:"; + char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 + sprintf(formattedNumber, "%04X", Values_DI); // 4자리로 고정된 형식의 문자열 생성 + str += formattedNumber; // 형식화된 문자열 추가 + str += ','; + str += RcvOK; + + str += "PV10?:"; + if(rcv_10_PV){ + for (int i = 0; i < Size_PV; i++) { + char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 + sprintf(formattedNumber, "%04X", Values_10_PV[i]); // 4자리로 고정된 형식의 문자열 생성 + str += formattedNumber; // 형식화된 문자열 추가 + str += ','; + } + str += RcvOK; + }else{ + str += RcvErr; + } + + str += "SV10?:"; + if(rcv_10_SV){ + for (int i = 0; i < Size_SV; i++) { + char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 + sprintf(formattedNumber, "%04X", Values_10_SV[i]); // 4자리로 고정된 형식의 문자열 생성 + str += formattedNumber; // 형식화된 문자열 추가 + str += ','; + } + str += RcvOK; + }else{ + str += RcvErr; + } + return str + FIN; +} + String Prcss_DO_Write(unsigned int data[], int dataSize){ String str = "DO!:"; if(dataSize != 1){ str += RcvErr; - return str; + return str + FIN; } unsigned int data0 = data[0]; @@ -12,7 +60,7 @@ String Prcss_DO_Write(unsigned int data[], int dataSize){ } str += RcvOK; - return str; + return str + FIN; } String Prcss_DI_Read(){ String str = "DI?:"; @@ -25,7 +73,7 @@ String Prcss_DI_Read(){ //} str += RcvOK; - return str; + return str + FIN; } // String Prcss_AI_Read(){ @@ -39,17 +87,30 @@ String Prcss_AI_Read(){ } str += RcvOK; - return str; + return str + FIN; } String Prcss_AO(unsigned int data[], int dataSize){ String str = "AO!:"; + int t = millis(); for(int i=0 ; i 200){ // Timeout = periodic(100ms) x 200 = 2 sec + if(Wait_485_cnt > 25){ // Timeout = periodic(20ms) x 25 = 500 msec Serial.println("485 not responced... (Timeout)"); + + if((latest_sent_msg == MC9_10_PV) ||(latest_sent_msg == MC9_10_SV)){ + timeoutMC9(); + } + latest_sent_msg = ""; Wait_485 = false; Wait_485_cnt = 0; numOf485--; + + return 0; } } @@ -58,31 +65,9 @@ int recieve_485(){ String message = read_buff(Buff_485_Rd); if(message != ""){ - - int addr; - String mode; - int data[8]; - int crc; numOf485--; - - - /* To be moved @MC0.ino */ if((latest_sent_msg == MC9_10_PV) ||(latest_sent_msg == MC9_10_SV)){ - if (parseMC9(message, addr, mode, data, crc)) { - - if(latest_sent_msg == MC9_10_PV){ - for(int i = 0 ; i < Size_PV ; i++){ - Values_10_PV[i] = data[i]; - } - } - if(latest_sent_msg == MC9_10_SV){ - for(int i = 0 ; i < Size_SV ; i++){ - Values_10_SV[i] = data[i]; - } - } - } else { - Serial.println("error 485 read"); - } + saveMC9(message); } else{ Serial.print("485 rcv : "); diff --git a/FC_InferfaceBoard/TCPIP - Write Read.vi b/FC_InferfaceBoard/TCPIP - Write Read.vi index fbd896b..9425bba 100644 Binary files a/FC_InferfaceBoard/TCPIP - Write Read.vi and b/FC_InferfaceBoard/TCPIP - Write Read.vi differ