[MC9] [LIB]

- MC9 PV 요청 및 수신로직 추가
  ㄴ 500ms 마다 요청 후, 10ms 마다 응답 확인
  ㄴ 수신데이터 처리 미작성
- Library 제거
main
Changwoo Park 2 years ago
parent f96a5544b5
commit 06967ceb60

@ -3,13 +3,16 @@
#include <avr/wdt.h> // ?? #include <avr/wdt.h> // ??
#include "SC_AI.h" // Differential analog input collector //#include "SC_AI.h" // Differential analog input collector
#include "SC_MC9.h" // MC9 Tempreture Controller //#include "SC_MC9.h" // MC9 Tempreture Controller
#define RcvOK "OK\r\n" #define RcvOK "OK\r\n"
#define RcvErr "ER\r\n" #define RcvErr "ER\r\n"
#define MODE_DEBUG false #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. // Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network. // The IP address will be dependent on your local network.
// gateway and subnet are optional: // gateway and subnet are optional:
@ -33,9 +36,14 @@ bool connection = false;
char buffer[1024]; char buffer[1024];
int Size_AI = 16; int Size_AI = 16;
int AI_VALUES[] = { int Values_AI[16];
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 int Size_PV = 8;
}; int Values_PV[8];
String Buf_485;
bool Wait_485;
int Wait_485_cnt;
int msCnt = 0; int msCnt = 0;
unsigned long timer = 0; unsigned long timer = 0;
@ -47,8 +55,8 @@ void setup() {
TCPIP_setup(mac, ip, gateway, subnet); TCPIP_setup(mac, ip, gateway, subnet);
// scitech korea Library // scitech korea Library
SC_AI_setup(); GPIO_setup();
SC_MC9_setup(); MC9_setup();
// Timer set // Timer set
MsTimer2::set(10, periodic_10ms); MsTimer2::set(10, periodic_10ms);
@ -77,7 +85,8 @@ void loop() {
webReponse(); webReponse();
client = server.available(); client = server.available();
//Prcss_SV();
//delay(100);
if (client) { if (client) {
// check if client is connected // check if client is connected
Serial.println("Client Connected!!!"); Serial.println("Client Connected!!!");
@ -90,31 +99,49 @@ void loop() {
// check for command byte // check for command byte
if (client.available() > 0) { if (client.available() > 0) {
char c; char c;
bool cr = false;
String command; String command;
String cmd; String cmd;
unsigned int data[32]; unsigned int data[32];
String message;
int dataSize; int dataSize;
busy = true; busy = true;
// Read message by byte // Read message by byte
while ((c = client.read())) { while ((c = client.read())) {
// Read data until LF or CR
if((c != 10) && (c != 13)){ // Read data until CR or LF
command += c; if((c != 13) && (c != 10)){
command += c;
}else{ }else{
break; break;
} }
} }
cmd = demuxCMD(command, data, &dataSize); // -------------------- Process CMD -------------------- //
cmd = demuxCMD(command, &message);
if(cmd=="AI"){ if(cmd=="AI"){
client.print(Prcss_AI()); client.print(Prcss_AI());
}else if(cmd=="AO"){ }else if(cmd=="AO"){
dataSize = demuxNum(message, data);
client.print(Prcss_AO(data, dataSize)); client.print(Prcss_AO(data, dataSize));
}else{ }else if(cmd=="PV"){
client.print(RcvErr); 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..."); Serial.println("Client Disconnected...");
} }
} }
@ -154,32 +181,10 @@ void TCPIP_setup(byte mac[], IPAddress ip, IPAddress gateway, IPAddress subnet){
web.begin(); 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(){ void AnalogIn_Print(){
for (int i = 0; i < Size_AI; i++) { for (int i = 0; i < Size_AI; i++) {
Serial.print(AI_VALUES[i]); Serial.print(Values_AI[i]);
Serial.print("\t"); Serial.print("\t");
} }
Serial.println(); Serial.println();

@ -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]);
}
}

@ -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
}

@ -3,7 +3,7 @@ String Prcss_AI(){
String str = ""; String str = "";
for (int i = 0; i < Size_AI; i++) { for (int i = 0; i < Size_AI; i++) {
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
sprintf(formattedNumber, "%04X", AI_VALUES[i]); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber; // 형식화된 문자열 추가
//str += String(AI_VALUES[i]); //str += String(AI_VALUES[i]);
str += ','; str += ',';
@ -22,3 +22,27 @@ String Prcss_AO(unsigned int data[], int dataSize){
Serial.println(str); Serial.println(str);
return RcvOK; 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;
}

@ -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("::"); int separatorIndex = command.indexOf("::");
String leftPart = command.substring(0, separatorIndex); 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진수 배열을 분리하여 추출 // 우측의 16진수 배열을 분리하여 추출
const char* delimiter = ","; const char* delimiter = ",";
@ -28,9 +33,6 @@ String demuxCMD(String command, unsigned int data[], int* dataSize) {
data[index] = strtoul(hexValue.c_str(), NULL, 16); data[index] = strtoul(hexValue.c_str(), NULL, 16);
index++; index++;
// dataSize에 배열 크기 저장
*dataSize = index;
// 왼쪽 부분인 "ABC"을 반환 // 왼쪽 부분인 "ABC"을 반환
return leftPart; return index;
} }

@ -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;
}
}
}

@ -1,27 +0,0 @@
/*
History:
20230504 - V0.2
20230504 - V0.1 init.
*/
#include <SC_AI.h>
#include <Arduino.h>
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
}

@ -1,7 +0,0 @@
#ifndef SC_AI_h
#define SC_AI_h
void SC_AI_setup();
void SC_AI(int arr[], int size);
#endif

@ -1,32 +0,0 @@
/*
History:
20230504 - V0.2
20230504 - V0.1 init.
*/
#include <SC_MC9.h>
#include <Arduino.h>
//#include <HardwareSerial.h>
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
}

@ -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
Loading…
Cancel
Save