> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/embedded-systems/resources/basic-hardware-and-firmware/interfacing-and-communication/lab-uart-rs485-rs232-protocol.md).

# LAB: UART, RS485, RS232 Protocol

## จุดประสงค์

1. เพื่อให้เข้าใจหลักการทำงานของ UART
2. เพื่อให้เข้าใจหลักการทำงานของ RS232 และ RS485

## LAB1: Basic UART Communication

การทดลองนี้จะใช้ Logic Analyzer แสดงข้อความการสื่อสารผ่าน UART pin ของ M5stack

### อุปกรณ์ทดลอง

<table><thead><tr><th width="374">Components</th><th>Quantity</th></tr></thead><tbody><tr><td><a href="https://docs.m5stack.com/#/en/app/demo-board">M5Stack Demo Board</a></td><td>1</td></tr><tr><td><a href="http://www.qdkingst.com/en">Logic Analyzer (รุ่น LA1016 or LA2016) </a></td><td>1</td></tr></tbody></table>

![อุปกรณ์การทดลอง](/files/a5NBwjD9GhwUUq0LSjgj)

### โปรแกรม

1. Arduino IDE
2. โปรแกรม KingstVIS

### การตั้งค่าโปรแกรม KingstVIS

* เชื่อมต่อ logic Analyzer กับ คอมพิวเตอร์&#x20;
* เปิดโปรแกรม Kingst VIS&#x20;
* ไปที่ Analyzers >> คลิ๊ก icon add(+) >> เลือก UART/RS232/485&#x20;

