이 튜토리얼에서는 아두이노 우노 R4 와이파이를 사용하여 시리얼 RS232 장치를 와이파이에 연결하는 변환기를 만드는 방법을 배우게 됩니다. 이 설정을 사용하면, 아두이노는 시리얼 RS232 인터페이스에서 데이터를 읽고 TCP 서버에 데이터를 전송할 수 있으며, 이는 동일한 로컬 네트워크나 인터넷을 통해 가능합니다. 또한 TCP 서버로부터 데이터를 받아 시리얼 RS232 인터페이스를 통해 다시 전송할 수도 있습니다.
다음 단계를 따르면 Arduino를 사용하여 시리얼 RS-232 장치와 TCP/IP 서버 간의 유연한 통신 연결을 설정할 수 있습니다.
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-rs232-to-wifi */#include <WiFiS3.h>const char* WIFI_SSID = "YOUR_WIFI_SSID"; // 귀하의 WIFI SSID로 변경하세요const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; // 귀하의 WIFI 비밀번호로 변경하세요const char* TCP_SERVER_ADDR = "192.168.0.26"; // TCP 서버의 IP 주소로 변경하세요constint TCP_SERVER_PORT = 1470;WiFiClient TCP_client;voidsetup() {Serial.begin(9600);Serial.println("Arduino: TCP CLIENT");// check for the WiFi module:if (WiFi.status() == WL_NO_MODULE) {Serial.println("WiFi 모듈과의 통신 실패!");// don't continuewhile (true) ; }String fv = WiFi.firmwareVersion();if (fv < WIFI_FIRMWARE_LATEST_VERSION) {Serial.println("펌웨어를 업그레이드하십시오"); }Serial.print("SSID에 연결을 시도합니다: ");Serial.println(WIFI_SSID);// attempt to connect to WiFi network:while (WiFi.begin(WIFI_SSID, WIFI_PASSWORD) != WL_CONNECTED) {delay(10000); // 연결을 위해 10초 동안 기다립니다: }Serial.print("WiFi에 연결됨 ");Serial.println(WIFI_SSID);// connect to TCP serverif (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT))Serial.println("TCP 서버에 연결됨");elseSerial.println("TCP 서버에 연결 실패");}voidloop() {if (TCP_client.connected()) {// read data from TCP and send them to RS232 interfaceif (TCP_client.available()) {char c = TCP_client.read();Serial.write(c); }// read data from RS232 interface and send them to TCPif (Serial.available()) {char c = Serial.read(); TCP_client.write(c); } } else {Serial.println("연결이 끊어졌습니다"); TCP_client.stop();// reconnect to TCP serverif (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) {Serial.println("TCP 서버에 다시 연결됨"); } else {Serial.println("TCP 서버에 다시 연결 실패");delay(1000); } }}
소프트웨어 시리얼을 위한 아두이노 코드
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-rs232-to-wifi */#include <WiFiS3.h>#include <SoftwareSerial.h>SoftwareSerial rs232(7, 6); // RX: 7, TX: 6const char* WIFI_SSID = "YOUR_WIFI_SSID"; // 자신의 WIFI SSID로 변경하세요const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; // 자신의 WIFI 비밀번호로 변경하세요const char* TCP_SERVER_ADDR = "192.168.0.26"; // TCP 서버의 IP 주소로 변경하세요constint TCP_SERVER_PORT = 1470;WiFiClient TCP_client;voidsetup() {Serial.begin(9600); rs232.begin(9600);Serial.println("Arduino: TCP CLIENT");// check for the WiFi module:if (WiFi.status() == WL_NO_MODULE) {Serial.println("WiFi 모듈과의 통신 실패!");// don't continuewhile (true) ; }String fv = WiFi.firmwareVersion();if (fv < WIFI_FIRMWARE_LATEST_VERSION) {Serial.println("펌웨어 업그레이드가 필요합니다"); }Serial.print("SSID에 연결을 시도합니다: ");Serial.println(WIFI_SSID);// attempt to connect to WiFi network:while (WiFi.begin(WIFI_SSID, WIFI_PASSWORD) != WL_CONNECTED) {delay(10000); // 연결을 위해 10초 기다립니다: }Serial.print("WiFi에 연결되었습니다 ");Serial.println(WIFI_SSID);// connect to TCP serverif (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT))Serial.println("TCP 서버에 연결되었습니다");elseSerial.println("TCP 서버에 연결 실패");}voidloop() {if (TCP_client.connected()) {// read data from TCP and send them to RS232 interfaceif (TCP_client.available()) {char c = TCP_client.read(); rs232.write(c); }// read data from RS232 interface and send them to TCPif (rs232.available()) {char c = rs232.read(); TCP_client.write(c); } } else {Serial.println("연결이 끊어졌습니다"); TCP_client.stop();// reconnect to TCP serverif (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) {Serial.println("TCP 서버에 다시 연결되었습니다"); } else {Serial.println("TCP 서버에 재연결 실패");delay(1000); } }}
테스팅
다음 흐름으로 데이터를 보내어 테스트를 할 수 있습니다:
PC상의 직렬 소프트웨어 → RS-232 → 아두이노 → 와이파이 → PC상의 TCP 서버 소프트웨어.
PC상의 TCP 서버 소프트웨어 → 와이파이 → 아두이노 → RS-232 → PC상의 직렬 소프트웨어.