From 5091ae49f746bed89435a0cf48d6de5e292df00c Mon Sep 17 00:00:00 2001 From: Changwoo Park Date: Thu, 4 May 2023 17:35:18 +0900 Subject: [PATCH] =?UTF-8?q?[TCPIP]=20[Timer]=20-=20TCPIP=20=20=20=E3=84=B4?= =?UTF-8?q?=20TCPIP=EB=8A=94=20Main=20Loop=EC=97=90=EC=84=9C=20=EB=8F=99?= =?UTF-8?q?=EC=9E=91=20=20=20=E3=84=B4=20TCPIP=20=EB=AA=85=EB=A0=B9?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=88=98=ED=96=89=20=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=95=84=EC=9B=83=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Timer ㄴ Serial print 동작하나 (1sec) ㄴ SC_AI 는 미동작 *원인 파악 필요 - 기타 ㄴ 485 read & write 구현 완료 ㄴ MC9 프로토콜 적용 필요 ㄴ AI C/O 필요 --- FC_InferfaceBoard/FC_InferfaceBoard.ino | 103 +++++++++++++++++++++--- libraries/Scitech/SC_AI.cpp | 25 ++++++ libraries/Scitech/SC_AI.h | 7 ++ libraries/Scitech/SC_MC9.cpp | 32 ++++++++ libraries/Scitech/SC_MC9.h | 15 ++++ libraries/Scitech/Sc_AnalogIn.cpp | 28 ------- libraries/Scitech/Sc_AnalogIn.h | 12 --- 7 files changed, 172 insertions(+), 50 deletions(-) create mode 100644 libraries/Scitech/SC_AI.cpp create mode 100644 libraries/Scitech/SC_AI.h create mode 100644 libraries/Scitech/SC_MC9.cpp create mode 100644 libraries/Scitech/SC_MC9.h delete mode 100644 libraries/Scitech/Sc_AnalogIn.cpp delete mode 100644 libraries/Scitech/Sc_AnalogIn.h diff --git a/FC_InferfaceBoard/FC_InferfaceBoard.ino b/FC_InferfaceBoard/FC_InferfaceBoard.ino index cbc0ccb..08c503f 100644 --- a/FC_InferfaceBoard/FC_InferfaceBoard.ino +++ b/FC_InferfaceBoard/FC_InferfaceBoard.ino @@ -1,4 +1,23 @@ -#include "Sc_AnalogIn.h" // Differential analog input collector +#include // Timer +#include + +#include // ?? + +#include "SC_AI.h" // Differential analog input collector +#include "SC_MC9.h" // MC9 Tempreture Controller + +// Enter a MAC address and IP address for your controller below. +// The IP address will be dependent on your local network. +// gateway and subnet are optional: +byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 }; +IPAddress ip(192, 168, 20, 177); +IPAddress gateway(192, 168, 20, 1); +IPAddress subnet(255, 255, 255, 0); + +// telnet defaults to port 23 +EthernetServer server(23); +boolean alreadyConnected = false; // whether or not the client was connected previously int Size_AI = 8; int AI_VALUES[] = { @@ -9,19 +28,83 @@ void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println("Start!"); + + TCPIP_setup(mac, ip, gateway, subnet); + + // scitech korea Library + SC_AI_setup(); + SC_MC9_setup(); + + + // + MsTimer2::set(50, AnalogIn); + MsTimer2::set(1000, AnalogIn_Print); + MsTimer2::start(); + + // 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() { - // put your main code here, to run repeatedly: - int i = 0; + // wait for a new client: + EthernetClient client = server.available(); + //SC_AI(AI_VALUES, Size_AI); + // check if client is connected + if (client) { - AnalogIn(AI_VALUES, Size_AI); + // check for command byte + if (client.available() > 0) { + cli();//stop interrupts + Serial.print(" "); + byte command = client.read(); + if (command == 'A'){ + client.print("AI"); + SC_MC9_read(AI_VALUES, Size_AI); + } + if (command == 'B'){ + client.print("AO"); + SC_MC9_write(AI_VALUES, Size_AI); + } + sei();//allow interrupts + } + } + //delay(50); - for (i = 0; i < 8; i++) { - Serial.print(AI_VALUES[i]); - // Serial.println(AnalogTest(3)); +} - } - Serial.println(""); - delay(1000); + +void TCPIP_setup(byte mac[], IPAddress ip, IPAddress gateway, IPAddress subnet){ + mac[5] = ip[3] & 0xFF; + // initialize the ethernet device + Ethernet.begin(mac, ip, gateway, subnet); + // start listening for clients + server.begin(); } + +void AnalogIn(){ + SC_AI(AI_VALUES, Size_AI); +} + +void AnalogIn_Print(){ + + for (int i = 0; i < 8; i++) { + Serial.print(AI_VALUES[i]); + + } + Serial.println(); +} \ No newline at end of file diff --git a/libraries/Scitech/SC_AI.cpp b/libraries/Scitech/SC_AI.cpp new file mode 100644 index 0000000..8748ff1 --- /dev/null +++ b/libraries/Scitech/SC_AI.cpp @@ -0,0 +1,25 @@ +/* + History: + 20230504 - V0.2 + 20230504 - V0.1 init. +*/ + +#include + +#include + +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] = i; + } + sei();//allow interrupts +} + diff --git a/libraries/Scitech/SC_AI.h b/libraries/Scitech/SC_AI.h new file mode 100644 index 0000000..46f34ea --- /dev/null +++ b/libraries/Scitech/SC_AI.h @@ -0,0 +1,7 @@ +#ifndef SC_AI_h +#define SC_AI_h + +void SC_AI_setup(); +void SC_AI(int arr[], int size); + +#endif diff --git a/libraries/Scitech/SC_MC9.cpp b/libraries/Scitech/SC_MC9.cpp new file mode 100644 index 0000000..9dd62d7 --- /dev/null +++ b/libraries/Scitech/SC_MC9.cpp @@ -0,0 +1,32 @@ +/* + History: + 20230504 - V0.2 + 20230504 - V0.1 init. +*/ + +#include + +#include +//#include + +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 +} \ No newline at end of file diff --git a/libraries/Scitech/SC_MC9.h b/libraries/Scitech/SC_MC9.h new file mode 100644 index 0000000..0af6355 --- /dev/null +++ b/libraries/Scitech/SC_MC9.h @@ -0,0 +1,15 @@ +#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 diff --git a/libraries/Scitech/Sc_AnalogIn.cpp b/libraries/Scitech/Sc_AnalogIn.cpp deleted file mode 100644 index 61ad925..0000000 --- a/libraries/Scitech/Sc_AnalogIn.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - MsTimer2.h - Using timer2 with 1ms resolution - Javier Valencia - - https://github.com/PaulStoffregen/MsTimer2 - - History: - 20230504 - V0.2 - 20230504 - V0.1 init. - -*/ - -#include - -void AnalogIn(int arr[], int size){ - cli();//stop interrupts - - int i = 0; - for(i = 0 ; i < size ; i++){ - arr[i] = i; - } - sei();//allow interrupts -} - -int AnalogTest(int i){ - - return i+4; -} \ No newline at end of file diff --git a/libraries/Scitech/Sc_AnalogIn.h b/libraries/Scitech/Sc_AnalogIn.h deleted file mode 100644 index 2513aeb..0000000 --- a/libraries/Scitech/Sc_AnalogIn.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef Sc_AnalogIn_h -#define Sc_AnalogIn_h - -void AnalogIn(int arr[], int size); -int AnalogTest(int i); -#endif - -#ifdef __AVR__ -#include -#elif defined(__arm__) && defined(TEENSYDUINO) -#include -#endif \ No newline at end of file