test

Dependencies:   mbed Motordriver SRF05

main.cpp

Committer:
LALALALALALALALA
Date:
2016-12-08
Revision:
2:ff7c5f9ba765
Parent:
0:6d65c70ef417
Child:
3:6484736ff515

File content as of revision 2:ff7c5f9ba765:

#include "mbed.h"
#include <string>
#include <cstring>
Serial bt(p28,p27);//tx,rx
Serial TAG(p9,p10);//tx,rx
Serial pc(USBTX,USBRX);
using namespace std;

void read_line();
void Rx_interrupt();
// Circular buffers for serial TX and RX data - used by interrupt routines
const int buffer_size = 255;
// might need to increase buffer size for high baud rates
char rx_buffer[buffer_size+1];
volatile int rx_in=0;
volatile int rx_out=0;
char rx_line[80];

string tagStr1, tagStr2, tagStr3;
bool flag1=false, flag2=false, flag3=false;
string str;

//std::string.
int main() {
    bt.baud(38400);
    pc.baud(9600);
    TAG.baud(115200);
    pc.printf("pc hello\n\r");
    bt.printf("bt hello\n\r");
    
    TAG.attach(&Rx_interrupt, Serial::RxIrq);
     
     
    while(1) {
   
    
        // Read a line from the large rx buffer from rx interrupt routine
        read_line();
        // Read ASCII number from rx line buffer
        //sscanf(rx_line,"%d",&rx_i);
        //pc.printf("string = %s\n\r",rx_line);
        // 若收到藍牙模組的資料,則送到「序列埠監控視窗」
        //pc.printf("tag thread.");
        switch(rx_line[1]){
        case '0':
            flag1=true;
            tagStr1=&rx_line[2];
            pc.printf("string1 = %s\n\r",tagStr1);
            break;
        case '1':
            flag2=true;
            tagStr2=&rx_line[2];
            pc.printf("string2 = %s\n\r",tagStr2);
            break;
        case '2':
            flag3=true;
            tagStr3=&rx_line[2];
            pc.printf("string3 = %s\n\r",tagStr3);
            break;
        }
        if(flag3==true&&flag2==true&&flag1==true){
            flag1=false,flag2=false,flag3=false;
            str=tagStr1+","+tagStr2+","+tagStr3;
            bt.printf("%s\n",str);
        }
        
    }
}
// Read a line from the large rx buffer from rx interrupt routine
void read_line() {
    int i;
    i = 0;
// Start Critical Section - don't interrupt while changing global buffer variables
    NVIC_DisableIRQ(UART1_IRQn);
// Loop reading rx buffer characters until end of line character
    while ((i==0) || (rx_line[i-1] != '\r')) {
// Wait if buffer empty
        if (rx_in == rx_out) {
// End Critical Section - need to allow rx interrupt to get new characters for buffer
            NVIC_EnableIRQ(UART1_IRQn);
            while (rx_in == rx_out) {
            }
// Start Critical Section - don't interrupt while changing global buffer variables
            NVIC_DisableIRQ(UART1_IRQn);
        }
        rx_line[i] = rx_buffer[rx_out];
        i++;
        rx_out = (rx_out + 1) % buffer_size;
    }
// End Critical Section
    NVIC_EnableIRQ(UART1_IRQn);
    rx_line[i-1] = 0;
    return;
}

// Interupt Routine to read in data from serial port
void Rx_interrupt() {
// Loop just in case more than one character is in UART's receive FIFO buffer
// Stop if buffer full
    while ((TAG.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
        rx_buffer[rx_in] = TAG.getc();
// Uncomment to Echo to USB serial to watch data flow
//        monitor_TAG.putc(rx_buffer[rx_in]);
        rx_in = (rx_in + 1) % buffer_size;
    }
    return;
}