en 129
/
RasPiUsartTest
usart test
main.cpp@0:8848a3fcb99c, 2014-06-30 (annotated)
- Committer:
- nameless129
- Date:
- Mon Jun 30 02:05:10 2014 +0000
- Revision:
- 0:8848a3fcb99c
rasPiUsatTest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nameless129 | 0:8848a3fcb99c | 1 | #pragma import __use_all_ctype |
nameless129 | 0:8848a3fcb99c | 2 | |
nameless129 | 0:8848a3fcb99c | 3 | #include "mbed.h" |
nameless129 | 0:8848a3fcb99c | 4 | #include <stdlib.h> |
nameless129 | 0:8848a3fcb99c | 5 | |
nameless129 | 0:8848a3fcb99c | 6 | Serial pc(USBTX, USBRX); |
nameless129 | 0:8848a3fcb99c | 7 | Serial pi(p9, p10); // tx, rx |
nameless129 | 0:8848a3fcb99c | 8 | |
nameless129 | 0:8848a3fcb99c | 9 | bool f_rcev = 0; |
nameless129 | 0:8848a3fcb99c | 10 | unsigned int c_rcev = 0; |
nameless129 | 0:8848a3fcb99c | 11 | char uartBuff[1000]; |
nameless129 | 0:8848a3fcb99c | 12 | char uartRcev[1000]; |
nameless129 | 0:8848a3fcb99c | 13 | |
nameless129 | 0:8848a3fcb99c | 14 | void callback() { |
nameless129 | 0:8848a3fcb99c | 15 | char buf = 0; |
nameless129 | 0:8848a3fcb99c | 16 | |
nameless129 | 0:8848a3fcb99c | 17 | buf = pi.getc(); |
nameless129 | 0:8848a3fcb99c | 18 | uartBuff[c_rcev] = buf; |
nameless129 | 0:8848a3fcb99c | 19 | if(uartBuff[c_rcev] == '\r' ) |
nameless129 | 0:8848a3fcb99c | 20 | { |
nameless129 | 0:8848a3fcb99c | 21 | uartBuff[c_rcev+1] = 0; |
nameless129 | 0:8848a3fcb99c | 22 | memcpy(uartRcev,uartBuff,sizeof(uartRcev) ); |
nameless129 | 0:8848a3fcb99c | 23 | memset(uartBuff,0,sizeof(uartBuff)); |
nameless129 | 0:8848a3fcb99c | 24 | c_rcev = 0; |
nameless129 | 0:8848a3fcb99c | 25 | f_rcev = 1; |
nameless129 | 0:8848a3fcb99c | 26 | } |
nameless129 | 0:8848a3fcb99c | 27 | else |
nameless129 | 0:8848a3fcb99c | 28 | { |
nameless129 | 0:8848a3fcb99c | 29 | c_rcev++; |
nameless129 | 0:8848a3fcb99c | 30 | } |
nameless129 | 0:8848a3fcb99c | 31 | } |
nameless129 | 0:8848a3fcb99c | 32 | |
nameless129 | 0:8848a3fcb99c | 33 | int main(void) { |
nameless129 | 0:8848a3fcb99c | 34 | |
nameless129 | 0:8848a3fcb99c | 35 | pi.baud(115200); |
nameless129 | 0:8848a3fcb99c | 36 | pc.baud(115200); |
nameless129 | 0:8848a3fcb99c | 37 | pi.attach(&callback); |
nameless129 | 0:8848a3fcb99c | 38 | |
nameless129 | 0:8848a3fcb99c | 39 | while (1) |
nameless129 | 0:8848a3fcb99c | 40 | { |
nameless129 | 0:8848a3fcb99c | 41 | if(f_rcev == 1) |
nameless129 | 0:8848a3fcb99c | 42 | { |
nameless129 | 0:8848a3fcb99c | 43 | // printf("\033[2J"); |
nameless129 | 0:8848a3fcb99c | 44 | // printf("\033[1;1H"); |
nameless129 | 0:8848a3fcb99c | 45 | // printf("%s\r\n",uartRcev); |
nameless129 | 0:8848a3fcb99c | 46 | puts(uartRcev); |
nameless129 | 0:8848a3fcb99c | 47 | f_rcev = 0; |
nameless129 | 0:8848a3fcb99c | 48 | } |
nameless129 | 0:8848a3fcb99c | 49 | } |
nameless129 | 0:8848a3fcb99c | 50 | } |