Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot mbed-rtos mbed
Fork of power_meter by
Diff: main.cpp
- Revision:
- 4:49e311f9671b
- Parent:
- 3:47839ac143d3
diff -r 47839ac143d3 -r 49e311f9671b main.cpp --- a/main.cpp Mon Dec 12 14:47:28 2016 +0000 +++ b/main.cpp Wed Feb 15 16:38:10 2017 +0000 @@ -1,59 +1,103 @@ #include "mbed.h" #include "DataLoggerRS232.h" - -Serial pc (USBTX, USBRX); // tx, rx -Serial dataLogger (PA_2,PA_3); // tx, rx +#include "mbed.h" +#include "mDot.h" +#include "MTSLog.h" +#include <string> +#include <vector> +#include <algorithm> +#include <math.h> + +//define baudrate +#define PC_BAUDRATE 115200 +#define CARD_MACHINE_BAUDRATE 19200 + +Serial pc (USBTX, USBRX); // tx, rx +Serial cm_rs485 (PA_2,PA_3); // tx, rx + +#define COMMAND_LENGTH 7 +#define RESPONSE_LENGTH 16 + +//#define RINGBUFFER_SIZE 100 + +//unsigned int inIndex = 0; +//unsigned int outIndex = 0; + +//char ringBuffer[RINGBUFFER_SIZE][RESPONSE_LENGTH] = {0}; //char DLcommand; to be translated into hex for reading of total power -int abc[]={104,87,36,19,0,0,0,104,17,4,51,51,51,51,63,22}; +char readCmd[COMMAND_LENGTH] = {0x09,0x41,0x31,0x46,0x33,0x46,0x0d}; +char readBuf[RESPONSE_LENGTH] = {0}; + +int main() +{ + char chr, chr0, chr1, chr2; + int i; + mDot* dot; + + // get a mDot handle + //dot = mDot::getInstance(); -char DLcommand2; -int main() { + // print library version information + //logInfo("version: %s", dot->getId().c_str()); + + //******************************************* + // configuration + //******************************************* + // reset to default config so we know what state we're in + //dot->resetConfig(); + + //set baudrate + //dot->setBaud(BAUDRATE); + //dot->setLogLevel(mts::MTSLog::INFO_LEVEL); + // set up the mDot with our network information: frequency sub band, network name, and network password + // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() - pc.baud(115200); - pc.printf("PC and Datalogger serial set up complete !!\n\r"); - dataLogger.baud(2400); - dataLogger.format(9,SerialBase::Even,1); - pc.printf("Here !!\n\r"); - int i; - const int m=16; + //logInfo("Baudrate is: %d", dot->getBaud()); + // 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 + //logInfo("setting frequency sub band"); + + pc.baud(PC_BAUDRATE); + pc.printf("PC COM RS232 baudrate: %d \n\r", PC_BAUDRATE); + cm_rs485.baud(CARD_MACHINE_BAUDRATE); + cm_rs485.format(9,SerialBase::Even,1); + //cm_rs485.attach(&txIsr, SerialBase::TxIrq); + //cm_rs485.attach(&rxIsr, SerialBase::RxIrq); + pc.printf("Card Machine RS485 baudrate: %d!\n\r", CARD_MACHINE_BAUDRATE); + while(1) { + + /* clear the read buffer */ + for(i=0;i<RESPONSE_LENGTH;i++){ + readBuf[i]=0; + } + + 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]); + } + } + i=0; -// if(pc.readable()) { - if(dataLogger.writeable()){ - for(i=0;i<m;i++){ - dataLogger.putc(abc[i]); - } - pc.printf("T"); - // dataLogger.printf("%c",DLcommand); -// dataLogger.count = 0; -// DLcommand = pc.getc(); -// pc.printf("\n\r%c\n\r",DLcommand); - -// dataLogger.putc(DLcommand); -// dataLogger.get_ECU_databyte(); + + chr = cm_rs485.getc(); + while(chr != 0x0d) + { + readBuf[i] = chr; + chr = cm_rs485.getc(); + i++; } -// dataLogger.display_ECU_databyte(); -// wait(4); -//----------------------------------------------- - if(dataLogger.readable()) { - pc.printf("R"); - char DLcommand[25]; -// dataLogger.count = 0; -for(i=0;i<25;i++){ - DLcommand[i]= dataLogger.getc(); - pc.putc(DLcommand[i]); - } - // DLcommand = dataLogger.getc(); -// DLcommand2 = dataLogger.getc(); - - ; -// pc.printf("\n\r%c\n\r",DLcommand2); - // dataLogger.putc(DLcommand); -// dataLogger.get_ECU_databyte(); - } -// dataLogger.display_ECU_databyte(); - wait(4); - - }} + pc.printf("Response[ASCII]:"); + for(i=0; i< RESPONSE_LENGTH; i++){ + pc.printf("%0X ", readBuf[i]); + if (readBuf[i] == 0) break; + } + pc.printf("Response[Text]:%s\n\r", &readBuf[3]); + + wait(1); + } +}