Supachai Vorapojpisut
/
TAIST_modbus
Example code for modbus serial
main.cpp@1:0007712e84a8, 2018-03-16 (annotated)
- Committer:
- vsupacha
- Date:
- Fri Mar 16 04:34:08 2018 +0000
- Revision:
- 1:0007712e84a8
- Parent:
- 0:f306cb0263a6
Draft of complete solution
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsupacha | 0:f306cb0263a6 | 1 | #include "mbed.h" |
vsupacha | 0:f306cb0263a6 | 2 | #include "modbus.h" |
vsupacha | 1:0007712e84a8 | 3 | #include "RawSerial.h" |
vsupacha | 0:f306cb0263a6 | 4 | |
vsupacha | 0:f306cb0263a6 | 5 | DigitalOut myled(LED1); |
vsupacha | 0:f306cb0263a6 | 6 | RawSerial pc(USBTX, USBRX); |
vsupacha | 1:0007712e84a8 | 7 | InterruptIn button(USER_BUTTON); |
vsupacha | 1:0007712e84a8 | 8 | Ticker timebase; |
vsupacha | 1:0007712e84a8 | 9 | |
vsupacha | 1:0007712e84a8 | 10 | uint32_t Tick = 0; |
vsupacha | 1:0007712e84a8 | 11 | uint32_t pressedTick = 0; |
vsupacha | 1:0007712e84a8 | 12 | uint32_t releasedTick = 0; |
vsupacha | 0:f306cb0263a6 | 13 | |
vsupacha | 0:f306cb0263a6 | 14 | void serial_callback() |
vsupacha | 0:f306cb0263a6 | 15 | { |
vsupacha | 0:f306cb0263a6 | 16 | uint8_t frame[6]; |
vsupacha | 1:0007712e84a8 | 17 | uint16_t offset, count; |
vsupacha | 1:0007712e84a8 | 18 | uint8_t reg_map; |
vsupacha | 0:f306cb0263a6 | 19 | |
vsupacha | 0:f306cb0263a6 | 20 | char ch = pc.getc(); |
vsupacha | 0:f306cb0263a6 | 21 | if (modbus_parser(ch, frame)) { |
vsupacha | 1:0007712e84a8 | 22 | offset = (frame[2] << 8) | (frame[3]); |
vsupacha | 1:0007712e84a8 | 23 | count = (frame[4] << 8) | (frame[5]); |
vsupacha | 1:0007712e84a8 | 24 | reg_map = modbus_check(offset, count); |
vsupacha | 1:0007712e84a8 | 25 | modbus_response(reg_map); |
vsupacha | 1:0007712e84a8 | 26 | } |
vsupacha | 1:0007712e84a8 | 27 | } |
vsupacha | 1:0007712e84a8 | 28 | |
vsupacha | 1:0007712e84a8 | 29 | void pressed_callback() |
vsupacha | 1:0007712e84a8 | 30 | { |
vsupacha | 1:0007712e84a8 | 31 | uint16_t skipCount; |
vsupacha | 1:0007712e84a8 | 32 | |
vsupacha | 1:0007712e84a8 | 33 | if ((Tick > pressedTick) && (Tick > releasedTick)) { |
vsupacha | 1:0007712e84a8 | 34 | skipCount = Tick - releasedTick; |
vsupacha | 1:0007712e84a8 | 35 | modbus_update(0x0000, skipCount); |
vsupacha | 1:0007712e84a8 | 36 | pressedTick = Tick; |
vsupacha | 1:0007712e84a8 | 37 | // printf("Pressed: %d - %d - %d\r\n", pressedTick, releasedTick, skipCount); |
vsupacha | 0:f306cb0263a6 | 38 | } |
vsupacha | 0:f306cb0263a6 | 39 | } |
vsupacha | 0:f306cb0263a6 | 40 | |
vsupacha | 1:0007712e84a8 | 41 | void released_callback() |
vsupacha | 1:0007712e84a8 | 42 | { |
vsupacha | 1:0007712e84a8 | 43 | uint16_t widthCount; |
vsupacha | 1:0007712e84a8 | 44 | |
vsupacha | 1:0007712e84a8 | 45 | if ((Tick > pressedTick) && (Tick > releasedTick)) { |
vsupacha | 1:0007712e84a8 | 46 | widthCount = Tick - pressedTick; |
vsupacha | 1:0007712e84a8 | 47 | modbus_update(0x0001, widthCount); |
vsupacha | 1:0007712e84a8 | 48 | releasedTick = Tick; |
vsupacha | 1:0007712e84a8 | 49 | // printf("Released: %d - %d - %d\r\n", pressedTick, releasedTick, widthCount); |
vsupacha | 1:0007712e84a8 | 50 | } |
vsupacha | 1:0007712e84a8 | 51 | } |
vsupacha | 1:0007712e84a8 | 52 | |
vsupacha | 1:0007712e84a8 | 53 | void ticker_callback() |
vsupacha | 1:0007712e84a8 | 54 | { |
vsupacha | 1:0007712e84a8 | 55 | Tick++; |
vsupacha | 1:0007712e84a8 | 56 | } |
vsupacha | 1:0007712e84a8 | 57 | |
vsupacha | 0:f306cb0263a6 | 58 | int main() { |
vsupacha | 0:f306cb0263a6 | 59 | // setup code |
vsupacha | 1:0007712e84a8 | 60 | pc.attach(&serial_callback); |
vsupacha | 1:0007712e84a8 | 61 | |
vsupacha | 0:f306cb0263a6 | 62 | // 1. button code |
vsupacha | 1:0007712e84a8 | 63 | button.rise(&pressed_callback); |
vsupacha | 1:0007712e84a8 | 64 | button.fall(&released_callback); |
vsupacha | 1:0007712e84a8 | 65 | |
vsupacha | 0:f306cb0263a6 | 66 | // 2. timer code |
vsupacha | 1:0007712e84a8 | 67 | timebase.attach(&ticker_callback, 0.1); |
vsupacha | 1:0007712e84a8 | 68 | printf("Starting\n"); |
vsupacha | 1:0007712e84a8 | 69 | printf("Test: %02X\r\n", modbus_check(0x0000, 0x0001)); |
vsupacha | 1:0007712e84a8 | 70 | printf("Test: %02X\r\n", modbus_check(0x0001, 0x0001)); |
vsupacha | 1:0007712e84a8 | 71 | printf("Test: %02X\r\n", modbus_check(0x0000, 0x0002)); |
vsupacha | 1:0007712e84a8 | 72 | printf("Test: %02X\r\n", modbus_check(0x0000, 0x0003)); |
vsupacha | 1:0007712e84a8 | 73 | printf("Test: %02X\r\n", modbus_check(0x0001, 0x0002)); |
vsupacha | 1:0007712e84a8 | 74 | printf("Test: %02X\r\n", modbus_check(0x0002, 0x0002)); |
vsupacha | 0:f306cb0263a6 | 75 | while(1) { |
vsupacha | 1:0007712e84a8 | 76 | // blinking LED |
vsupacha | 1:0007712e84a8 | 77 | myled = !myled; |
vsupacha | 1:0007712e84a8 | 78 | wait(2.5); |
vsupacha | 1:0007712e84a8 | 79 | printf("Detection: %d - %d\r\n", pressedTick, releasedTick); |
vsupacha | 1:0007712e84a8 | 80 | modbus_response(0x01); |
vsupacha | 1:0007712e84a8 | 81 | modbus_response(0x02); |
vsupacha | 1:0007712e84a8 | 82 | modbus_response(0x03); |
vsupacha | 0:f306cb0263a6 | 83 | } |
vsupacha | 0:f306cb0263a6 | 84 | } |