- 485_1 Dual MC9
  ㄴ 485_1에 연결된 두개의 MC9 테스트 完
  ㄴ 485_1, 485_2에 각 2개 까지 연결 가능하며, 485_2는 추후 업데이트 예정
  ㄴ 각 MC9은 Addr가 0이 아니어야하며, chMC9으로 초기화 필요
  ㄴ PV, SV read 및 SV write 테스트 完
  ㄴ Auto Tune 업데이트 필요
- 일부 미사용 processes 들 삭제
main
Changwoo Park 1 year ago
parent 24723b0e74
commit 6c60af89fa

@ -1,4 +1,3 @@
//
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);

@ -175,35 +175,20 @@ void loop() {
//Serial.println(cmd); //Serial.println(cmd);
if(cmd=="ALL?"){ if(cmd=="ALL?"){
client.print(Prcss_ALL_Read()); client.print(Prcss_ALL_Read());
} }
// GPIO // GPIO
else if(cmd=="DO!"){ else if(cmd=="DO!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_DO_Write(data, dataSize)); client.print(Prcss_DO_Write(data, dataSize));
}else if(cmd=="DI?"){
client.print(Prcss_DI_Read());
}else if(cmd=="AO!"){ }else if(cmd=="AO!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_AO_Write(data, dataSize)); client.print(Prcss_AO_Write(data, dataSize));
}else if(cmd=="AI?"){
client.print(Prcss_AI_Read());
} }
// MC9 // MC9
else if(cmd=="SV!"){ else if(cmd=="SV!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_SV_Write(data, dataSize)); client.print(Prcss_SV_Write(data, dataSize));
}else if(cmd=="SV?"){
client.print(Prcss_SV_Read());
}else if(cmd=="PV?"){
client.print(Prcss_PV_Read());
} }
/* /*
else if(cmd=="ATon!"){ else if(cmd=="ATon!"){
@ -220,13 +205,16 @@ 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));
}else if(cmd=="ChMC9!"){ }else if(cmd=="ChMC9!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_ChMC9(data, dataSize)); client.print(Prcss_ChMC9(data, dataSize));
} }
// Extra
else if(cmd=="AT!"){
dataSize = demuxNum(cmdData, data);
client.print("");
}
//else if(cmd==""){ //else if(cmd==""){
//} //}

@ -10,6 +10,9 @@ void setupMC9_1(int i, int data){
String msg_MC9_PV(int addr){ String msg_MC9_PV(int addr){
String message = ""; String message = "";
if(addr < 10){
message += "0";
}
message += String(addr); message += String(addr);
message += "DRS,08,0001"; message += "DRS,08,0001";
message += sumMC9(message); message += sumMC9(message);
@ -21,6 +24,9 @@ String msg_MC9_PV(int addr){
String msg_MC9_SV(int addr){ String msg_MC9_SV(int addr){
String message = ""; String message = "";
if(addr < 10){
message += "0";
}
message += String(addr); message += String(addr);
message += "DRS,08,0011"; message += "DRS,08,0011";
message += sumMC9(message); message += sumMC9(message);
@ -29,36 +35,107 @@ String msg_MC9_SV(int addr){
return message; return message;
} }
int msg_MC9_SV_set(unsigned int data[], int dataSize){
String message = "";
int lp0, lp1;
if(dataSize > 8){
lp0 = 8;
lp1 = dataSize;
/*
if(dataSize==16){
lp1 = 8;
}else{
lp1 = dataSize%8;
}
*/
}else{
lp0 = dataSize;
lp1 = 0;
}
if(RS485_1_Addr[0] != 0){
if(RS485_1_Addr[0] < 10){
message += "0";
}
message = String(RS485_1_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_Wr, message);
}
if((RS485_1_Addr[1] != 0) && (lp1>0)){
if(RS485_1_Addr[1] < 10){
message += "0";
}
message = String(RS485_1_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_Wr, message);
}
return 0;
}
int saveMC9(String message){ int saveMC9(String message){
int addr, idx; int addr;
int idx = -1;
String mode; String mode;
int data[8]; int data[8];
int crc; int crc;
if (parseMC9(message, addr, mode, data, crc)) { if (parseMC9(message, addr, mode, data, crc)) {
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
if(RS485_1_Addr[i] == addr){ if(RS485_1_Addr[i] == addr){
idx = i; idx = i;
break; break;
}else{
return -1;
} }
} }
if(latest_sent_msg.indexOf("DRS,08,0001") != -1){ // if PV CMD if(idx < 0){
return -1;
}
if(latest_sent_msg.indexOf("DRS,08,0001") != -1){ // if sent message is PV CMD
//rcv_10_PV = true; //rcv_10_PV = true;
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){ for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
RS485_1_Values_PV[i + idx*8] = data[i]; RS485_1_Values_PV[i + idx*8] = data[i];
} }
} }
//if(latest_sent_msg == msg_MC9_SV(10)){
if(latest_sent_msg.indexOf("DRS,08,0011") != -1){ // if SV CMD if(latest_sent_msg.indexOf("DRS,08,0011") != -1){ // if sent message is SV CMD
//rcv_10_SV = true; //rcv_10_SV = true;
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){ for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
RS485_1_Values_SV[i + idx*8] = data[i]; RS485_1_Values_SV[i + idx*8] = data[i];
} }
} }
return 1; return 1;
} else { } else {
Serial.println("error 485 read"); Serial.println("error 485 read");
return 0; return 0;

@ -1,6 +1,6 @@
String Prcss_ALL_Read(){ String Prcss_ALL_Read(){
String str = ""; String str = "";
str += "AI?:"; str += "AI?:";
for (int i = 0; i < Size_AI; i++) { for (int i = 0; i < Size_AI; i++) {
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
@ -10,20 +10,19 @@ String Prcss_ALL_Read(){
} }
str += RcvOK; str += RcvOK;
str += "DI?:"; str += "DI?:";
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 char formattedNumber[5];
sprintf(formattedNumber, "%04X", Values_DI); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", Values_DI);
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber;
str += ','; str += ',';
str += RcvOK; str += RcvOK;
str += "PV10?:"; str += "PV10?:";
if(rcv_10_PV){ if(rcv_10_PV){
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]; // 4자리 숫자 + 널 종료 문자 char formattedNumber[5];
sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i]); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i]);
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber;
str += ','; str += ',';
} }
str += RcvOK; str += RcvOK;
@ -34,15 +33,16 @@ String Prcss_ALL_Read(){
str += "SV10?:"; str += "SV10?:";
if(rcv_10_SV){ if(rcv_10_SV){
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]; // 4자리 숫자 + 널 종료 문자 char formattedNumber[5];
sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i]); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i]);
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber;
str += ','; str += ',';
} }
str += RcvOK; str += RcvOK;
}else{ }else{
str += RcvErr; str += RcvErr;
} }
return str + FIN; return str + FIN;
} }
@ -62,109 +62,23 @@ String Prcss_DO_Write(unsigned int data[], int dataSize){
str += RcvOK; str += RcvOK;
return str + FIN; return str + FIN;
} }
String Prcss_DI_Read(){
String str = "DI?:";
//for (int i = 0; i < Size_DI; i++) {
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
sprintf(formattedNumber, "%04X", Values_DI); // 4자리로 고정된 형식의 문자열 생성
str += formattedNumber; // 형식화된 문자열 추가
str += ',';
//}
str += RcvOK;
return str + FIN;
}
//
String Prcss_AI_Read(){
String 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;
return str + FIN;
}
String Prcss_AO_Write(unsigned int data[], int dataSize){ String Prcss_AO_Write(unsigned int data[], int dataSize){
String str = "AO!:"; String str = "AO!:";
//int t = millis();
int d = 2; int d = 2;
for(int i=0 ; i<dataSize ; i++){ for(int i=0 ; i<dataSize ; i++){
AO_Write(i, data[i]); AO_Write(i, data[i]);
} }
//t = millis()-t;
//Serial.println(t);
str += RcvOK;
return str + FIN;
}
String Prcss_PV_Read(){
String str = "PV10?:";
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;
return str + FIN;
}
String Prcss_SV_Read(){
String str = "SV10?:";
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; str += RcvOK;
return str + FIN; return str + FIN;
} }
String Prcss_SV_Write(unsigned int data[], int dataSize){ String Prcss_SV_Write(unsigned int data[], int dataSize){
String str = "SV!:"; String str = "SV!:";
String message, cmd, cmdData;
cmd = "10DWR,";
if (dataSize < 9) {
cmd = "10DWR,0" + String(dataSize) + ",";
} else {
str += RcvErr;
return str + FIN;
}
cmdData = "";
for(int i=0 ; i<dataSize ; 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
cmdData += MC9_CH[i] + 1; // ZONE 1
cmdData += ",";
cmdData += String(hexStr);
if(i != dataSize -1){
cmdData += ",";
}
}
message = cmd + cmdData; msg_MC9_SV_set(data, dataSize);
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_Wr, message);
Serial.println(message);
str += RcvOK; str += RcvOK;
return str + FIN; return str + FIN;
@ -181,7 +95,6 @@ String Prcss_AT_Write(bool onOff){
cmd += "0000"; cmd += "0000";
} }
message = cmd; message = cmd;
message += sumMC9(message); message += sumMC9(message);
message = "" + message + CRLF; message = "" + message + CRLF;
@ -191,7 +104,6 @@ String Prcss_AT_Write(bool onOff){
return str + FIN; 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;

@ -1,9 +1,7 @@
// 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 RS485_OE_1 22 //RS485 CH#1 Output Enable => pin 22 for CH#1
#define Snd_485 HIGH #define Snd_485 HIGH
#define Rcv_485 LOW #define Rcv_485 LOW
void RS485_setup(){ void RS485_setup(){
Serial1.setTimeout(300); Serial1.setTimeout(300);
Serial1.begin(9600); Serial1.begin(9600);
@ -69,7 +67,6 @@ int recieve_485(){
String message = read_buff(Buff_485_Rd); String message = read_buff(Buff_485_Rd);
if(message != ""){ if(message != ""){
numOf485--; numOf485--;
//if((latest_sent_msg == msg_MC9_PV(10)) || (latest_sent_msg == msg_MC9_SV(10))){
if(latest_sent_msg.indexOf("DRS") != -1){ if(latest_sent_msg.indexOf("DRS") != -1){
saveMC9(message); saveMC9(message);
} }
@ -84,7 +81,6 @@ int recieve_485(){
return 0; return 0;
} }
/* For Test from PC */ /* For Test from PC */
void recieve_485_0(){ void recieve_485_0(){
@ -103,5 +99,3 @@ void recieve_485_0(){
IdeSerial = ""; IdeSerial = "";
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save