[TCPIP] [AI]

- 파일 분리
-TCPIP
  ㄴ AI 및 AO::0000,FFFF 등 명령 수행 가능
  ㄴ Labview Test 파일
- 예제소스 (MCP4922)
main
Changwoo Park 2 years ago
parent 5091ae49f7
commit f96a5544b5

@ -6,23 +6,38 @@
#include "SC_AI.h" // Differential analog input collector
#include "SC_MC9.h" // MC9 Tempreture Controller
#define RcvOK "OK\r\n"
#define RcvErr "ER\r\n"
#define MODE_DEBUG false
// 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 };
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
// SCPI defaults to port 5025
EthernetServer server(5025);
EthernetClient client;
// HTTP defaults to port 5025
EthernetServer web(80);
unsigned long lastDataReceivedTime;
unsigned long timeoutPeriod = 5000; // 타임아웃 시간 (5초)
bool busy = false;
bool connection = false;
char buffer[1024];
int Size_AI = 8;
int Size_AI = 16;
int AI_VALUES[] = {
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int msCnt = 0;
unsigned long timer = 0;
void setup() {
// put your setup code here, to run once:
@ -35,14 +50,12 @@ void setup() {
SC_AI_setup();
SC_MC9_setup();
//
MsTimer2::set(50, AnalogIn);
MsTimer2::set(1000, AnalogIn_Print);
// Timer set
MsTimer2::set(10, periodic_10ms);
MsTimer2::start();
// Report address
Serial.print("MAC:");
Serial.print("MAC>>");
for (int i = 0; i < 6; i++) {
if (mac[i] < 16) {
Serial.print("0");
@ -53,37 +66,81 @@ void setup() {
}
}
Serial.println();
Serial.print("IP:");
Serial.print("IP >>");
Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
//SC_AI(AI_VALUES, Size_AI);
// check if client is connected
webReponse();
client = server.available();
if (client) {
// check if client is connected
Serial.println("Client Connected!!!");
// Do What Message Command
while(client.connected()){
int buffCnt = 0;
// check for command byte
if (client.available() > 0) {
char c;
String command;
String cmd;
unsigned int data[32];
int dataSize;
busy = true;
// Read message by byte
while ((c = client.read())) {
// Read data until LF or CR
if((c != 10) && (c != 13)){
command += c;
}else{
break;
}
}
cmd = demuxCMD(command, data, &dataSize);
if(cmd=="AI"){
client.print(Prcss_AI());
}else if(cmd=="AO"){
client.print(Prcss_AO(data, dataSize));
}else{
client.print(RcvErr);
}
// Debug
if(MODE_DEBUG){
//Serial.print("Received command: ");
//Serial.println(command);
}
busy = false;
// 데이터를 수신했으므로 타임아웃 타이머 초기화
lastDataReceivedTime = millis();
}// if end, client.available()
//Serial.println("end client available");
// 타임아웃 확인
if (millis() - lastDataReceivedTime > timeoutPeriod) {
Serial.println("Client Disconnected... (Timeout)");
client.stop();
}
// 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);
Serial.println("Client Disconnected...");
}
}
@ -94,17 +151,69 @@ void TCPIP_setup(byte mac[], IPAddress ip, IPAddress gateway, IPAddress subnet){
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
web.begin();
}
void AnalogIn(){
SC_AI(AI_VALUES, Size_AI);
void periodic_10ms(){
msCnt += 10;
if(!busy){
SC_AI(AI_VALUES, Size_AI);
// periodic_1s
if (msCnt > 1000){
msCnt = 0;
periodic_1s();
}
}
}
void AnalogIn_Print(){
void periodic_1s(){
for (int i = 0; i < 8; i++) {
//AnalogIn_Print();
}
void AnalogIn_Print(){
for (int i = 0; i < Size_AI; i++) {
Serial.print(AI_VALUES[i]);
Serial.print("\t");
}
Serial.println();
}
void webReponse(){
EthernetClient webClient = web.available();
if (webClient) {
Serial.println("Web client connected");
while (webClient.connected()) {
if (webClient.available()) {
// 웹 브라우저에서 요청이 도착한 경우
String request = webClient.readStringUntil('\r');
//Serial.println(request);
webClient.flush();
// 웹 브라우저에 출력할 문구 작성
String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: text/html\r\n\r\n";
response += "<html><body>";
response += Prcss_AI();
//response += "<h1>Hello, World!</h1>";
response += "</body></html>";
// 문구를 웹 브라우저로 전송
webClient.print(response);
delay(1);
// 클라이언트 연결 종료
webClient.stop();
}
}
}
}

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

@ -0,0 +1,36 @@
String demuxCMD(String command, unsigned int data[], int* dataSize) {
command.replace(" ", "");
// "::"를 기준으로 문자열을 분리
int separatorIndex = command.indexOf("::");
String leftPart = command.substring(0, separatorIndex);
String rightPart = command.substring(separatorIndex + 2);
// 우측의 16진수 배열을 분리하여 추출
const char* delimiter = ",";
int startIndex = 0;
int endIndex = rightPart.indexOf(delimiter);
int index = 0;
while (endIndex >= 0) {
String hexValue = rightPart.substring(startIndex, endIndex);
// 16진수 문자열을 16진수 숫자로 변환하여 data 배열에 저장
data[index] = strtoul(hexValue.c_str(), NULL, 16);
index++;
startIndex = endIndex + 1;
endIndex = rightPart.indexOf(delimiter, startIndex);
}
// 남은 마지막 16진수 배열 원소 처리
String hexValue = rightPart.substring(startIndex);
data[index] = strtoul(hexValue.c_str(), NULL, 16);
index++;
// dataSize에 배열 크기 저장
*dataSize = index;
// 왼쪽 부분인 "ABC"을 반환
return leftPart;
}

@ -7,19 +7,21 @@
#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
//cli();//stop interrupts
int i = 0;
for(i = 0 ; i < size ; i++){
arr[i] = i;
arr[i] = analogRead(aipin[i]);
}
sei();//allow interrupts
//sei();//allow interrupts
}

@ -0,0 +1,47 @@
// MCP4922 Demo code sinewave at 16 res top to bot.
// For comparison to MCP4725 operation (DAC_RESOLUTION=-5).
#include <SPI.h>
SPISettings settingsA(16000000, MSBFIRST, SPI_MODE0); // At 16 = SPI Clock = 8MHz.
int RCLKPin = 53; // pin 12 on the 74hc595 latch - nSS
int SRCLKPin = 52; // pin 11 on the 74hc595 shift register clock - SCK
int SERPin = 51; // MOSI
//////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200); // Start serial port (debug).
pinMode(RCLKPin, OUTPUT); // Set SPI control PINs to output.
pinMode(SRCLKPin, OUTPUT);
pinMode(SERPin, OUTPUT);
SPI.begin();
Serial.println("MCP4922 SPI Dual DAC SPI hardware mode");
noInterrupts();
}
//////////////////////////////////////////////////////////////////////////////
// 0 - A, 1 - B
//
void writeMCP4922_AB(byte AB, uint16_t v) {
v |=0xf000; // B15(A/B)=1 B, B14(BUF)=1 on, B13(GAn) 1=x1 B12(SHDNn) 1=off
if (!AB) v &= ~0x8000; // When zero clear B15 for A.
SPI.beginTransaction(settingsA);
digitalWrite(RCLKPin, LOW);
SPI.transfer( (0xff00 & v)>>8 );
SPI.transfer( 0x00ff & v );
digitalWrite(RCLKPin, HIGH);
SPI.endTransaction;
}
void loop() {
writeMCP4922_AB( 0, 4092 );
writeMCP4922_AB( 1, 3000 );
}
Loading…
Cancel
Save