- 전반적인 구성 완료
  ㄴ Read all 기능 추가
  ㄴ MC9 AT 기능 추가 (임시, 1ch에 대해 한정)
  ㄴ 기타 테스트 완료
main v0.1.0
Changwoo Park 2 years ago
parent 7036eb5629
commit 00529466cf

Binary file not shown.

@ -1,13 +1,15 @@
#include "Arduino.h"
#include <MsTimer2.h> // Timer
#include <Ethernet2.h>
#include <avr/wdt.h> // ??
#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 = "";
if(cmd=="DO!"){
//Serial.println(cmd);
if(cmd=="ALL?"){
client.print(Prcss_ALL_Read());
}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 = "";
}

@ -5,6 +5,8 @@ void GPIO_setup(){
for (int i = 0; i < Size_DO; i++) {
pinMode(DoPin[i], OUTPUT);
}
analogReference(EXTERNAL);
}
void read_analog(){

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

@ -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<dataSize ; i++){
str += String(data[i]);
str += "\t";
if(i==0){
AO_0.setDACOutVoltage(data[i], 0);
delay(10);
}else if(i==1){
AO_0.setDACOutVoltage(data[i], 1);
delay(10);
}
//str += String(data[i]);
//str += "\t";
}
Serial.println(str);
return RcvOK;
t = millis()-t;
Serial.println(t);
str += RcvOK;
return str + FIN;
}
String Prcss_PV_Read(){
@ -63,7 +124,7 @@ String Prcss_PV_Read(){
}
str += RcvOK;
return str;
return str + FIN;
}
String Prcss_SV_Read(){
@ -77,14 +138,22 @@ String Prcss_SV_Read(){
}
str += RcvOK;
return str;
return str + FIN;
}
String Prcss_SV_Write(unsigned int data[], int dataSize){
String str = "SV!:";
String message, cmd, cmdData;
cmd = "10DWR,08,";
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)
@ -102,7 +171,29 @@ String Prcss_SV_Write(unsigned int data[], int dataSize){
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_Wr, message);
Serial.println(cmdData);
Serial.println(message);
str += RcvOK;
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;
message += sumMC9(message);
message = "" + message + CRLF;
write_buff_first(Buff_485_Wr, message);
return RcvOK;
str += RcvOK;
return str + FIN;
}

@ -41,12 +41,19 @@ int recieve_485(){
// Timeout code
if(Wait_485){
Wait_485_cnt++;
if(Wait_485_cnt > 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 : ");

Loading…
Cancel
Save