- ENC28J60 Ethernet 기반 업데이트
- 485 두개 사용할 수 있도록 업데이트
- LabView 라이브러리는 두번째 485 사용함수 미작성
- LabView 의 글로벌 변수 별도 선언
devIFB_v2
Changwoo Park 12 months ago
parent 2789e42c77
commit 5b16385aa5

@ -1,16 +1,24 @@
void Ethernet_setup(){ void Ethernet_setup(){
/*
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 }; byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 };
IPAddress ip(192, 168, 20, 177); IPAddress ip(192, 168, 20, 177);
IPAddress gateway(192, 168, 20, 1); IPAddress gateway(192, 168, 20, 1);
IPAddress subnet(255, 255, 255, 0); IPAddress subnet(255, 255, 255, 0);
*/
uint8_t mac[6] = {MACADDRESS};
uint8_t myIP[4] = {MYIPADDR};
uint8_t myMASK[4] = {MYIPMASK};
uint8_t myDNS[4] = {MYDNS};
uint8_t myGW[4] = {MYGW};
// Mac as unique value by change last byte from ip last value. // Mac as unique value by change last byte from ip last value.
mac[5] = ip[3] & 0xFF; //mac[5] = ip[3] & 0xFF;
// initialize the ethernet device // initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet); Ethernet.begin(mac,myIP,myDNS,myGW,myMASK);
// start listening for clients // start listening for clients
server.begin(); server.begin();
web.begin(); //web.begin();
// Report address // Report address
Serial.print("MAC>>"); Serial.print("MAC>>");
@ -28,36 +36,6 @@ void Ethernet_setup(){
Serial.println(Ethernet.localIP()); Serial.println(Ethernet.localIP());
} }
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 += "<html><body>";
response += Prcss_AI_Read();
//response += "<h1>Hello, World!</h1>";
response += "</body></html>";
// 문구를 웹 브라우저로 전송
webClient.print(response);
delay(1);
// 클라이언트 연결 종료
webClient.stop();
}
}
}
}
String demuxCMD(String command, String* rightPart) { String demuxCMD(String command, String* rightPart) {

@ -1,12 +1,37 @@
#include "Arduino.h" #include "Arduino.h"
#include <MsTimer2.h> // MsTimer2 v1.1 #include <MsTimer2.h> // MsTimer2 v1.1
#include <Ethernet2.h> // Ethernet2 v1.0.4 //#include <Ethernet2.h> // Ethernet2 v1.0.4
// for ENC28J60
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPClient.h>
// ------------------
#include "GP8403.h" #include "GP8403.h"
#define VERSION "23-12-20 09:57"
#define COMMENT "MC9: 2x2=4"
#define AI_RANGE 10
#define AI_AVG_SIZE 20
/* ---------- Ethernet (ENC) ---------- */
#define MACADDRESS 0x00,0x01,0x02,0x03,0x04,0x05
#define MYIPADDR 192,168,20,177
#define MYIPMASK 255,255,255,0
#define MYDNS 192,168,20,1
#define MYGW 192,168,20,1
#define LISTENPORT 5025
/* ---------- Init ---------- */
bool Init_AO = false;
bool Init_MC9 = false;
/* ---------- Periodics ---------- */ /* ---------- Periodics ---------- */
// Flags // Flags
bool T_1ms = false; bool T_1ms = false;
bool T_2ms = false; bool T_2ms = false;
bool T_4ms = false;
bool T_5ms = false; bool T_5ms = false;
bool T_10ms = false; bool T_10ms = false;
bool T_20ms = false; bool T_20ms = false;
@ -21,8 +46,6 @@ bool T_5000ms = false;
// Datas // Datas
int msCnt = 0; int msCnt = 0;
unsigned long timer = 0; unsigned long timer = 0;
#define AI_AVG_SIZE 50
#define AI_RANGE 8
/* ---------- Arduino Mega 2560 Basic IO ---------- */ /* ---------- Arduino Mega 2560 Basic IO ---------- */
// Pins // Pins
@ -56,13 +79,10 @@ DFRobot_GP8403 AO_14(&Wire,0x5F);
// Data Storage // Data Storage
int RngAO[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int RngAO[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/* ---------- Ethernet (Ethernet Sheld2) ---------- */ /* ---------- Ethernet (ENC28J60) ---------- */
// Ethernet Client
EthernetClient client; EthernetClient client;
// SCPI defaults to port 5025 EthernetServer server = EthernetServer(LISTENPORT);
EthernetServer server(5025);
// HTTP defaults to port 5025
EthernetServer web(80);
unsigned long lastDataReceivedTime; unsigned long lastDataReceivedTime;
unsigned long timeoutPeriod = 5000; // 이더넷 타임아웃 시간 (5초) unsigned long timeoutPeriod = 5000; // 이더넷 타임아웃 시간 (5초)
@ -71,17 +91,21 @@ bool State_eth = false;
HardwareSerial* Serials[] = {&Serial1, &Serial2}; HardwareSerial* Serials[] = {&Serial1, &Serial2};
const int BUFF_SIZE = 1024; const int BUFF_SIZE = 512;
char Buff_Eth_Rd[BUFF_SIZE] = {0}; char Buff_Eth_Rd[BUFF_SIZE] = {0};
char Buff_485_1_Wr[BUFF_SIZE] = {0}; char Buff_485_1_Wr[BUFF_SIZE] = {0};
char Buff_485_1_Rd[BUFF_SIZE] = {0}; char Buff_485_1_Rd[BUFF_SIZE] = {0};
char Buff_485_2_Wr[BUFF_SIZE] = {0}; char Buff_485_2_Wr[BUFF_SIZE] = {0};
char Buff_485_2_Rd[BUFF_SIZE] = {0}; char Buff_485_2_Rd[BUFF_SIZE] = {0};
String latest_sent_msg; String latest_sent_msg_1;
int numOf485 = 0; int numOf485 = 0;
int returnTime = 0; int returnTime = 0;
String latest_sent_msg_2;
int numOf485_2 = 0;
int returnTime_2 = 0;
/* ---------- RS485 ---------- */ /* ---------- RS485 ---------- */
// 485 CH 1 // 485 CH 1
bool Wait_485_1; bool Wait_485_1;
@ -96,9 +120,9 @@ int RS485_1_Values_SV[8*2];
// 485 CH 2 // 485 CH 2
bool Wait_485_2; bool Wait_485_2;
int Wait_485_2_cnt; int Wait_485_2_cnt;
int RS485_2_Addr[2]; int RS485_2_Addr[2] = {0,0};
bool RS485_2_Rcv_PV[2]; bool RS485_2_Rcv_PV[2] = {false, false};
bool RS485_2_Rcv_SV[2]; bool RS485_2_Rcv_SV[2] = {false, false};
int RS485_2_Rcv_size = 8; int RS485_2_Rcv_size = 8;
int RS485_2_Values_PV[8*2]; int RS485_2_Values_PV[8*2];
int RS485_2_Values_SV[8*2]; int RS485_2_Values_SV[8*2];
@ -112,10 +136,13 @@ String Prcss_AI_Read();
String Prcss_AO_Write(unsigned int data[], int dataSize); String Prcss_AO_Write(unsigned int data[], int dataSize);
String Prcss_PV_Read(); String Prcss_PV_Read();
String Prcss_SV_Read(); String Prcss_SV_Read();
String Prcss_SV_Write(unsigned int data[], int dataSize); String Prcss_SV_Write_1(unsigned int data[], int dataSize);
String Prcss_AT_Write(unsigned int data[], int dataSize); String Prcss_AT_Write_1(unsigned int data[], int dataSize);
String Prcss_SV_Write_2(unsigned int data[], int dataSize);
String Prcss_AT_Write_2(unsigned int data[], int dataSize);
String Prcss_RngAO(unsigned int data[], int dataSize); String Prcss_RngAO(unsigned int data[], int dataSize);
String Prcss_ChMC9(unsigned int data[], int dataSize); String Prcss_ChMC9_1(unsigned int data[], int dataSize);
String Prcss_ChMC9_2(unsigned int data[], int dataSize);
//Ethernet //Ethernet
void Ethernet_setup(); void Ethernet_setup();
@ -125,8 +152,10 @@ int demuxNum(String rightPart, unsigned int data[]);
//RS485 //RS485
void RS485_setup(); void RS485_setup();
void send_485(); void send_485_1();
int recieve_485(); void send_485_2();
int recieve_485_1();
int recieve_485_2();
void recieve_485_0(); void recieve_485_0();
//GPIO //GPIO
@ -136,10 +165,13 @@ void read_digital();
//MC9 //MC9
void setupMC9_1(int i, int data); void setupMC9_1(int i, int data);
String msg_MC9_PV(int addr); void setupMC9_2(int i, int data);
String msg_MC9_SV(int addr); String msg_MC9_PV_read(int addr);
int saveMC9(String message); String msg_MC9_SV_read(int addr);
int timeoutMC9(); int saveMC9_1(String message);
int saveMC9_2(String message);
int timeoutMC9_1();
int timeoutMC9_2();
int parseMC9(const String& message, int& addr, String& mode, int data[8], int& crc); int parseMC9(const String& message, int& addr, String& mode, int data[8], int& crc);
String sumMC9(String input); String sumMC9(String input);

@ -10,11 +10,16 @@
String IdeSerial; // for 485_0 String IdeSerial; // for 485_0
void setup() { void setup() {
// Serial setup (Debug) // Serial setup (Debug)
Serial.begin(9600); Serial.begin(9600);
Serial.println("Start!"); Serial.print("Version ::: ");
Serial.println(VERSION);
Serial.print("Comment ::: ");
Serial.println(COMMENT);
// modules setup (init.) // modules setup (init.)
Ethernet_setup(); Ethernet_setup();
GPIO_setup(); GPIO_setup();
@ -87,11 +92,20 @@ void loop() {
// MC9 // MC9
else if(cmd=="SV_1!"){ else if(cmd=="SV_1!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_SV_Write(data, dataSize)); client.print(Prcss_SV_Write_1(data, dataSize));
}else if(cmd=="AT_1!"){ }else if(cmd=="AT_1!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_AT_Write(data, dataSize)); client.print(Prcss_AT_Write_1(data, dataSize));
}
else if(cmd=="SV_2!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_SV_Write_2(data, dataSize));
}else if(cmd=="AT_2!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_AT_Write_2(data, dataSize));
} }
// Init // Init
@ -101,11 +115,15 @@ void loop() {
}else if(cmd=="RngAO!"){ }else if(cmd=="RngAO!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_RngAO(data, dataSize)); client.print(Prcss_RngAO(data, dataSize));
Init_AO = true;
}else if(cmd=="ChMC9_1!"){ }else if(cmd=="ChMC9_1!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_ChMC9(data, dataSize)); client.print(Prcss_ChMC9_1(data, dataSize));
Init_MC9 = true;
}else if(cmd=="ChMC9_2!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_ChMC9_2(data, dataSize));
Init_MC9 = true;
} }
// Extra // Extra

@ -7,10 +7,15 @@ const int MC9_CH[] = {1000, 1008, 1016, 1024, 1100, 1108, 1116, 1124};
void setupMC9_1(int i, int data){ void setupMC9_1(int i, int data){
RS485_1_Addr[i] = data; RS485_1_Addr[i] = data;
Serial.println("MC9 No."+ String(i) + " 's address set as " + String(data)); Serial.println("MC9_1 No."+ String(i) + " 's address set as " + String(data));
} }
void setupMC9_2(int i, int data){
RS485_2_Addr[i] = data;
String msg_MC9_PV(int addr){ Serial.println("MC9_2 No."+ String(i) + " 's address set as " + String(data));
}
String msg_MC9_PV_read(int addr){
String message = ""; String message = "";
if(addr < 10){ if(addr < 10){
@ -24,7 +29,7 @@ String msg_MC9_PV(int addr){
return message; return message;
} }
String msg_MC9_SV(int addr){ String msg_MC9_SV_read(int addr){
String message = ""; String message = "";
if(addr < 10){ if(addr < 10){
@ -38,10 +43,13 @@ String msg_MC9_SV(int addr){
return message; return message;
} }
int msg_MC9_SV_set(unsigned int data[], int dataSize){ int msg_MC9_SV_set_1(unsigned int data[], int dataSize){
String message = ""; String message = "";
int lp0, lp1; int lp0, lp1;
if(dataSize > 16){
dataSize = 16;
}
if(dataSize > 8){ if(dataSize > 8){
lp0 = 8; lp0 = 8;
lp1 = dataSize; lp1 = dataSize;
@ -91,13 +99,14 @@ int msg_MC9_SV_set(unsigned int data[], int dataSize){
} }
message += sumMC9(message); message += sumMC9(message);
message = "" + message + CRLF; message = "" + message + CRLF;
write_buff_first(Buff_485_1_Wr, message); write_buff_first(Buff_485_1_Wr, message);
} }
return 0; return 0;
} }
int msg_MC9_AT_set(unsigned int data[], int dataSize){ int msg_MC9_AT_set_1(unsigned int data[], int dataSize){
String message = ""; String message = "";
bool at[16]; bool at[16];
@ -126,7 +135,6 @@ int msg_MC9_AT_set(unsigned int data[], int dataSize){
}else{ }else{
atFlag = "0000"; // Auto Tuen Off atFlag = "0000"; // Auto Tuen Off
} }
message += "0302,"; // Ch No cmd. message += "0302,"; // Ch No cmd.
message += "000" + String(i+1) + ","; // Ch No. message += "000" + String(i+1) + ","; // Ch No.
message += "0501,"; // AT cmd. message += "0501,"; // AT cmd.
@ -137,6 +145,7 @@ int msg_MC9_AT_set(unsigned int data[], int dataSize){
} }
message += sumMC9(message); message += sumMC9(message);
message = "" + message + CRLF; message = "" + message + CRLF;
write_buff_first(Buff_485_1_Wr, message); write_buff_first(Buff_485_1_Wr, message);
} }
@ -167,11 +176,146 @@ int msg_MC9_AT_set(unsigned int data[], int dataSize){
message = "" + message + CRLF; message = "" + message + CRLF;
write_buff_first(Buff_485_1_Wr, message); write_buff_first(Buff_485_1_Wr, message);
} }
return 0;
}
int msg_MC9_SV_set_2(unsigned int data[], int dataSize){
String message = "";
int lp0, lp1;
if(dataSize > 16){
dataSize = 16;
}
if(dataSize > 8){
lp0 = 8;
lp1 = dataSize;
}else{
lp0 = dataSize;
lp1 = 0;
}
if(RS485_2_Addr[0] != 0){
if(RS485_2_Addr[0] < 10){
message += "0";
}
message = String(RS485_2_Addr[0]);
message += "DWR,0" + String(lp0) + ",";
for(int i=0 ; i<lp0 ; i++){
char hexStr[5]; // Buffer to hold the hexadecimal string (4 characters + null terminator)
sprintf(hexStr, "%04X", data[i]); // Format as 4 digit uppercase hexadecimal
message += MC9_CH[i] + 1; // ZONE 1
message += ",";
message += String(hexStr);
if(i != (lp0-1)){
message += ",";
}
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
}
if((RS485_2_Addr[1] != 0) && (lp1>0)){
if(RS485_2_Addr[1] < 10){
message += "0";
}
message = String(RS485_2_Addr[1]);
message += "DWR,0" + String(lp1-8) + ",";
for(int i=8 ; i<lp1 ; i++){
char hexStr[5]; // Buffer to hold the hexadecimal string (4 characters + null terminator)
sprintf(hexStr, "%04X", data[i]); // Format as 4 digit uppercase hexadecimal
message += MC9_CH[i%8] + 1; // ZONE 1
message += ",";
message += String(hexStr);
if(i != (lp1-1)){
message += ",";
}
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
}
return 0;
}
int msg_MC9_AT_set_2(unsigned int data[], int dataSize){
String message = "";
bool at[16];
int lp0, lp1;
// Number to boolean array
unsigned int data0 = data[0];
for (int i = 0; i < 16; i++) {
at[i] = (bitRead(data0, i) ? HIGH : LOW);
}
lp0 = 8;
lp1 = 8;
message = "";
if(RS485_2_Addr[0] != 0){
if(RS485_2_Addr[0] < 10){
message += "0";
}
message += String(RS485_2_Addr[0]);
message += "DWR,16,";
for(int i=0 ; i<8 ; i++){
String atFlag;
if(at[i]){
atFlag = "0001"; // Auto Tuen On
}else{
atFlag = "0000"; // Auto Tuen Off
}
message += "0302,"; // Ch No cmd.
message += "000" + String(i+1) + ","; // Ch No.
message += "0501,"; // AT cmd.
message += atFlag; // AT flag
if(i != 7){
message += ",";
}
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
}
message = "";
if((RS485_2_Addr[1] != 0) && (lp1>0)){
if(RS485_1_Addr[1] < 10){
message += "0";
}
message += String(RS485_2_Addr[1]);
message += "DWR,16,";
for(int i=0 ; i<8 ; i++){
String atFlag;
if(at[i+8]){
atFlag = "0001";
}else{
atFlag = "0000";
}
message += "0302,"; // Ch No cmd.
message += "000" + String(i+1) + ","; // Ch No.
message += "0501,"; // AT cmd.
message += atFlag; // AT flag
if(i != 7){
message += ",";
}
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
}
return 0; return 0;
} }
int saveMC9(String message){ int saveMC9_1(String message){
int addr; int addr;
int idx = -1; int idx = -1;
String mode; String mode;
@ -190,7 +334,7 @@ int saveMC9(String message){
return -1; return -1;
} }
if(latest_sent_msg.indexOf("DRS,08,0001") != -1){ // if sent message is PV CMD if(latest_sent_msg_1.indexOf("DRS,08,0001") != -1){ // if sent message is PV CMD
//rcv_10_PV = true; //rcv_10_PV = true;
RS485_1_Rcv_PV[idx] = true; RS485_1_Rcv_PV[idx] = true;
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){ for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
@ -198,7 +342,7 @@ int saveMC9(String message){
} }
} }
if(latest_sent_msg.indexOf("DRS,08,0011") != -1){ // if sent message is SV CMD if(latest_sent_msg_1.indexOf("DRS,08,0011") != -1){ // if sent message is SV CMD
//rcv_10_SV = true; //rcv_10_SV = true;
RS485_1_Rcv_SV[idx] = true; RS485_1_Rcv_SV[idx] = true;
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){ for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
@ -213,7 +357,49 @@ int saveMC9(String message){
} }
} }
int timeoutMC9(){ int saveMC9_2(String message){
int addr;
int idx = -1;
String mode;
int data[8];
int crc;
if (!parseMC9(message, addr, mode, data, crc)) {
for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s
if(RS485_2_Addr[i] == addr){
idx = i;
break;
}
}
if(idx < 0){
return -1;
}
if(latest_sent_msg_2.indexOf("DRS,08,0001") != -1){ // if sent message is PV CMD
//rcv_10_PV = true;
RS485_2_Rcv_PV[idx] = true;
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
RS485_2_Values_PV[i + idx*8] = data[i];
}
}
if(latest_sent_msg_2.indexOf("DRS,08,0011") != -1){ // if sent message is SV CMD
//rcv_10_SV = true;
RS485_2_Rcv_SV[idx] = true;
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
RS485_2_Values_SV[i + idx*8] = data[i];
}
}
return 0;
} else {
Serial.println("error 485 read");
return -1;
}
}
int timeoutMC9_1(){
int addr; int addr;
int idx = -1; int idx = -1;
String msg, mode, cmd; String msg, mode, cmd;
@ -222,12 +408,12 @@ int timeoutMC9(){
int crc; int crc;
// Message parsing // Message parsing
msg = latest_sent_msg.substring(1); msg = latest_sent_msg_1.substring(1);
sscanf(msg.c_str(), "%2d%3s", &addr, &dummy); sscanf(msg.c_str(), "%2d%3s", &addr, &dummy);
Serial.print("485 not responced... (Timeout) Req msg : " + latest_sent_msg); //230721 Serial.print("485 not responced... (Timeout) Req msg : " + latest_sent_msg_1);
if (latest_sent_msg.indexOf("DRS") != -1) { if (latest_sent_msg_1.indexOf("DRS") != -1) {
for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s
// Find idx of array // Find idx of array
if(RS485_1_Addr[i] == addr){ if(RS485_1_Addr[i] == addr){
@ -236,23 +422,38 @@ int timeoutMC9(){
} }
} }
// If not matched, return error return 0;
if(idx < 0){
} else {
Serial.println(); Serial.println();
return -1; return -1;
} }
if(latest_sent_msg.indexOf("DRS,08,0001") != -1){ // if sent message is PV CMD }
RS485_1_Rcv_PV[idx] = false;
Serial.print(" >> PV @"); int timeoutMC9_2(){
Serial.println(idx); int addr;
} int idx = -1;
if(latest_sent_msg.indexOf("DRS,08,0011") != -1){ // if sent message is SV CMD String msg, mode, cmd;
RS485_1_Rcv_SV[idx] = false; char dummy[4];
int data[8];
int crc;
// Message parsing
Serial.print(" >> SV @"); msg = latest_sent_msg_2.substring(1);
Serial.println(idx); sscanf(msg.c_str(), "%2d%3s", &addr, &dummy);
//230721 Serial.print("485 not responced... (Timeout) Req msg : " + latest_sent_msg_1);
if (latest_sent_msg_2.indexOf("DRS") != -1) {
for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s
// Find idx of array
if(RS485_2_Addr[i] == addr){
idx = i;
break;
}
} }
return 0; return 0;
} else { } else {
@ -260,18 +461,6 @@ int timeoutMC9(){
return -1; return -1;
} }
/*
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 -1;
}
*/
} }
int parseMC9(const String& message, int& addr, String& mode, int data[8], int& crc) { int parseMC9(const String& message, int& addr, String& mode, int data[8], int& crc) {

@ -1,17 +1,18 @@
void Periodic_run(){ void Periodic_run(){
if(T_1ms){ if(T_1ms){
// Read Arduino AI
// read_analog(); // read_analog();
T_1ms = false; T_1ms = false;
} }
if(T_2ms){ if(T_2ms){
// Read Arduino AI
read_analog(); // runtime of 1.xxx ms @8ch read_analog(); // runtime of 1.xxx ms @8ch
T_2ms = false; T_2ms = false;
} }
if(T_4ms){
//read_analog();
T_4ms = false;
}
if(T_5ms){ if(T_5ms){
// Read Arduino AI
//read_analog(); // runtime of 2.048012 ms @16ch //read_analog(); // runtime of 2.048012 ms @16ch
T_5ms = false; T_5ms = false;
} }
@ -20,13 +21,11 @@ void Periodic_run(){
T_10ms = false; T_10ms = false;
} }
if(T_20ms){ if(T_20ms){
// send and recieve RS485 send_485_1();
send_485(); recieve_485_1();
recieve_485();
T_20ms = false; T_20ms = false;
} }
if(T_50ms){ if(T_50ms){
// Read Arduino DI
read_digital(); read_digital();
T_50ms = false; T_50ms = false;
} }
@ -39,41 +38,23 @@ void Periodic_run(){
T_200ms = false; T_200ms = false;
} }
if(T_500ms){ if(T_500ms){
// RS485 recieve (for developing)
recieve_485_0(); recieve_485_0(); // RS485 recieve (for developing)
T_500ms = false; T_500ms = false;
} }
if(T_1000ms){ if(T_1000ms){
T_1000ms = false;
}
if(T_2000ms){
// Read MC9 PV // Read MC9 PV
for(int i=0 ; i < 2 ; i++){ for(int i=0 ; i < 2 ; i++){
if(RS485_1_Addr[i] > 0){ if(RS485_1_Addr[i] > 0){
write_buff(Buff_485_1_Wr, msg_MC9_PV(RS485_1_Addr[i])); write_buff(Buff_485_1_Wr, msg_MC9_PV_read(RS485_1_Addr[i]));
} }
} }
/*
float sum_AI = 0.0f;
float factor = 1.0f / AI_AVG_SIZE;
String str = "AI?:";
for (int i = 0; i < Size_AI; i++) {
int sum_AI = 0;
for(int j = 0; j < AI_AVG_SIZE; j++) {
sum_AI += (float) Recent_AI[i][j] * factor;
}
sum_AI *= 100.0f; // convert the sum to an integer representation
unsigned int integer_part = (unsigned int)sum_AI;
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 if(numOf485 > 10){
//sprintf(formattedNumber, "%04X", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성
sprintf(formattedNumber, "%04X", integer_part); // 4자리로 고정된 형식의 문자열 생성
str += formattedNumber; // 형식화된 문자열 추가
str += ',';
}
Serial.println(str);
*/
T_1000ms = false;
}
if(T_2000ms){
if(numOf485 > 0){
Serial.print("----- remain 485 buff : "); // For Serial.print("----- remain 485 buff : "); // For
Serial.println(numOf485); // Debugging Serial.println(numOf485); // Debugging
} }
@ -83,7 +64,7 @@ float sum_AI = 0.0f;
// Read MC9 SV // Read MC9 SV
for(int i=0 ; i < 2 ; i++){ for(int i=0 ; i < 2 ; i++){
if(RS485_1_Addr[i] > 0){ if(RS485_1_Addr[i] > 0){
write_buff(Buff_485_1_Wr, msg_MC9_SV(RS485_1_Addr[i])); //write_buff(Buff_485_1_Wr, msg_MC9_SV_read(RS485_1_Addr[i]));
} }
} }
T_5000ms = false; T_5000ms = false;
@ -94,9 +75,12 @@ void timer_1ms(){
msCnt += 1; msCnt += 1;
T_1ms = true; T_1ms = true;
if (msCnt % 4 == 0){ if (msCnt % 2 == 0){
T_2ms = true; T_2ms = true;
} }
if (msCnt % 4 == 0){
T_4ms = true;
}
if (msCnt % 5 == 0){ if (msCnt % 5 == 0){
T_5ms = true; T_5ms = true;
} }

@ -1,5 +1,6 @@
String Prcss_ALL_Read(){ String Prcss_ALL_Read(){
String str = ""; String str = "";
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
float factor = 1.0f / AI_AVG_SIZE; float factor = 1.0f / AI_AVG_SIZE;
str += "AI?:"; str += "AI?:";
@ -12,7 +13,6 @@ String Prcss_ALL_Read(){
sum_AI *= 50.0f; // convert the sum to an integer representation, Transfer data max(0xFFFF): 65,535. Each data max(10bit ADC): 1024. 1024 x 50 = 51,200 sum_AI *= 50.0f; // convert the sum to an integer representation, Transfer data max(0xFFFF): 65,535. Each data max(10bit ADC): 1024. 1024 x 50 = 51,200
unsigned int integer_part = (unsigned int)sum_AI; unsigned int integer_part = (unsigned int)sum_AI;
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
//sprintf(formattedNumber, "%04X", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성 //sprintf(formattedNumber, "%04X", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성
sprintf(formattedNumber, "%04X", integer_part); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", integer_part); // 4자리로 고정된 형식의 문자열 생성
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber; // 형식화된 문자열 추가
@ -21,7 +21,7 @@ String Prcss_ALL_Read(){
str += RcvOK; str += RcvOK;
str += "DI?:"; str += "DI?:";
char formattedNumber[5];
sprintf(formattedNumber, "%04X", Values_DI); sprintf(formattedNumber, "%04X", Values_DI);
str += formattedNumber; str += formattedNumber;
str += ','; str += ',';
@ -29,7 +29,6 @@ String Prcss_ALL_Read(){
str += "PV_1?:"; str += "PV_1?:";
for (int i = 0; i < RS485_1_Rcv_size*2; i++) { for (int i = 0; i < RS485_1_Rcv_size*2; i++) {
char formattedNumber[5];
int rcv = int(RS485_1_Rcv_PV[i/RS485_1_Rcv_size]); int rcv = int(RS485_1_Rcv_PV[i/RS485_1_Rcv_size]);
sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i] * rcv); sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i] * rcv);
@ -41,7 +40,6 @@ String Prcss_ALL_Read(){
str += "SV_1?:"; str += "SV_1?:";
for (int i = 0; i < RS485_1_Rcv_size*2; i++) { for (int i = 0; i < RS485_1_Rcv_size*2; i++) {
char formattedNumber[5];
int rcv = int(RS485_1_Rcv_SV[i/RS485_1_Rcv_size]); int rcv = int(RS485_1_Rcv_SV[i/RS485_1_Rcv_size]);
sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i] * rcv); sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i] * rcv);
@ -50,6 +48,11 @@ String Prcss_ALL_Read(){
} }
str += RcvOK; str += RcvOK;
str += "INIT?:";
sprintf(formattedNumber, "%04X", ((unsigned int)(Init_AO & Init_MC9)));
str += formattedNumber;
str += ',';
str += RcvOK;
return str + FIN; return str + FIN;
} }
@ -83,44 +86,43 @@ String Prcss_AO_Write(unsigned int data[], int dataSize){
return str + FIN; return str + FIN;
} }
String Prcss_SV_Write(unsigned int data[], int dataSize){ String Prcss_SV_Write_1(unsigned int data[], int dataSize){
String str = "SV!:"; String str = "SV_1!:";
msg_MC9_SV_set(data, dataSize); msg_MC9_SV_set_1(data, dataSize);
str += RcvOK; str += RcvOK;
Serial.println(str);
return str + FIN; return str + FIN;
} }
String Prcss_AT_Write(unsigned int data[], int dataSize){ String Prcss_AT_Write_1(unsigned int data[], int dataSize){
String str = "AT!:"; String str = "AT_1!:";
msg_MC9_AT_set(data, dataSize); msg_MC9_AT_set_1(data, dataSize);
str += RcvOK; str += RcvOK;
return str + FIN; return str + FIN;
} }
/*
String Prcss_AT_Write(bool onOff){
String str = "AT!:";
String message, cmd;
cmd = "10DWR,02,0302,0001,0501,";
if(onOff){
cmd += "0001";
}else{
cmd += "0000";
}
message = cmd; String Prcss_SV_Write_2(unsigned int data[], int dataSize){
message += sumMC9(message); String str = "SV_2!:";
message = "" + message + CRLF;
write_buff_first(Buff_485_Wr, message); msg_MC9_SV_set_2(data, dataSize);
str += RcvOK; str += RcvOK;
return str + FIN; return str + FIN;
} }
*/
String Prcss_AT_Write_2(unsigned int data[], int dataSize){
String str = "AT_2!:";
msg_MC9_AT_set_2(data, dataSize);
str += RcvOK;
return str + FIN;
}
String Prcss_RngAO(unsigned int data[], int dataSize){ String Prcss_RngAO(unsigned int data[], int dataSize){
String str = "RngAO!:"; String str = "RngAO!:";
int rng = 0; int rng = 0;
@ -139,8 +141,8 @@ String Prcss_RngAO(unsigned int data[], int dataSize){
return str + FIN; return str + FIN;
} }
String Prcss_ChMC9(unsigned int data[], int dataSize){ String Prcss_ChMC9_1(unsigned int data[], int dataSize){
String str = "ChMC9!:"; String str = "ChMC9_1!:";
int d = 20; int d = 20;
for(int i=0 ; i<dataSize ; i++){ for(int i=0 ; i<dataSize ; i++){
setupMC9_1(i, data[i]); setupMC9_1(i, data[i]);
@ -149,3 +151,14 @@ String Prcss_ChMC9(unsigned int data[], int dataSize){
str += RcvOK; str += RcvOK;
return str + FIN; return str + FIN;
} }
String Prcss_ChMC9_2(unsigned int data[], int dataSize){
String str = "ChMC9_2!:";
int d = 20;
for(int i=0 ; i<dataSize ; i++){
setupMC9_2(i, data[i]);
delay(d);
}
str += RcvOK;
return str + FIN;
}

@ -18,13 +18,13 @@ void RS485_setup(){
digitalWrite(RS485_OE_2, Rcv_485); digitalWrite(RS485_OE_2, Rcv_485);
} }
void send_485(){ void send_485_1(){
if(!Wait_485_1){ if(!Wait_485_1){
String message = read_buff(Buff_485_1_Wr); String message = read_buff(Buff_485_1_Wr);
if(message != ""){ if(message != ""){
//save sent message info. for received data processing //save sent message info. for received data processing
latest_sent_msg = message; latest_sent_msg_1 = message;
message += CRLF; message += CRLF;
digitalWrite(RS485_OE_1, Snd_485); delay(5); digitalWrite(RS485_OE_1, Snd_485); delay(5);
@ -39,26 +39,40 @@ void send_485(){
} }
} }
int recieve_485(){ void send_485_2(){
if(!Wait_485_2){
String message = read_buff(Buff_485_2_Wr);
if(message != ""){
//save sent message info. for received data processing
latest_sent_msg_2 = message;
message += CRLF;
digitalWrite(RS485_OE_2, Snd_485); delay(5);
Serial2.print(message);
returnTime = millis();
Serial2.flush();
digitalWrite(RS485_OE_2, Rcv_485); delay(5);
Wait_485_2 = true;
Wait_485_2_cnt = 0;
}
}
}
int recieve_485_1(){
// Timeout code // Timeout code
if(Wait_485_1){ if(Wait_485_1){
Wait_485_1_cnt++; Wait_485_1_cnt++;
if(Wait_485_1_cnt > 25){ // Timeout = periodic(20ms) x 20 = 400 msec if(Wait_485_1_cnt > 40){ // Timeout = periodic(20ms) x 25 = 500 msec
//Serial.println("485 not responced... (Timeout)"); //Serial.println("485 not responced... (Timeout)");
timeoutMC9(); timeoutMC9_1();
//if((latest_sent_msg == MC9_10_PV) ||(latest_sent_msg == MC9_10_SV)){
latest_sent_msg_1 = "";
/*
if((latest_sent_msg == msg_MC9_PV(10)) || (latest_sent_msg == msg_MC9_SV(10))){
timeoutMC9();
}
*/
latest_sent_msg = "";
Wait_485_1 = false; Wait_485_1 = false;
Wait_485_1_cnt = 0; Wait_485_1_cnt = 0;
numOf485--; numOf485--;
return -1; return -1;
} }
} }
@ -71,8 +85,8 @@ int recieve_485(){
String message = read_buff(Buff_485_1_Rd); String message = read_buff(Buff_485_1_Rd);
if(message != ""){ if(message != ""){
numOf485--; numOf485--;
if(latest_sent_msg.indexOf("DRS") != -1){ if(latest_sent_msg_1.indexOf("DRS") != -1){
saveMC9(message); saveMC9_1(message);
} }
else{ else{
Serial.print("----- 485 rcv : "); Serial.print("----- 485 rcv : ");
@ -85,6 +99,45 @@ int recieve_485(){
return 0; return 0;
} }
int recieve_485_2(){
// Timeout code
if(Wait_485_2){
Wait_485_2_cnt++;
if(Wait_485_2_cnt > 40){ // Timeout = periodic(20ms) x 25 = 500 msec
//Serial.println("485 not responced... (Timeout)");
timeoutMC9_2();
latest_sent_msg_2 = "";
Wait_485_2 = false;
Wait_485_2_cnt = 0;
numOf485--;
return -1;
}
}
// Receive pv data
while(Wait_485_2 && (Serial2.available() > 0)) {
char c = Serial2.read();
write_buff_c(Buff_485_2_Rd, c);
}
String message = read_buff(Buff_485_2_Rd);
if(message != ""){
numOf485--;
if(latest_sent_msg_2.indexOf("DRS") != -1){
saveMC9_2(message);
}
else{
Serial.print("----- 485 rcv : ");
Serial.println(message);
}
Wait_485_2 = false;
Wait_485_2_cnt = 0;
}
return 0;
}
/* For Test from PC */ /* For Test from PC */
void recieve_485_0(){ void recieve_485_0(){
@ -102,3 +155,21 @@ void recieve_485_0(){
IdeSerial = ""; IdeSerial = "";
} }
} }
void send_temp(bool F){
String message = "TEST";
if(F) {
message += CRLF;
message += " with CRLF";
message += CRLF;
}
digitalWrite(RS485_OE_1, Snd_485); delay(5);
Serial1.print(message);
returnTime = millis();
Serial1.flush();
digitalWrite(RS485_OE_1, Rcv_485); delay(5);
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -31,9 +31,26 @@ EthernetServer server = EthernetServer(LISTENPORT);
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println("Serial Done");
Ethernet.begin(mac,myIP,myDNS,myGW,myMASK); Ethernet.begin(mac,myIP,myDNS,myGW,myMASK);
// start listening for clients // start listening for clients
server.begin(); server.begin();
// Report address
Serial.print("MAC>>");
for (int i = 0; i < 6; i++) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i < 5) {
Serial.print(":");
}
}
Serial.println();
Serial.print("IP >>");
Serial.println(Ethernet.localIP());
} }
void loop() { void loop() {

Loading…
Cancel
Save