NX4 사용

- 단, 각 485의 첫번째만 NX4 가능
- 필요시 추후 확장 예정
- UAE SOFC용으로 업데이트
devIFB_v2
Changwoo Park 2 weeks ago
parent 9d5f29ee40
commit 30a1e87a1a

@ -8,8 +8,8 @@
// ------------------
#include "GP8403.h"
#define VERSION "240129 1044"
#define COMMENT "MC9 or AO init"
#define VERSION "250228 1553"
#define COMMENT "MC9 & NX4"
#define AI_RANGE 10
#define AI_AVG_SIZE 20
@ -99,14 +99,17 @@ char Buff_485_2_Wr[BUFF_SIZE] = {0};
char Buff_485_2_Rd[BUFF_SIZE] = {0};
String latest_sent_msg_1;
int numOf485 = 0;
int returnTime = 0;
int numOf485_1 = 0;
int returnTime_1 = 0;
String latest_sent_msg_2;
int numOf485_2 = 0;
int returnTime_2 = 0;
/* ---------- RS485 ---------- */
// NX4 Addr Start
#define NX4_ADDR 11
// 485 CH 1
bool Wait_485_1;
int Wait_485_1_cnt;
@ -156,7 +159,6 @@ void send_485_1();
void send_485_2();
int recieve_485_1();
int recieve_485_2();
void recieve_485_0();
//GPIO
void GPIO_setup();