![ตั้งค่าเลือกประเภท Protocol ที่ต้องการวิเคราะห์](https://paper-attachments.dropbox.com/s_D18756BAE3E80869884585460B6ED96FBA8CCCFA19CD3E275B39B4FA7D912A30_1599708293476_11.png)

* ไปที่ Analyzers >> คลิ๊ก icon setting >> Edit  ตั้งค่าตามรูปด้านล่าง >> OK

![ตั้งค่า channel 3 ](/files/-MI39XBRc98jXuntXhkP)

* ไปที่ Analyzers อีกครั้ง >> คลิ๊ก icon setting >> Display Format >> ASCII  เพื่อให้โปรแกรมแสดงผลเป็นข้อความ

![ตั้งค่า channel 3 แสดงข้อความ](https://paper-attachments.dropbox.com/s_D18756BAE3E80869884585460B6ED96FBA8CCCFA19CD3E275B39B4FA7D912A30_1599791502747_1_1.png)

* ไปที่ Channel 3  เลือกกด Indicates "rising edge trigger"

![Trigger condition setting](/files/-MHu3jGoXJ81ffmTz-JM)

{% hint style="danger" %}
**Setup Arduino IDE for M5Stack**

* เข้าไปตั้งค่าตาม [การตั้งค่าM5stack](https://docs.m5stack.com/#/en/arduino/arduino_development)&#x20;
* เมื่อตั้งค่าเสร็จแล้ว

  * ไปที่ tool >> Board>> M5 stack core esp32&#x20;

  เพื่อทำการอัพโหลดข้อมูลลงไปยังบอร์ด
  {% endhint %}

### PIN Connect

รายชื่อ PIN เชื่อมต่อกัน

| M5Stack port   | Logic Analyzer |
| -------------- | -------------- |
| UART0 (pin 1T) | Channel 3      |
| UART0 (pin 3R) | -              |
| GND            | GND            |

ตำแหน่ง UART port ที่ใช้ในการทดลองนี้แสดงดังภาพด้านล่าง

![UART port on M5stack demo board](/files/-MIIuR9TYxxW_Hd27B8M)

### Code

{% code title="M5Stack print data" %}

```arduino
void setup() {
 Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
 Serial.println("On");
 delay(1000);
}
```

{% endcode %}

### บันทึกผลการทดลอง

1. บันทึกขั้นตอนการทดลอง
2. บันทึกภาพผลวงจรและภาพที่แสดงบน โปรแกรม logic Analyzer&#x20;
3. อธิบายผลการทดลอง

## LAB2: UART to RS232

&#x20;      การทดลองนี้เป็นการสื่อสารกันระหว่าง UART ของบอร์ด M5Stack กับ RS232 บน  [M5Stack Demo Board](https://docs.m5stack.com/#/en/app/demo-board) โดยใช้ [Logic Analyzer (รุ่น LA1016 or LA2016) ](http://www.qdkingst.com/en)ในการแสดงข้อความที่ M5Stack ส่งออกจากที่ pin UART ไปยัง RS232 port

### อุปกรณ์ทดลอง

<table><thead><tr><th width="374">Components</th><th>Quantity</th></tr></thead><tbody><tr><td><a href="https://docs.m5stack.com/#/en/app/demo-board">M5Stack Demo Board</a></td><td>1</td></tr><tr><td><a href="http://www.qdkingst.com/en">Logic Analyzer (รุ่น LA1016 or LA2016) </a></td><td>1</td></tr></tbody></table>

![อุปกรณ์การทดลอง](/files/a5NBwjD9GhwUUq0LSjgj)

### การตั้งค่าโปรแกรม KingstVIS &#x20;

* เชื่อมต่อ logic Analyzer กับ คอมพิวเตอร์&#x20;
* เปิดโปรแกรม Kingst VIS&#x20;
* ไปที่ Analyzers >> คลิ๊ก icon add(+) >> เลือก UART/RS232/485&#x20;

![ตั้งค่าเลือกประเภท Protocol ที่ต้องการวิเคราะห์](https://paper-attachments.dropbox.com/s_D18756BAE3E80869884585460B6ED96FBA8CCCFA19CD3E275B39B4FA7D912A30_1599708293476_11.png)

* *<mark style="color:red;">ให้ add ทั้งหมด 2 ครั้ง</mark>* และตั้งค่าแต่ละ add เป็น channel 0 และ 3 วิธีตั้งค่าดังตัวอย่างด้านล่าง
* ไปที่ Analyzers >> คลิ๊ก icon setting >> Edit  ตั้งค่าตามรูปด้านล่าง >> OK

![ ตั้งค่า channel 0](https://paper-attachments.dropbox.com/s_65E4F4CFBED8B1475A71CEBA11660ECDB42A7380296FC75A69A91C11B3A8D823_1639996008454_image.png)

![ตั้งค่า channel 3](https://paper-attachments.dropbox.com/s_65E4F4CFBED8B1475A71CEBA11660ECDB42A7380296FC75A69A91C11B3A8D823_1639996060765_image.png)

* ไปที่ Analyzers อีกครั้ง >> คลิ๊ก icon setting >> Display Format >> ASCII  เพื่อให้โปรแกรมแสดงผลเป็นข้อความ ทั้ง Channel 0 และ 3

![ ตั้งค่า channel 0 และ 3 แสดงข้อความ](https://paper-attachments.dropbox.com/s_D18756BAE3E80869884585460B6ED96FBA8CCCFA19CD3E275B39B4FA7D912A30_1599791502747_1_1.png)

* ไปที่ Channel 3  เลือกกด Indicates "rising edge trigger"

![Trigger condition setting](/files/-MHu3jGoXJ81ffmTz-JM)

### PIN Connect

| M5Stack port   | RS232 port           | Logic Analyzer |
| -------------- | -------------------- | -------------- |
| UART0 (pin 1T) | Header (pin Tx)      | Channel 3      |
| UART0 (pin 3R) | Header (pin Rx)      | -              |
| -              | DB9 connector(pin 2) | Channel 0      |
| -              | DB9 connector(pin 3) | Channel 1      |
| GND            | GND                  | GND            |

ตำแหน่ง RS232 port ที่ใช้ในการทดลองนี้แสดงดังภาพด้านล่าง

![UART0 port of M5Stack  and RS232 port on M5Stack Demo Board](/files/-MI2g7Ass3SdX90niL3i)

ตำแหน่งที่เชื่อมต่อ Logic analyzer เพื่อวิเคราะห์สัญญาณ

![เชื่อมต่อ Channel logic Analyzer to RS232 port](/files/-MINE0nUnU_AZCIeZlAo)

### code&#x20;

{% hint style="info" %}

#### "Code เดิมจากการทดลอง LAB UART "

{% endhint %}

### บันทึกผลการทดลอง

1. บันทึกขั้นตอนการทดลอง
2. บันทึกภาพผลวงจรและภาพที่แสดงบน โปรแกรม logic Analyzer&#x20;
3. อธิบายความเหมือนหรือแตกต่างของ Channel 0,1,3 ที่แสดงบนโปรแกรม Logic analyzer&#x20;

## LAB3: UART to RS485

การทดลองนี้ใช้ UART สื่อสารกันระหว่าง M5Stack กับ RS485 บน  [M5Stack Demo Board](https://docs.m5stack.com/#/en/app/demo-board) โดยใช้ Logic Analyzer ของ [ www.qdkingst.com ](http://www.qdkingst.com/en)ในการแสดงข้อความที่ M5Stack ส่งออกไปยัง RS485 port

### อุปกรณ์ทดลอง

<table><thead><tr><th width="374">Components</th><th>Quantity</th></tr></thead><tbody><tr><td><a href="https://docs.m5stack.com/#/en/app/demo-board">M5Stack Demo Board</a></td><td>1</td></tr><tr><td><a href="http://www.qdkingst.com/en">Logic Analyzer (รุ่น LA1016 or LA2016) </a></td><td>1</td></tr></tbody></table>

![อุปกรณ์การทดลอง](/files/a5NBwjD9GhwUUq0LSjgj)

### การตั้งค่าโปรแกรม KingstVIS &#x20;

* เชื่อมต่อ logic Analyzer กับ คอมพิวเตอร์&#x20;
* เปิดโปรแกรม Kingst VIS&#x20;
* ไปที่ Analyzers >> คลิ๊ก icon add(+) >> เลือก UART/RS232/485&#x20;

![ตั้งค่าเลือกประเภท Protocol ที่ต้องการวิเคราะห์](https://paper-attachments.dropbox.com/s_D18756BAE3E80869884585460B6ED96FBA8CCCFA19CD3E275B39B4FA7D912A30_1599708293476_11.png)

* *<mark style="color:red;">ให้ add ทั้งหมด 2 ครั้ง</mark>* และตั้งค่าแต่ละ add เป็น channel 0 และ 3 วิธีตั้งค่าดังตัวอย่างด้านล่าง
* ไปที่ Analyzers >> คลิ๊ก icon setting >> Edit  ตั้งค่าตามรูปด้านล่าง >> OK

![ ตั้งค่า channel 0](https://paper-attachments.dropbox.com/s_65E4F4CFBED8B1475A71CEBA11660ECDB42A7380296FC75A69A91C11B3A8D823_1639996008454_image.png)

![ ตั้งค่า channel 1](https://paper-attachments.dropbox.com/s_65E4F4CFBED8B1475A71CEBA11660ECDB42A7380296FC75A69A91C11B3A8D823_1639999067512_image.png)

![ตั้งค่า channel 3](https://paper-attachments.dropbox.com/s_65E4F4CFBED8B1475A71CEBA11660ECDB42A7380296FC75A69A91C11B3A8D823_1639996060765_image.png)

* ไปที่ Analyzers อีกครั้ง >> คลิ๊ก icon setting >> Display Format >> ASCII  เพื่อให้โปรแกรมแสดงผลเป็นข้อความ ทั้ง Channel 0, 1 และ 3

![ ตั้งค่า channel 0 และ 3 แสดงข้อความ](https://paper-attachments.dropbox.com/s_D18756BAE3E80869884585460B6ED96FBA8CCCFA19CD3E275B39B4FA7D912A30_1599791502747_1_1.png)

* ไปที่ Channel 3  เลือกกด Indicates "rising edge trigger"

![Trigger condition setting](/files/-MHu3jGoXJ81ffmTz-JM)

### PIN Connect

ชื่อ PIN ของบอร์ด M5Stack ต่อกับ RS485 port และ Logic Analyzer

| RS485 | Logic Analyzer | M5stack        |
| ----- | -------------- | -------------- |
| TXD   | Channel 3      | UART0 (pin 1T) |
| RXD   | -              | UART0 (pin 3R) |
| A     | Channel 1      | -              |
| B     | Channel 0      | -              |
| GND   | GND            | GND            |

ตำแหน่ง RS485 port ที่ใช้ในการทดลองนี้แสดงดังภาพด้านล่าง

![UART0 port of M5Stack  and RS485 port on M5Stack Demo Board](/files/-MI32o00DBUOxcbjIajW)

ตำแหน่งที่เชื่อมต่อ Logic analyzer เพื่อวิเคราะห์สัญญาณ

![เชื่อมต่อ Channel logic Analyzer to RS485 port](/files/-MINEPJzCyzmFbVtIzWv)

### code&#x20;

{% hint style="info" %}

#### "เดิมจากการทดลอง LAB UART to RS232"

{% endhint %}

### บันทึกผลการทดลอง

1. บันทึกภาพวงจรการทดลองและภาพผลที่แสดงบน โปรแกรม logic Analyzer
2. อธิบายความเหมือนหรือแตกต่างของ Channel 0,1,3 ที่แสดงบนโปรแกรม Logic analyzer&#x20;
3. จากการทดลองที่1,2 ให้อธิบายความเหมือนและต่างของการสื่อสารแบบ RS232 และ RS485 ลงในเอกสารบันทึกผลการทดลอง


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aic-eec.com/embedded-systems/resources/basic-hardware-and-firmware/interfacing-and-communication/lab-uart-rs485-rs232-protocol.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
