Last active
January 31, 2021 23:52
-
-
Save TakehikoShimojima/b7f25828bae3df70368ebcecb1c55e73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5StickC.h> | |
#include <ModbusRTU.h> | |
#define RX_PIN 26 // RS485HatのRXピン | |
#define TX_PIN 0 // RS485HatのTXピン | |
#define REGN 10 | |
#define SLAVE_ID 1 | |
ModbusRTU mb; | |
Modbus::ResultCode result; | |
void setup() { | |
M5.begin(); | |
M5.Lcd.setTextSize(2); | |
M5.Lcd.setRotation(3); | |
Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN); // Serial2を初期設定 | |
mb.begin(&Serial2); // Serial2のアドレスを渡してmodbusを初期設定する | |
mb.master(); // 自分自身をマスタに設定 | |
} | |
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { | |
result = event; // リクエストの終了ステータスをresultにセットする | |
return true; | |
} | |
uint16_t value; | |
void loop() { | |
while (mb.slave()) ; // スレーブがリクエストを処理中なら待つ | |
mb.readIreg(SLAVE_ID, REGN, &value, 1, cb); // スレーブのIregを読む | |
while (mb.slave()) { // スレーブのリクエスト完了を待つ | |
mb.task(); | |
delay(100); | |
} | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setCursor(0, 0); | |
if (result == Modbus::EX_SUCCESS) { | |
M5.Lcd.printf("val: %d\r\n", value); | |
} else { | |
M5.Lcd.printf("err: %2x\r\n", result); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment