การพัฒนาโปรแกรม ส่งข้อมูลผ่าน RS-232:
RS-232 เป็นมาตรฐาน (Standard) สำหรับการสื่อสารข้อมูลแบบอนุกรมผ่านสายสัญญาณ (serial communication) ที่กำหนดโดย EIA (Electronic Industries Alliance) ตั้งแต่ปี ค.ศ. 1960 ซึ่ง RS-232 กำหนดมาตรฐานสำหรับระดับแรงดันไฟฟ้า (Voltage levels), ความเร็วในการส่งข้อมูล (Baud rate), การเชื่อมต่อสาย (Pinouts) และโปรโตคอลการสื่อสาร การส่งข้อมูลการตรวจวัดจากบอร์ด Arduino ไปให้การโปรแกรม Python ที่รันอยู่บนเครื่องคอมพิวเตอร์ จะเริ่มต้นจากการเขียนโปรแกรมอ่านข้อมูลจากเซนเซอร์ที่เชื่อมต่ออยู่กับพิน Analog - Digital ต่างๆ และเขียนโปรแกรมให้ส่งข้อมูลออกไปสู่ Serial port หลังจากนั้นจะต้องเขียนโปรแกรม Python ที่รันอยู่บนเครื่องคอมพิวเตอร์ให้สามารถรับข้อมูล และแสดงผลข้อมูลรูปแบบต่างๆ ได้
โปรแกรมสำหรับอ่านข้อมูลจากพิน Digital (D2) และส่งข้อมูลออกไปสู่ Serial port มีรูปแบบดังนี้
void setup() {
pinMode(2, INPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(2);
if (state == HIGH) {
Serial.println("HIGH");
} else {
Serial.println("LOW");
}
delay(500);
}
การเขียนโปรแกรมบนโปรแกรม ArduinoIDE แสดงตามภาพด้านล่าง
โปรแกรมสำหรับอ่านข้อมูลจากพิน Digital (D2) และส่งข้อมูลออกไปสู่ Serial port มีรูปแบบดังนี้
from tkinter import Tk
from tk_tools import Led
import serial
import threading
def read_arduino():
while True:
if ser.in_waiting:
state = ser.readline().decode('utf-8').strip()
if state == "HIGH":
led.to_green(on=True)
else:
led.to_green(on=False)
def close_app():
ser.close()
root.destroy()
# Setup serial connection
ser = serial.Serial('COM7', 9600, timeout=1) # Change 'COM3' to your Arduino port
root = Tk()
root.title("LED Example")
root.geometry("600x500+300+300")
# Create and display LED
led = Led(root, size=50)
led.place(x=250,y=80)
thread = threading.Thread(target=read_arduino, daemon=True)
thread.start()
root.protocol("WM_DELETE_WINDOW", close_app)
root.mainloop()
โปรแกรมสำหรับควบคุมพิน Digital (D13) จาก Serial port มีรูปแบบดังนี้
int ledPin = 13;
char command;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
command = Serial.read();
if (command == '1') {
digitalWrite(ledPin, HIGH);
} else if (command == '0') {
digitalWrite(ledPin, LOW);
}
}
}
การเขียนโปรแกรมบนโปรแกรม ArduinoIDE แสดงตามภาพด้านล่าง
โปรแกรมสำหรับอ่านข้อมูลจากพิน Digital (D2) และส่งข้อมูลออกไปสู่ Serial port มีรูปแบบดังนี้
from tkinter import Tk, Button
from tk_tools import Led
import serial
def onled():
ser.write(b'1')
def offled():
ser.write(b'0')
def close_app():
ser.close() # Close the serial connection
root.destroy() # Close the window
root = Tk()
root.title("LED Example")
root.geometry("600x500+300+300")
# Setup serial connection
ser = serial.Serial('COM7', 9600, timeout=1) # Change 'COM3' to your Arduino port
button = Button(root, text="on", font=('Courier 20 bold'), command=onled, width=5, height=1, bg="gray")
button.place(x=150, y=200)
button = Button(root, text="off", font=('Courier 20 bold'), command=offled, width=5, height=1, bg="gray")
button.place(x=350, y=200)
root.protocol("WM_DELETE_WINDOW", close_app)
root.mainloop()
โปรแกรมสำหรับควบคุมพิน Digital (D13) จาก Serial port มีรูปแบบดังนี้
void setup() {
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
int commaIndex = command.indexOf(',');
if (commaIndex != -1) {
int pin = command.substring(0, commaIndex).toInt();
int state = command.substring(commaIndex + 1).toInt();
digitalWrite(pin, state);
}
}
}
การเขียนโปรแกรมบนโปรแกรม ArduinoIDE แสดงตามภาพด้านล่าง
โปรแกรมสำหรับอ่านข้อมูลจากพิน Digital (D2) และส่งข้อมูลออกไปสู่ Serial port มีรูปแบบดังนี้
from tkinter import Tk, Button
from tk_tools import Led
import serial
def onled_1():
ser.write(b'13,1')
def offled_1():
ser.write(b'13,0')
def onled_2():
ser.write(b'2,1')
def offled_2():
ser.write(b'2,0')
def close_app():
ser.close() # Close the serial connection
root.destroy() # Close the window
root = Tk()
root.title("LED Example")
root.geometry("600x500+300+300")
# Setup serial connection
ser = serial.Serial('COM7', 9600, timeout=1) # Change 'COM3' to your Arduino port
button = Button(root, text="on_1", font=('Courier 20 bold'), command=onled_1, width=5, height=1, bg="gray")
button.place(x=150, y=200)
button = Button(root, text="off_1", font=('Courier 20 bold'), command=offled_1, width=5, height=1, bg="gray")
button.place(x=350, y=200)
button = Button(root, text="on_2", font=('Courier 20 bold'), command=onled_2, width=5, height=1, bg="gray")
button.place(x=150, y=300)
button = Button(root, text="off_2", font=('Courier 20 bold'), command=offled_2, width=5, height=1, bg="gray")
button.place(x=350, y=300)
root.protocol("WM_DELETE_WINDOW", close_app)
root.mainloop()