
TAIST class assignment
Fork of TAIST_modbus by
Revision 1:d2e8a3c822f1, committed 2018-03-14
- Comitter:
- Benjaphan
- Date:
- Wed Mar 14 14:55:52 2018 +0000
- Parent:
- 0:f306cb0263a6
- Commit message:
- Ms. Benjaphan Sommana StudentID: 6022040445; Mr. Thotsawut Mhudchuay StudentID:6014552855; Mr. Pongporamat Charuchinda ID:6014552863; Ms. Nabeela Shaheen, ID:6014553142
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 |
diff -r f306cb0263a6 -r d2e8a3c822f1 main.cpp --- a/main.cpp Wed Mar 14 06:17:48 2018 +0000 +++ b/main.cpp Wed Mar 14 14:55:52 2018 +0000 @@ -1,29 +1,64 @@ #include "mbed.h" #include "modbus.h" +#include <math.h> +#include <stdio.h> +Ticker t; DigitalOut myled(LED1); +InterruptIn mbtn(USER_BUTTON); RawSerial pc(USBTX, USBRX); +int a; +int b,c,d; +int casep = 0; +int16_t nub = 0; +int16_t time1; +int16_t time2; +void count() +{ nub = nub +1; +} +void press() { + if((nub-c)>2){ + d = nub; + time2 = d-c; + a = nub; + } +} +void release() { + if((nub-b)>2){ + b = nub; + time1 = b-a; + c = nub ; + } +} void serial_callback() { uint8_t frame[6]; - char ch = pc.getc(); if (modbus_parser(ch, frame)) { - + switch(frame[3]){ + case 0: casep = 1; break; //Please enter :000300000003FA\r\n to show pushing time + case 1: casep = 2; break; //Please enter :000300010003F9\r\n to show releasing time + case 2: casep = 3; break; //Please enter :000300020003F8\r\n to show both pushing and releasing time + + } } } int main() { - // setup code - pc.attach(serial_callback); - // 1. button code - // 2. timer code + mbtn.fall(&press); + mbtn.rise(&release); + + t.attach(&count, 0.1); while(1) { - // loop code - myled = 1; // LED is ON - wait(0.2); // 200 ms - myled = 0; // LED is OFF - wait(1.0); // 1 sec + pc.attach(serial_callback); + switch(casep){ + case 0: break; + case 1: pc.printf("The releasing time taken was %.1f seconds\n\n",time2/10.0); break; + case 2: pc.printf("The pushing time taken was %.1f seconds\n\n",time1/10.0); break; + case 3: pc.printf("The pushing time taken was %.1f seconds\nThe release time taken was %.1f seconds\n\n",time1/10.0,time2/10.0); break; + default: break; + } + casep = 0; } -} +} \ No newline at end of file
diff -r f306cb0263a6 -r d2e8a3c822f1 modbus.cpp --- a/modbus.cpp Wed Mar 14 06:17:48 2018 +0000 +++ b/modbus.cpp Wed Mar 14 14:55:52 2018 +0000 @@ -19,16 +19,16 @@ tmpbuf[1] = buf[i*2 + 1]; frame[i] = strtoul(tmpbuf, NULL, 16); } + //printf("frame[0] is %d, frame[1] is %d", frame[0], frame[1]); 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) { + if ((sum + lrc) == 256) { status = 1; } } - return status; } @@ -86,9 +86,12 @@ } break; case END: - if (ch == '\n') { - if (process_buffer(buf, frame)) { - if ((frame[0] == nodeId) && (frame[1] == FUNC_CODE)) { + if (ch == '\n') + { + if (process_buffer(buf, frame)) + { + if ((frame[0] == nodeId) && (frame[1] == FUNC_CODE)) + { status = 1; } } @@ -100,4 +103,4 @@ } return status; -} +} \ No newline at end of file