summaryrefslogtreecommitdiff
path: root/src/hal/user_comps/mb2hal/examples/mb2hal_example_01_arduino/mb2hal_example_01_arduino.ino
blob: 9cd1c68fc2d43b0ee18b14044f1087646db31bc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * mb2hal_example_01_arduino.ino
 * Arduino sketch to connect to HAL's LinuxCNC using mb2hal component.
 * Tested 2012-10-12 with Arduino Mega 2560 R3.
 *
 * Victor Rocco, adapted from Stéphane Raimbault's source code wich is
 * Copyright © 2011-2012 Stéphane Raimbault <stephane.raimbault@gmail.com>
 * License ISC, see LICENSE for more details.
 */

#include <HardwareSerial.h>
#include <Modbusino.h>

/* Initialize the slave with the ID 1 */
ModbusinoSlave modbusino_slave(1);
/* Allocate a mapping of 10 values */
uint16_t tab_reg[10];

void setup() {
    /* The transfer speed is set to 115200 bauds */
    modbusino_slave.setup(115200);
}

void loop() {
    /* Launch Modbus slave loop with:
       - pointer to the mapping
       - max values of mapping */
    modbusino_slave.loop(tab_reg, 10);
    /* Copy the INPUT registers (5-9) to the OUTPUT registers (0-4) */
    tab_reg[0] = tab_reg[5];
    tab_reg[1] = tab_reg[6];
    tab_reg[2] = tab_reg[7];
    tab_reg[3] = tab_reg[8];
    tab_reg[4] = tab_reg[9];
}