Modbus là một protocol dùng khá phổ biến trên thế giới. Hầu hết các thiết bị điện tử công nghiệp điều có hỗ trợ chuẩn giao tiếp này. Vì vậy trên các dòng Vi điều khiển lập trình cũng không thể thiếu việc đọc dữ liệu qua chuẩn giao tiếp này. Bài viết hôm nay sẽ giúp các bạn biến Kit STM32F103RC của mình thành một thiết bị Slave giao tiếp bằng Protocol Modbus RTU.
Source code
#include <Arduino.h>
#include <ModbusRtu.h>
#include <HardwareSerial.h>
#define LED PD2
HardwareSerial Serial_Slave(PA10, PA9);
uint16_t au16data[16] = {
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, 1};
Modbus slave(1, Serial_Slave, 0); // this is slave @1 and RS-232 or USB-FTDI
void setup()
{
pinMode(LED, OUTPUT);
Serial_Slave.begin(19200); // baud-rate at 19200
slave.start();
digitalWrite(LED, HIGH);
}
void loop()
{
slave.poll(au16data, 16);
if (au16data[0] == 1)
{
digitalWrite(LED, LOW);
}
else
{
digitalWrite(LED, HIGH);
}
}
No comments:
Post a Comment