Modbus with stm 32

Dependencies:   mbed

Committer:
lhakpa
Date:
Wed Mar 14 11:33:36 2018 +0000
Revision:
1:00f04cdf25aa
Parent:
0:40b96f9186c3
add comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lhakpa 0:40b96f9186c3 1 #include "mbed.h"
lhakpa 0:40b96f9186c3 2 #include "modbus.h"
lhakpa 0:40b96f9186c3 3
lhakpa 0:40b96f9186c3 4 uint8_t nodeId = 0;
lhakpa 0:40b96f9186c3 5 uint16_t regValue[ADDR_RANGE];
lhakpa 0:40b96f9186c3 6
lhakpa 0:40b96f9186c3 7 enum state {IDLE, RECEPTION, END} protState = IDLE;
lhakpa 0:40b96f9186c3 8
lhakpa 0:40b96f9186c3 9 int process_buffer(char *buf, uint8_t *frame)
lhakpa 0:40b96f9186c3 10 {
lhakpa 0:40b96f9186c3 11 int status = 0;
lhakpa 0:40b96f9186c3 12 uint8_t sum = 0;
lhakpa 0:40b96f9186c3 13 uint8_t lrc, i;
lhakpa 0:40b96f9186c3 14 char tmpbuf[] = {0, 0, 0};
lhakpa 0:40b96f9186c3 15
lhakpa 0:40b96f9186c3 16 if (strlen(buf) == 14) {
lhakpa 0:40b96f9186c3 17 for (i = 0; i < 6; i++) {
lhakpa 0:40b96f9186c3 18 tmpbuf[0] = buf[i*2];
lhakpa 0:40b96f9186c3 19 tmpbuf[1] = buf[i*2 + 1];
lhakpa 0:40b96f9186c3 20 frame[i] = strtoul(tmpbuf, NULL, 16);
lhakpa 0:40b96f9186c3 21 }
lhakpa 0:40b96f9186c3 22 tmpbuf[0] = buf[12]; tmpbuf[1] = buf[13];
lhakpa 0:40b96f9186c3 23 lrc = strtoul(tmpbuf, NULL, 16);
lhakpa 0:40b96f9186c3 24 for (i = 0; i < 6; i++) {
lhakpa 0:40b96f9186c3 25 sum += frame[i];
lhakpa 0:40b96f9186c3 26 }
lhakpa 0:40b96f9186c3 27 if ((sum + lrc) == 0) {
lhakpa 0:40b96f9186c3 28 status = 1;
lhakpa 0:40b96f9186c3 29 }
lhakpa 0:40b96f9186c3 30 }
lhakpa 0:40b96f9186c3 31
lhakpa 0:40b96f9186c3 32 return status;
lhakpa 0:40b96f9186c3 33 }
lhakpa 0:40b96f9186c3 34
lhakpa 0:40b96f9186c3 35 void modbus_init(uint8_t id)
lhakpa 0:40b96f9186c3 36 {
lhakpa 0:40b96f9186c3 37 nodeId = id;
lhakpa 0:40b96f9186c3 38 }
lhakpa 0:40b96f9186c3 39
lhakpa 0:40b96f9186c3 40 uint16_t modbus_read(uint16_t offset)
lhakpa 0:40b96f9186c3 41 {
lhakpa 0:40b96f9186c3 42 if (offset < ADDR_RANGE) {
lhakpa 0:40b96f9186c3 43 return regValue[offset];
lhakpa 0:40b96f9186c3 44 }
lhakpa 0:40b96f9186c3 45 return 0xFFFF;
lhakpa 0:40b96f9186c3 46 }
lhakpa 0:40b96f9186c3 47
lhakpa 0:40b96f9186c3 48 uint16_t modbus_update(uint8_t offset, uint16_t val)
lhakpa 0:40b96f9186c3 49 {
lhakpa 0:40b96f9186c3 50 uint16_t tmp;
lhakpa 0:40b96f9186c3 51
lhakpa 0:40b96f9186c3 52 if (offset < ADDR_RANGE) {
lhakpa 0:40b96f9186c3 53 tmp = regValue[offset];
lhakpa 0:40b96f9186c3 54 regValue[offset] = val;
lhakpa 0:40b96f9186c3 55 return tmp;
lhakpa 0:40b96f9186c3 56 }
lhakpa 0:40b96f9186c3 57 return 0xFFFF;
lhakpa 0:40b96f9186c3 58 }
lhakpa 0:40b96f9186c3 59
lhakpa 0:40b96f9186c3 60 int modbus_parser(char ch, uint8_t *frame)
lhakpa 0:40b96f9186c3 61 {
lhakpa 0:40b96f9186c3 62 static char buf[514];
lhakpa 0:40b96f9186c3 63 static int idx = 0;
lhakpa 0:40b96f9186c3 64 static int status = 0;
lhakpa 0:40b96f9186c3 65
lhakpa 0:40b96f9186c3 66 switch(protState) {
lhakpa 0:40b96f9186c3 67 case IDLE:
lhakpa 0:40b96f9186c3 68 if (ch == ':') {
lhakpa 0:40b96f9186c3 69 protState = RECEPTION;
lhakpa 0:40b96f9186c3 70 idx = 0;
lhakpa 0:40b96f9186c3 71 status = 0;
lhakpa 0:40b96f9186c3 72 }
lhakpa 0:40b96f9186c3 73 break;
lhakpa 0:40b96f9186c3 74 case RECEPTION:
lhakpa 0:40b96f9186c3 75 if ((ch >= '0') && (ch <= '9')) {
lhakpa 0:40b96f9186c3 76 buf[idx++] = ch;
lhakpa 0:40b96f9186c3 77 } else if ((ch >= 'a') && (ch <= 'f')) {
lhakpa 0:40b96f9186c3 78 buf[idx++] = ch;
lhakpa 0:40b96f9186c3 79 } else if ((ch >= 'A') && (ch <= 'F')) {
lhakpa 0:40b96f9186c3 80 buf[idx++] = ch;
lhakpa 0:40b96f9186c3 81 } else if (ch == '\r') {
lhakpa 0:40b96f9186c3 82 buf[idx] = 0;
lhakpa 0:40b96f9186c3 83 protState = END;
lhakpa 0:40b96f9186c3 84 } else {
lhakpa 0:40b96f9186c3 85 protState = IDLE;
lhakpa 0:40b96f9186c3 86 }
lhakpa 0:40b96f9186c3 87 break;
lhakpa 0:40b96f9186c3 88 case END:
lhakpa 0:40b96f9186c3 89 if (ch == '\n') {
lhakpa 0:40b96f9186c3 90 if (process_buffer(buf, frame)) {
lhakpa 0:40b96f9186c3 91 if ((frame[0] == nodeId) && (frame[1] == FUNC_CODE)) {
lhakpa 0:40b96f9186c3 92 status = 1;
lhakpa 0:40b96f9186c3 93 }
lhakpa 0:40b96f9186c3 94 }
lhakpa 0:40b96f9186c3 95 }
lhakpa 0:40b96f9186c3 96 protState = IDLE;
lhakpa 0:40b96f9186c3 97 break;
lhakpa 0:40b96f9186c3 98 default:
lhakpa 0:40b96f9186c3 99 protState = IDLE;
lhakpa 0:40b96f9186c3 100 }
lhakpa 0:40b96f9186c3 101
lhakpa 0:40b96f9186c3 102 return status;
lhakpa 0:40b96f9186c3 103 }