fist release of rssi test
Dependencies: libmDot mbed-rtos mbed
Fork of CardRead_20170216 by
Revision 18:ee1ec5f500bf, committed 2017-05-08
- Comitter:
- wang1tao
- Date:
- Mon May 08 13:47:22 2017 +0000
- Parent:
- 17:b60549bc8626
- Commit message:
- first release
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r b60549bc8626 -r ee1ec5f500bf main.cpp --- a/main.cpp Thu Feb 16 17:34:37 2017 +0000 +++ b/main.cpp Mon May 08 13:47:22 2017 +0000 @@ -1,5 +1,4 @@ #include "mbed.h" -//#include "DataLoggerRS232.h" #include "mbed.h" #include "mDot.h" #include "MTSLog.h" @@ -12,29 +11,8 @@ #define RETURN_ERR -1 //define baudrate #define PC_BAUDRATE 115200 -#define CARD_MACHINE_BAUDRATE 19200 - -#define COMMAND_LENGTH 7 -#define RESPONSE_LENGTH 16 -#define RINGBUFFER_SIZE 100 - -#define READ_PERIOD 1 -#define SEND_PERIOD 5 - -const char readCmd[COMMAND_LENGTH] = {0x09,0x41,0x31,0x46,0x33,0x46,0x0d}; - -char readBuf[RESPONSE_LENGTH] = {0}; -char ringBuffer[RINGBUFFER_SIZE][RESPONSE_LENGTH] = {0}; - -unsigned int inIndex = 0; -unsigned int outIndex = 0; /* functions prototype */ -void readCardID(void); -void sendCardID(void); - -void my_strcpy(char *dst, char *src); -void my_strclear(char *str, unsigned int len); int configNetwork(void); int joinNetwork(void); @@ -42,7 +20,6 @@ /* Global variables */ Serial pc (USBTX, USBRX); // tx, rx -Serial cm_rs485 (PA_2,PA_3); // tx, rx mDot* dot; @@ -52,7 +29,8 @@ int main() { - int32_t ret; + int32_t ret, counter; + char dataBuf[15]; pc.baud(PC_BAUDRATE); pc.printf("\n\r\n\r\n\r"); @@ -60,9 +38,6 @@ pc.printf("SiFOX remote card read system!\n\r"); pc.printf("============================================\n\r"); pc.printf("PC COM RS232 baudrate: %d \n\r", PC_BAUDRATE); - cm_rs485.baud(CARD_MACHINE_BAUDRATE); - cm_rs485.format(9,SerialBase::Even,1); - pc.printf("Card Machine RS485 baudrate: %d!\n\r", CARD_MACHINE_BAUDRATE); //******************************************* // Configurate Network @@ -78,18 +53,15 @@ if(ret != RETURN_OK) dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart - pc.printf("Wait 15S or card machine booting...\n\r"); - wait(15); - + counter = 0; while(1) { - /* Read Card ID */ - readCardID(); - - /* Send Card ID to gateway */ - sendCardID(); - + sprintf(dataBuf, "Send:%d", counter); + /* Send data to gateway */ + send_data(dataBuf); /* wait a while */ - wait(1); + wait(10); + + counter++; } } @@ -117,6 +89,16 @@ // frequency sub band is only applicable in the 915 (US) frequency band // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels + //pc.printf("Setting TX frequency band as 868MHz\n\r"); + //if ((ret = dot->setTxFrequency(mDot::FB_868)) != mDot::MDOT_OK){ + // pc.printf("Error:failed to set TX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); + // return RETURN_ERR; + //} + //pc.printf("Setting RX frequency band as 868MHz\n\r"); + //if ((ret = dot->setRxFrequency(mDot::FB_868)) != mDot::MDOT_OK){ + // pc.printf("Error:failed to set RX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); + // return RETURN_ERR; + //} pc.printf("Setting frequency sub band\n\r"); if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); @@ -165,7 +147,7 @@ { int32_t ret,i; std::vector<uint8_t> sendData; - char _header[] = "ChongQi!"; + char _header[] = "Reset!"; // attempt to join the network pc.printf("Joining network...\n\r"); @@ -218,127 +200,3 @@ } -void my_strcpy(char *dst, char *src) -{ - int j=0; - - while(src[j] != 0) - { - dst[j] = src[j]; - j++; - } -} - - -void my_strclear(char *str, unsigned int len) -{ - unsigned int j; - for(j=0; j<len; j++)str[j]=0; -} - -unsigned int get_F_position(char *str) -{ - unsigned int i=0; - while(str[i] != 'F') - { - i++; - if( i >6)break; - } - return i; -} - - -void readCardID(void) -{ - unsigned int i, p; - char chr; - - /* clear the read buffer */ - my_strclear(readBuf, RESPONSE_LENGTH); - - pc.printf("Send the READ command!\n\r");//, &readCmd[1]); - if(cm_rs485.writeable()){ - for(i=0; i<COMMAND_LENGTH; i++){ - cm_rs485.putc(readCmd[i]); - } - } - - /* read the response */ - chr = cm_rs485.getc(); - i = 0; - while(chr != 0x0d) - { - readBuf[i] = chr; - i++; - chr = cm_rs485.getc(); - } - - /* print the response for debug */ - pc.printf("Response[ASCII]:"); - - for(i=0; i< RESPONSE_LENGTH; i++){ - pc.printf("%0X ", readBuf[i]); - if (readBuf[i] == 0) break; - } - - p = get_F_position(readBuf); - pc.printf("P=%d\n\r", p); - pc.printf("Response[Text]:%s\n\r", &readBuf[p+1]); - /* add to ringBuffer if valid */ - if((p != 0) && (readBuf[p+1] != '3') && (readBuf[p+2] !='C')) //valid response - { - my_strcpy(ringBuffer[inIndex], &readBuf[p+1]); //strim the 3 bytes prehead - //pc.printf("addr = 0x%0X, inIndex = %d\n\r", &ringBuffer[inIndex],inIndex); - inIndex++; - if(inIndex >= RINGBUFFER_SIZE) inIndex = 0; - if(ringBuffer[inIndex][0] != 0) //is not empty - { - pc.printf("Warning! Ringbuffer overflow at [%d]!\n\r", inIndex); - pc.printf("Please have a check of the network!\n\r"); - pc.printf("Data in ring buffer:\n\r"); - for(i=0; i<RINGBUFFER_SIZE; i++) - { - pc.printf("Data[%d]: %s \n\r", i, ringBuffer[i]); - } - - wait(10); //in seconds - } - } -} - -void sendCardID(void) -{ - int32_t ret; - - //pc.printf("Send Tick!\n\r"); - /*send the data */ - if(ringBuffer[outIndex][0] != 0) //not empty - { - pc.printf("Send dada[%s] to Gateway!\n\r", ringBuffer[outIndex]); - #ifndef NO_MULTITECH_GATEWAY - /* t.b.d : send data though LoRA */ - if(send_data(ringBuffer[outIndex]) != RETURN_OK) - { - pc.printf("Failed to send data to netwotk!\n\r"); - pc.printf("Rejoin network and try one more time!\n\r"); - // attempt to rejoin the network - pc.printf("Attemp to rejoin network....\n\r"); - if ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) { - pc.printf("Failed to rejoin network! %d:%s \n\r", ret, mDot::getReturnCodeString(ret).c_str()); - return; - }else{ - pc.printf("Rejoin network successfully!"); - if(send_data(ringBuffer[outIndex]) != RETURN_OK) return; //abort to send - } - } - #endif - //clear the buffer - my_strclear(ringBuffer[outIndex], RESPONSE_LENGTH); - outIndex++; - if(outIndex >= RINGBUFFER_SIZE)outIndex = 0; - }else{ //empty - pc.printf("Nothing to send!\n\r"); - //wait(10); - } - -} \ No newline at end of file