
Modbus with stm 32
Revision 0:40b96f9186c3, committed 2018-03-14
- Comitter:
- lhakpa
- Date:
- Wed Mar 14 11:28:39 2018 +0000
- Child:
- 1:00f04cdf25aa
- Commit message:
- modbus Work
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 14 11:28:39 2018 +0000 @@ -0,0 +1,29 @@ +#include "mbed.h" +#include "modbus.h" + +DigitalOut myled(LED1); +RawSerial pc(USBTX, USBRX); + +void serial_callback() +{ + uint8_t frame[6]; + + char ch = pc.getc(); + if (modbus_parser(ch, frame)) { + + } +} + +int main() { + // setup code + pc.attach(serial_callback); + // 1. button code + // 2. timer code + while(1) { + // loop code + myled = 1; // LED is ON + wait(0.2); // 200 ms + myled = 0; // LED is OFF + wait(1.0); // 1 sec + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Mar 14 11:28:39 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/aa5281ff4a02 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modbus.cpp Wed Mar 14 11:28:39 2018 +0000 @@ -0,0 +1,103 @@ +#include "mbed.h" +#include "modbus.h" + +uint8_t nodeId = 0; +uint16_t regValue[ADDR_RANGE]; + +enum state {IDLE, RECEPTION, END} protState = IDLE; + +int process_buffer(char *buf, uint8_t *frame) +{ + int status = 0; + uint8_t sum = 0; + uint8_t lrc, i; + char tmpbuf[] = {0, 0, 0}; + + if (strlen(buf) == 14) { + for (i = 0; i < 6; i++) { + tmpbuf[0] = buf[i*2]; + tmpbuf[1] = buf[i*2 + 1]; + frame[i] = strtoul(tmpbuf, NULL, 16); + } + tmpbuf[0] = buf[12]; tmpbuf[1] = buf[13]; + lrc = strtoul(tmpbuf, NULL, 16); + for (i = 0; i < 6; i++) { + sum += frame[i]; + } + if ((sum + lrc) == 0) { + status = 1; + } + } + + return status; +} + +void modbus_init(uint8_t id) +{ + nodeId = id; +} + +uint16_t modbus_read(uint16_t offset) +{ + if (offset < ADDR_RANGE) { + return regValue[offset]; + } + return 0xFFFF; +} + +uint16_t modbus_update(uint8_t offset, uint16_t val) +{ + uint16_t tmp; + + if (offset < ADDR_RANGE) { + tmp = regValue[offset]; + regValue[offset] = val; + return tmp; + } + return 0xFFFF; +} + +int modbus_parser(char ch, uint8_t *frame) +{ + static char buf[514]; + static int idx = 0; + static int status = 0; + + switch(protState) { + case IDLE: + if (ch == ':') { + protState = RECEPTION; + idx = 0; + status = 0; + } + break; + case RECEPTION: + if ((ch >= '0') && (ch <= '9')) { + buf[idx++] = ch; + } else if ((ch >= 'a') && (ch <= 'f')) { + buf[idx++] = ch; + } else if ((ch >= 'A') && (ch <= 'F')) { + buf[idx++] = ch; + } else if (ch == '\r') { + buf[idx] = 0; + protState = END; + } else { + protState = IDLE; + } + break; + case END: + if (ch == '\n') { + if (process_buffer(buf, frame)) { + if ((frame[0] == nodeId) && (frame[1] == FUNC_CODE)) { + status = 1; + } + } + } + protState = IDLE; + break; + default: + protState = IDLE; + } + + return status; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modbus.h Wed Mar 14 11:28:39 2018 +0000 @@ -0,0 +1,14 @@ +#ifndef MODBUS_H +#define MODBUS_H + +#define FUNC_CODE 3 +#define ADDR_BASE 0x0000 +#define ADDR_RANGE 2 +#define MAX_FRAME_SIZE 20 + +void modbus_init(uint8_t id); +uint16_t modbus_read(uint16_t offset); +uint16_t modbus_update(uint8_t offset, uint16_t val); +int modbus_parser(char ch, uint8_t *frame); + +#endif // MODBUS_H