@ -44,7 +44,7 @@ void loop() {
String cmdData;
unsigned int data[32];
int dataSize;
Serial.println("new start");
// check if client is connected
Serial.println("Client Connected!!!");
@ -83,29 +83,41 @@ void loop() {
// GPIO
else if(cmd=="DO!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_DO_Write(data, dataSize));
Prcss_DO_Write(data, dataSize);
client.print(Prcss_ALL_Read());
//client.print(Prcss_DO_Write(data, dataSize));
}else if(cmd=="AO!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_AO_Write(data, dataSize));
Prcss_AO_Write(data, dataSize);
client.print(Prcss_ALL_Read());
//client.print(Prcss_AO_Write(data, dataSize));
}
// MC9
else if(cmd=="SV_1!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_SV_Write_1(data, dataSize));
Prcss_SV_Write_1(data, dataSize);
client.print(Prcss_ALL_Read());
//client.print(Prcss_SV_Write_1(data, dataSize));
}else if(cmd=="AT_1!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_AT_Write_1(data, dataSize));
Prcss_AT_Write_1(data, dataSize);
client.print(Prcss_ALL_Read());
//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));
Prcss_SV_Write_2(data, dataSize);
client.print(Prcss_ALL_Read());
//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));
Prcss_AT_Write_2(data, dataSize);
client.print(Prcss_ALL_Read());
//client.print(Prcss_AT_Write_2(data, dataSize));
}
// Init
@ -114,15 +126,21 @@ void loop() {
}else if(cmd=="RngAO!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_RngAO(data, dataSize));
Prcss_RngAO(data, dataSize);
client.print(Prcss_ALL_Read());
//client.print(Prcss_RngAO(data, dataSize));
Init_AO = true;
}else if(cmd=="ChMC9_1!"){
dataSize = demuxNum(cmdData, data);
client.print(Prcss_ChMC9_1(data, dataSize));
Prcss_ChMC9_1(data, dataSize);
client.print(Prcss_ALL_Read());
//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));
Prcss_ChMC9_2(data, dataSize);
client.print(Prcss_ALL_Read());
//client.print(Prcss_ChMC9_2(data, dataSize));
Init_MC9 = true;
}

@ -22,9 +22,16 @@ String msg_MC9_PV_read(int addr){
message += "0";
}
message += String(addr); // Address
message += "DRS,08,0001"; // Data Read Seris 08 data from 0001(PV) ch
message += sumMC9(message); // Check Sum
message = "" + message; // Start with STX
if(addr < NX4_ADDR){ //MC9
message += "DRS,08,0001"; // Data Read Seris 08 data from 0001(PV) ch
message += sumMC9(message); // Check Sum
message = "" + message; // Start with STX
}else{ //NX4, only 1 Ch
message += "DRS,01,0001"; // Data Read Seris 08 data from 0001(PV) ch
message += sumMC9(message); // Check Sum
message = "" + message; // Start with STX
}
return message;
}
@ -36,9 +43,16 @@ String msg_MC9_SV_read(int addr){
message += "0";
}
message += String(addr); // Address
message += "DRS,08,0011"; // Data Read Seris 08 data from 0001(SV) ch
message += sumMC9(message); // Check Sum
message = "" + message; // Start with STX
if(addr < NX4_ADDR){ //MC9
message += "DRS,08,0011"; // Data Read Seris 08 data from 0001(SV) ch
message += sumMC9(message); // Check Sum
message = "" + message; // Start with STX
}else{ //NX4, only 1 Ch
message += "DRS,01,0002"; // Data Read Seris 08 data from 0001(PV) ch
message += sumMC9(message); // Check Sum
message = "" + message; // Start with STX
}
return message;
}
@ -46,7 +60,7 @@ String msg_MC9_SV_read(int addr){
int msg_MC9_SV_set_1(unsigned int data[], int dataSize){
String message = "";
int lp0, lp1;
int addr;
if(dataSize > 16){
dataSize = 16;
}
@ -58,26 +72,58 @@ int msg_MC9_SV_set_1(unsigned int data[], int dataSize){
lp1 = 0;
}
if(RS485_1_Addr[0] != 0){
if(RS485_1_Addr[0] < 10){
addr = RS485_1_Addr[0];
if(addr != 0){
if(addr < 10){
message += "0";
}
message = String(RS485_1_Addr[0]);
message += "DWR,0" + String(lp0) + ",";
for(int i=0 ; i<lp0 ; i++){
message = String(addr);
if(addr < NX4_ADDR){ //MC9
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;
if(write_buff_first(Buff_485_1_Wr, message)){
numOf485_1++;
}
}else{ //NX4, only 1 Ch
char hexStr[5]; // Buffer to hold the hexadecimal string (4 characters + null terminator)
sprintf(hexStr, "%04X", data[i]); // Format as 4 digit uppercase hexadecimal
String messageRate = message;
message += MC9_CH[i] + 1; // ZONE 1
message += ",";
//SV Message
message += "DWS,01,0301,";
sprintf(hexStr, "%04X", data[0]); // Format as 4 digit uppercase hexadecimal
message += String(hexStr);
if(i != (lp0-1)){
message += ",";
message += sumMC9(message);
message = "" + message + CRLF;
if(write_buff_first(Buff_485_1_Wr, message)){
numOf485_1++;
}
//Up,Down Rate Message
messageRate += "DWS,02,0108,";
sprintf(hexStr, "%04X", data[1]); // Format as 4 digit uppercase hexadecimal
messageRate += String(hexStr) + "," + String(hexStr);
messageRate += sumMC9(messageRate);
messageRate = "" + messageRate + CRLF;
if(write_buff_first(Buff_485_1_Wr, messageRate)){
numOf485_1++;
}
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_1_Wr, message);
}
if((RS485_1_Addr[1] != 0) && (lp1>0)){
@ -100,7 +146,9 @@ int msg_MC9_SV_set_1(unsigned int data[], int dataSize){
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_1_Wr, message);
if(write_buff_first(Buff_485_1_Wr, message)){
numOf485_1++;
}
}
return 0;
@ -146,7 +194,9 @@ int msg_MC9_AT_set_1(unsigned int data[], int dataSize){
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_1_Wr, message);
if(write_buff_first(Buff_485_1_Wr, message)){
numOf485_1++;
}
}
message = "";
@ -174,7 +224,9 @@ int msg_MC9_AT_set_1(unsigned int data[], int dataSize){
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_1_Wr, message);
if(write_buff_first(Buff_485_1_Wr, message)){
numOf485_1++;
}
}
return 0;
}
@ -182,7 +234,8 @@ int msg_MC9_AT_set_1(unsigned int data[], int dataSize){
int msg_MC9_SV_set_2(unsigned int data[], int dataSize){
String message = "";
int lp0, lp1;
int addr;
if(dataSize > 16){
dataSize = 16;
}
@ -194,26 +247,59 @@ int msg_MC9_SV_set_2(unsigned int data[], int dataSize){
lp1 = 0;
}
if(RS485_2_Addr[0] != 0){
if(RS485_2_Addr[0] < 10){
addr = RS485_2_Addr[0];
if(addr != 0){
if(addr < 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 = String(addr);
message += MC9_CH[i] + 1; // ZONE 1
message += ",";
message += String(hexStr);
if(i != (lp0-1)){
if(addr < NX4_ADDR){ //MC9
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;
if(write_buff_first(Buff_485_2_Wr, message)){
numOf485_2++;
}
}else{ //NX4, only 1 Ch
char hexStr[5]; // Buffer to hold the hexadecimal string (4 characters + null terminator)
String messageRate = message;
//SV Message
message += "DWS,01,0301,";
sprintf(hexStr, "%04X", data[0]); // Format as 4 digit uppercase hexadecimal
message += String(hexStr);
message += sumMC9(message);
message = "" + message + CRLF;
if(write_buff_first(Buff_485_2_Wr, message)){
numOf485_2++;
}
//Up,Down Rate Message
messageRate += "DWS,02,0108,";
sprintf(hexStr, "%04X", data[1]); // Format as 4 digit uppercase hexadecimal
messageRate += String(hexStr) + "," + String(hexStr);
messageRate += sumMC9(messageRate);
messageRate = "" + messageRate + CRLF;
if(write_buff_first(Buff_485_2_Wr, messageRate)){
numOf485_2++;
}
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
}
if((RS485_2_Addr[1] != 0) && (lp1>0)){
@ -235,7 +321,9 @@ int msg_MC9_SV_set_2(unsigned int data[], int dataSize){
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
if(write_buff_first(Buff_485_2_Wr, message)){
numOf485_2++;
}
}
return 0;
@ -281,7 +369,9 @@ int msg_MC9_AT_set_2(unsigned int data[], int dataSize){
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
if(write_buff_first(Buff_485_2_Wr, message)){
numOf485_2++;
}
}
message = "";
@ -309,7 +399,9 @@ int msg_MC9_AT_set_2(unsigned int data[], int dataSize){
}
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_2_Wr, message);
if(write_buff_first(Buff_485_2_Wr, message)){
numOf485_2++;
}
}
return 0;
@ -340,6 +432,10 @@ int saveMC9_1(String message){
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
RS485_1_Values_PV[i + idx*8] = data[i];
}
}else if(latest_sent_msg_1.indexOf("DRS,01,0001") != -1){ // if sent message is PV CMD
//rcv_10_PV = true;
RS485_1_Rcv_PV[idx] = true;
RS485_1_Values_PV[0] = data[0];
}
if(latest_sent_msg_1.indexOf("DRS,08,0011") != -1){ // if sent message is SV CMD
@ -348,11 +444,15 @@ int saveMC9_1(String message){
for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
RS485_1_Values_SV[i + idx*8] = data[i];
}
}else if(latest_sent_msg_1.indexOf("DRS,01,0002") != -1){ // if sent message is PV CMD
//rcv_10_PV = true;
RS485_1_Rcv_SV[idx] = true;
RS485_1_Values_SV[0] = data[0];
}
return 0;
} else {
Serial.println("error 485 read");
Serial.println("485_1: unknown message recived");
return -1;
}
}
@ -379,22 +479,31 @@ int saveMC9_2(String message){
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++){
for(int i = 0 ; i < RS485_2_Rcv_size ; i++){
RS485_2_Values_PV[i + idx*8] = data[i];
}
}else if(latest_sent_msg_2.indexOf("DRS,01,0001") != -1){ // if sent message is PV CMD
//rcv_10_PV = true;
RS485_2_Rcv_PV[idx] = true;
RS485_2_Values_PV[0] = data[0];
}
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++){
for(int i = 0 ; i < RS485_2_Rcv_size ; i++){
RS485_2_Values_SV[i + idx*8] = data[i];
}
}else if(latest_sent_msg_2.indexOf("DRS,01,0002") != -1){ // if sent message is PV CMD
//rcv_10_PV = true;
RS485_2_Rcv_SV[idx] = true;
RS485_2_Values_SV[0] = data[0];
}
return 0;
} else {
Serial.println("error 485 read");
Serial.println("485_2: unknown message recived");
return -1;
}
}
@ -478,7 +587,11 @@ int parseMC9(const String& message, int& addr, String& mode, int data[8], int& c
// Check the parsed status and the number of extracted elements
if (strcmp(statusC, "OK") != 0 || ret != 12) {
int ret = sscanf(message.c_str(), "%2s%3s,%2s,%4x%2x",
addrC, modeC, statusC, &dataC[0], &crcC);
if (strcmp(statusC, "OK") != 0 || ret != 5) {
return -1;
}
}
// Copy data to the output parameters

@ -22,7 +22,9 @@ void Periodic_run(){
}
if(T_20ms){
send_485_1();
send_485_2();
recieve_485_1();
recieve_485_2();
T_20ms = false;
}
if(T_50ms){
@ -38,8 +40,7 @@ void Periodic_run(){
T_200ms = false;
}
if(T_500ms){
recieve_485_0(); // RS485 recieve (for developing)
T_500ms = false;
}
if(T_1000ms){
@ -48,15 +49,27 @@ void Periodic_run(){
}
if(T_2000ms){
// Read MC9 PV
for(int i=0 ; i < 2 ; i++){
if(RS485_1_Addr[i] > 0){
write_buff(Buff_485_1_Wr, msg_MC9_PV_read(RS485_1_Addr[i]));
if(write_buff(Buff_485_1_Wr, msg_MC9_PV_read(RS485_1_Addr[i]))){
numOf485_1++;
}
}
}
for(int i=0 ; i < 2 ; i++){
if(RS485_2_Addr[i] > 0){
if(write_buff(Buff_485_2_Wr, msg_MC9_PV_read(RS485_2_Addr[i]))){
numOf485_2++;
}
}
}
if(numOf485 > 10){
if(numOf485_1 > 10){
Serial.print("----- remain 485 buff : "); // For
Serial.println(numOf485); // Debugging
Serial.println(numOf485_1); // Debugging
}
T_2000ms = false;
}

@ -48,6 +48,26 @@ String Prcss_ALL_Read(){
}
str += RcvOK;
str += "PV_2?:";
for (int i = 0; i < RS485_2_Rcv_size*2; i++) {
int rcv = int(RS485_2_Rcv_PV[i/RS485_2_Rcv_size]);
sprintf(formattedNumber, "%04X", RS485_2_Values_PV[i] * rcv);
str += formattedNumber;
str += ',';
}
str += RcvOK;
str += "SV_2?:";
for (int i = 0; i < RS485_2_Rcv_size*2; i++) {
int rcv = int(RS485_2_Rcv_SV[i/RS485_2_Rcv_size]);
sprintf(formattedNumber, "%04X", RS485_2_Values_SV[i] * rcv);
str += formattedNumber;
str += ',';
}
str += RcvOK;
str += "INIT?:";
sprintf(formattedNumber, "%04X", ((unsigned int)(Init_AO | Init_MC9)));
str += formattedNumber;

@ -29,7 +29,7 @@ void send_485_1(){
message += CRLF;
digitalWrite(RS485_OE_1, Snd_485); delay(5);
Serial1.print(message);
returnTime = millis();
returnTime_1 = millis();
Serial1.flush();
digitalWrite(RS485_OE_1, Rcv_485); delay(5);
@ -50,12 +50,13 @@ void send_485_2(){
message += CRLF;
digitalWrite(RS485_OE_2, Snd_485); delay(5);
Serial2.print(message);
returnTime = millis();
returnTime_2 = millis();
Serial2.flush();
digitalWrite(RS485_OE_2, Rcv_485); delay(5);
Wait_485_2 = true;
Wait_485_2_cnt = 0;
}
}
}
@ -71,7 +72,7 @@ int recieve_485_1(){
latest_sent_msg_1 = "";
Wait_485_1 = false;
Wait_485_1_cnt = 0;
numOf485--;
numOf485_1--;
return -1;
}
@ -84,7 +85,7 @@ int recieve_485_1(){
String message = read_buff(Buff_485_1_Rd);
if(message != ""){
numOf485--;
numOf485_1--;
if(latest_sent_msg_1.indexOf("DRS") != -1){
saveMC9_1(message);
}
@ -110,7 +111,7 @@ int recieve_485_2(){
latest_sent_msg_2 = "";
Wait_485_2 = false;
Wait_485_2_cnt = 0;
numOf485--;
numOf485_2--;
return -1;
}
@ -123,7 +124,7 @@ int recieve_485_2(){
String message = read_buff(Buff_485_2_Rd);
if(message != ""){
numOf485--;
numOf485_2--;
if(latest_sent_msg_2.indexOf("DRS") != -1){
saveMC9_2(message);
}
@ -137,39 +138,3 @@ int recieve_485_2(){
return 0;
}
/* For Test from PC */
void recieve_485_0(){
while(Serial.available() > 0) {
char c = Serial.read();
IdeSerial += c;
}
if(IdeSerial.endsWith("\n")){
IdeSerial.replace(CRLF,"");
IdeSerial += sumMC9(IdeSerial);
IdeSerial = "" + IdeSerial + CRLF;
write_buff_first(Buff_485_1_Wr, 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);
}

@ -21,7 +21,6 @@ int write_buff(char* buff, String str) {
size_t len = strlen(cstr);
if (strlen(buff) + len < BUFF_SIZE) {
strcat(buff, cstr);
numOf485++;
return 1;
} else {
Serial.println("Not enough space in buffer! (write)");
@ -38,7 +37,6 @@ int write_buff_first(char* buff, String str) {
strcpy(temp, buff); // copy existing content to temp buffer
strcpy(buff, cstr); // copy new string to buffer
strcat(buff, temp); // append old content to buffer
numOf485++;
return 1;
} else {
Serial.println("Not enough space in buffer! (prepend)");

Loading…
Cancel
Save