0
xela chen

Realtek您好:

需要Realtek提供以下功能source code或library:

1.透過RS232設定Ameba為Modbus主站讀取從站PLC資料

Modbus master function
The purpose is to query an array of data
from an external Modbus slave device(PLC).
It allows you to use software serial instead of hardware serial.
In case that you want to use D1 & D2 for other purposes.
The link media can be USB or RS232.

**Arduino Modbus master 範例如下 :

#include <ModbusRtu.h>
#include <SoftwareSerial.h>

/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0); // this is master and RS-232 or USB-FTDI via software serial

SoftwareSerial mySerial(3, 5);//Create a SoftwareSerial object so that we can use software serial. Search "software serial" on Arduino.cc to find out more details.

void setup() {
Serial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
master.begin( &mySerial, 9600 ); // begin the ModBus object. The first parameter is the address of your SoftwareSerial address. Do not forget the "&". 9600 means baud-rate at 9600
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 1000;
u8state = 0;
}

2.提供Ameba使用的Modbus 指令(共13個指令)

**Arduino Modbus 指令範例如下:

This is an structe which contains a query to an slave device

modbus_t telegram;

void loop() {
switch( u8state ) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
telegram.u8id = 104; // slave address(從站站號)
telegram.u8fct = 4; // function code (this one is registers read)(功能碼)
telegram.u16RegAdd = 3; // start address in slave(寄存器起始號)
telegram.u16CoilsNo = 1; // number of elements (coils or registers) to read
telegram.au16reg = au16data; // pointer to a memory array in the Arduino

master.query( telegram ); // send query (only once)
u8state++;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 2000;
Serial.println(au16data[0]);//Or do something else!
}

感謝!

Xela

sean 已回應