lhakpa dorji
/
TAIST_modbus_Assignment
Modbus serial assignment
Fork of TAIST_modbus by
Revision 1:b50d9b988d1e, committed 2018-03-14
- Comitter:
- lhakpa
- Date:
- Wed Mar 14 14:55:36 2018 +0000
- Parent:
- 0:f306cb0263a6
- Commit message:
- modbus assignment
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
modbus.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Mar 14 06:17:48 2018 +0000 +++ b/main.cpp Wed Mar 14 14:55:36 2018 +0000 @@ -1,29 +1,77 @@ #include "mbed.h" #include "modbus.h" - +//DigitalIn button1(USER_BUTTON); +InterruptIn mybtn(USER_BUTTON); DigitalOut myled(LED1); RawSerial pc(USBTX, USBRX); +Timer t1,t2,Timer1; +uint8_t id= 40001; +uint16_t offset=1; +uint16_t press=3,release=4,temp=0; +bool pushed = false; void serial_callback() -{ +{printf("serial call back\n"); uint8_t frame[6]; + char ch = pc.getc(); + printf("ch = %c \n ",ch); - char ch = pc.getc(); - if (modbus_parser(ch, frame)) { - + modbus_init(id); + modbus_read(offset); + + temp=modbus_update( offset, press); + + printf("Temp= %f\n",temp); + if (modbus_parser(ch, frame)) { + printf("Modbus Parser\n"); } } + +void ledON() +{ + myled = 1; //LED ON + wait(0.2); // 200 ms + t1.start(); + t2.stop(); + if(t2.read()<0.1) + { printf("Bouncing\n");} + else + {printf("Button released for %.1f seconds.\r\n", t2.read()); + release=t2.read(); + t2.reset(); + } +} + +void ledOFF() +{ + myled = 0; //LED OFF + wait(0.2); // 200 ms + t1.stop(); + t2.start(); + if(t1.read()<0.1) + { printf("Bouncing\n");} + else + { + printf("Button push for %.1f seconds.\r\n", t1.read()); + press=t1.read(); + t1.reset(); + } +} + + + + int main() { // setup code pc.attach(serial_callback); + // 1. button code + mybtn.fall(&ledON); // Interrupt occur + mybtn.rise(&ledOFF); // 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 + } -} +} \ No newline at end of file
--- a/modbus.cpp Wed Mar 14 06:17:48 2018 +0000 +++ b/modbus.cpp Wed Mar 14 14:55:36 2018 +0000 @@ -7,7 +7,7 @@ enum state {IDLE, RECEPTION, END} protState = IDLE; int process_buffer(char *buf, uint8_t *frame) -{ +{printf("Modbus buffer\n"); int status = 0; uint8_t sum = 0; uint8_t lrc, i; @@ -33,12 +33,12 @@ } void modbus_init(uint8_t id) -{ +{printf("Modbus Initial ID\n"); nodeId = id; } uint16_t modbus_read(uint16_t offset) -{ +{ printf("Modbus Read\n"); if (offset < ADDR_RANGE) { return regValue[offset]; } @@ -48,7 +48,7 @@ uint16_t modbus_update(uint8_t offset, uint16_t val) { uint16_t tmp; - + printf("Modbus Update\n"); if (offset < ADDR_RANGE) { tmp = regValue[offset]; regValue[offset] = val; @@ -69,18 +69,23 @@ protState = RECEPTION; idx = 0; status = 0; + printf("IDLE case \n"); } break; case RECEPTION: + printf("Reception case \n"); if ((ch >= '0') && (ch <= '9')) { - buf[idx++] = ch; + buf[idx++] = ch; + printf("character length0-9\n"); } else if ((ch >= 'a') && (ch <= 'f')) { buf[idx++] = ch; } else if ((ch >= 'A') && (ch <= 'F')) { buf[idx++] = ch; } else if (ch == '\r') { + printf("type \r END \n"); buf[idx] = 0; protState = END; + } else { protState = IDLE; } @@ -90,6 +95,7 @@ if (process_buffer(buf, frame)) { if ((frame[0] == nodeId) && (frame[1] == FUNC_CODE)) { status = 1; + printf("END\n"); } } } @@ -100,4 +106,4 @@ } return status; -} +} \ No newline at end of file