Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SerialPort.h>
- #include <PString.h>
- SerialPort<2,64,64> port2;
- SerialPort<3,128,128> port3;
- //Serial.write chokes on a zero for some reason
- const uint8_t hexZero = 0x00;
- char inByte;
- //byte syncBuffer[2];
- char parseBuffer[50];
- char outBuffer[200];
- PString parseString(parseBuffer,50);
- PString outString(outBuffer,200);
- boolean USBReady = false;
- boolean fileOpen = false;
- boolean logOK = true;
- boolean fileClosed = false;
- boolean fileWritten = false;
- uint16_t timeOutCounter = 0;
- uint8_t openCounter = 0;
- uint8_t readyCounter = 0;
- uint8_t responseNumber = 0;
- union{
- unsigned long outBytes;
- byte outBytesHex[4];
- }
- outBytesUnion;
- float fakeData = 54.23;
- unsigned long timeStampStart = 0;
- unsigned long timeStampEnd = 0;
- int i;
- int j;
- void setup(){
- delay(5000);//wait for the logger to boot
- //the logger can run at 3Mbaud but I was only able to interface at 2Mbaud
- port3.begin(2000000);
- //switch to short commands
- port2.begin(9600);
- port2.write(0x10);
- port2.write(0x0D);
- port2.flush();
- //change baud rate to 2Mbaud
- port2.write(0x14);
- port2.write(0x20);
- port2.write(0x01);
- port2.write(hexZero);
- //port2.write(0x00);
- //sketch_jun13b:13: error: call of overloaded 'write(int)' is ambiguous ???
- port2.write(hexZero);
- port2.write(0x0D);
- port2.flush();
- port2.end();
- //wait then restart the serial port
- delay(500);
- port2.begin(2000000);
- port2.write(0x0D);
- delay(5);
- port2.flushRx();
- }
- void loop(){
- /*while (port3.available() > 0){
- inByte = port3.read();
- port2.write(inByte);
- }
- while (port2.available() > 0){
- inByte = port2.read();
- port3.write(inByte);
- }*/
- /*USBReady = ReadyCheck();
- port3.println("\r\nmain loop");
- //port3.println(true);
- //port3.println(false);
- port3.println(USBReady);
- port3.println("end of main loop");*/
- //delay(1000);
- port3.println("pause 0***********************************************");
- pause();
- USBReady = ReadyCheck();
- if (USBReady == true){
- port3.println("device responded ready");
- }
- else{
- port3.println("no response");
- }
- port3.println("pause 1***********************************************");
- pause();
- fileOpen = OpenFile();
- if (fileOpen == true){
- port3.println("the file is opened");
- }
- else{
- port3.println("the file is not opened");
- }
- port3.println("pause 2***********************************************");
- pause();
- timeStampStart = millis();
- fileWritten = WriteFile();
- timeStampEnd = millis();
- if (fileWritten == true){
- port3.println("the file was written to");
- }
- else{
- port3.println("the write was not successful");
- }
- port3.println("pause 3***********************************************");
- pause();
- fileClosed = CloseFile();
- if (fileClosed == true){
- port3.println("the file is closed");
- }
- else{
- port3.println("the flie is not closed");
- }
- }
- void pause(){
- while(digitalRead(6)==0){
- }//wait for the toggle
- delay(1000);//debounce
- }
- boolean WriteFile(){
- /*readyCounter = 0;
- port3.println("entering ready check from write");
- while(ReadyCheck() != true){
- readyCounter++;
- if (readyCounter == 50){
- logOK = false;
- return false;
- }
- } */
- outString.begin();
- outString+=timeStampStart;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=fakeData;
- outString+=",";
- outString+=timeStampEnd;
- outString+="\r\n";
- outBytesUnion.outBytes = outString.length();
- //port3.println("string length");
- //port3.println(outBytesUnion.outBytes);
- //port3.println("pre write avaialabe bytes");
- //j=port2.available();
- //port3.println(j);
- /*for(i=0;i<j;i++){
- inByte = port2.read();
- port3.print("byte number: ");
- port3.println(i);
- port3.write(inByte);
- port3.println("");
- }
- port3.println("");*/
- port2.write(0x65);
- port2.write(0x0D);
- port2.write(0x08);
- port2.write(0x20);
- port2.write(outBytesUnion.outBytesHex[3]);
- port2.write(outBytesUnion.outBytesHex[2]);
- port2.write(outBytesUnion.outBytesHex[1]);
- port2.write(outBytesUnion.outBytesHex[0]);
- port2.write(0x0D);
- //port2.write(outString);
- //port3.println("writing data ");
- for(i=0;i<outBytesUnion.outBytes;i++){
- //port3.println(i);
- port2.write(outBuffer[i]);
- delayMicroseconds(50);
- }
- //delay(100);
- port2.write(0x45);
- port2.write(0x0D);
- //port2.write(0x0D);
- //port3.println("post write avaialabe bytes");
- //port3.println(port2.available());
- //port3.println("freeze point1");
- /*timeOutCounter = 0;
- while(port2.available() <2){
- timeOutCounter ++;
- //port3.println(timeOutCounter);
- if (timeOutCounter == 300){
- port3.println("timeout 1");
- logOK = false;
- return false;
- }
- }
- //port3.println("freeze point2");
- //port3.println(timeOutCounter);
- timeOutCounter = 0;
- //port3.println("initial read for 0x65");
- inByte = port2.read();
- //port3.write(inByte);
- //port3.println("");
- timeOutCounter = 0;
- while (inByte != 0x65){
- //port3.println("looking for 0x65");
- inByte = port2.read();
- //port3.write(inByte);
- timeOutCounter ++;
- if (timeOutCounter == 500){
- port3.println("timeout 2");
- logOK = false;
- return false;
- }
- }
- //port3.println("freeze point3");
- //port3.println("timeout counter ");
- //port3.println(timeOutCounter);
- //port3.println("looking for 0x45");
- timeOutCounter = 0;
- parseString.begin();
- //port3.println("freeze point1");
- while(inByte != 0x45){
- //port3.println("freeze point2");
- while(port2.available() <1){
- // port3.println(timeOutCounter);
- // port3.println("freeze point3");
- timeOutCounter++;
- if (timeOutCounter == 5000){
- port3.println("timeout 3");
- logOK = false;
- return false;
- }
- }
- // port3.println("freeze point4");
- port3.println("time out 3");
- port3.println(timeOutCounter);
- timeOutCounter = 0;
- //port3.println("buffering response");
- inByte = port2.read();
- //port3.println("freeze point5");
- //port3.write(inByte);
- if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
- //port3.write(inByte);
- parseString += inByte;
- }
- //port3.println("freeze point6");
- //port3.println("current input buffer");
- //port3.println(parseString);
- }
- //port3.println("freeze point7");
- port2.read();//read the final 0x0D from the buffer
- //port3.write(inByte);
- responseNumber = ParseResponse();
- //port3.println("parse buffer write");
- //port3.println(parseBuffer);
- if (responseNumber == 0){
- port3.println("exit from write file num ava:");
- port3.println(port2.available());
- return true;
- }
- else{
- return false;
- } */
- }
- boolean CloseFile(){
- readyCounter = 0;
- port3.println("entering ready check from close");
- while(ReadyCheck() != true){
- readyCounter++;
- if (readyCounter == 50){
- logOK = false;
- return false;
- }
- }
- port2.write(0x65);
- port2.write(0x0D);
- port2.write(0x0A);
- port2.write(0x20);
- port2.write("LOG.CSV");
- port2.write(0x0D);
- port2.write(0x45);
- port2.write(0x0D);
- //port3.println("command to close file given");
- timeOutCounter = 0;
- while(port2.available() <2){
- timeOutCounter ++;
- //port3.println(timeOutCounter);
- if (timeOutCounter == 3000){
- port3.println("timeout 1");
- logOK = false;
- return false;
- }
- }
- port3.println(timeOutCounter);
- //port3.println(timeOutCounter);
- timeOutCounter = 0;
- //port3.println("initial read for 0x65");
- inByte = port2.read();
- //port3.write(inByte);
- while (inByte != 0x65){
- //port3.println("looking for 0x65");
- inByte = port2.read();
- //port3.write(inByte);
- timeOutCounter ++;
- if (timeOutCounter == 500){
- port3.println("timeout 2");
- logOK = false;
- return false;
- }
- }
- port3.println(timeOutCounter);
- timeOutCounter = 0;
- parseString.begin();
- while(inByte != 0x45){
- while(port2.available() <1){
- timeOutCounter++;
- if (timeOutCounter==100000){
- port3.println("timeout 3");
- logOK = false;
- return false;
- }
- }
- port3.println("timeout 3");
- port3.println(timeOutCounter);
- timeOutCounter = 0;
- //port3.println("buffering response");
- inByte = port2.read();
- //port3.write(inByte);
- if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
- //port3.write(inByte);
- parseString += inByte;
- }
- //port3.println("current input buffer");
- //port3.println(parseString);
- }
- port2.read();//read the final 0x0D from the buffer
- //port3.write(inByte);
- responseNumber = ParseResponse();
- //port3.println(parseBuffer);
- if (responseNumber == 0){
- port3.println("close file exit num ava:");
- port3.println(port2.available());
- return true;
- }
- else{
- return false;
- }
- //if (responseNumber == 8){
- // return true;
- // }
- }
- boolean OpenFile(){
- port3.println("start of open file");
- readyCounter = 0;
- port3.println("entering ready check from open");
- while(ReadyCheck() != true){
- readyCounter++;
- port3.println("ready counter");
- port3.println(readyCounter);
- if (readyCounter == 50){
- logOK = false;
- return false;
- }
- }
- //port2.flushTx();
- //port2.flushRx();
- port2.write(0x65);
- port2.write(0x0D);
- port2.write(0x09);
- port2.write(0x20);
- port2.write("LOG.CSV");
- port2.write(0x0D);
- port2.write(0x45);
- port2.write(0x0D);
- port3.println("command to open file given");
- timeOutCounter = 0;
- while(port2.available() <2){
- timeOutCounter ++;
- //port3.println(timeOutCounter);
- if (timeOutCounter == 3000){
- port3.println(timeOutCounter);
- port3.println("timed out");
- logOK = false;
- return false;
- }
- }
- port3.println(timeOutCounter);
- timeOutCounter = 0;
- port3.println("initial read for 0x65");
- inByte = port2.read();
- //port3.write(inByte);
- while (inByte != 0x65){
- port3.println("looking for 0x65");
- inByte = port2.read();
- //port3.write(inByte);
- timeOutCounter ++;
- if (timeOutCounter == 500){
- logOK = false;
- return false;
- }
- }
- timeOutCounter = 0;
- parseString.begin();
- while(inByte != 0x45){
- while(port2.available() <1){
- timeOutCounter++;
- if (timeOutCounter == 10000){
- port3.println(timeOutCounter);
- port3.println("timed out");
- logOK = false;
- return false;
- }
- }
- port3.println(timeOutCounter);
- timeOutCounter=0;
- port3.println("buffering response");
- inByte = port2.read();
- //port3.write(inByte);
- if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
- //port3.write(inByte);
- parseString += inByte;
- }
- //port3.println("current input buffer");
- //port3.println(parseString);
- }
- port2.read();//read the final 0x0D from the buffer
- //port3.write(inByte);
- port3.println("parse string 1");
- port3.println(parseBuffer);
- responseNumber = ParseResponse();
- if (responseNumber == 8){
- port3.println("open file exit FO \r\n Num of byte ava:");
- port3.println(port2.available());
- return true;
- }
- //port3.println("response number:");
- //port3.println(parseBuffer);
- //port3.println(responseNumber);
- /*if (responseNumber == 0){
- return true;
- }else{
- return false;
- }*/
- if (responseNumber == 0){//recieved a prompt - check if file is there
- port2.write(0x65);
- port2.write(0x0D);
- port2.write(0x01);
- port2.write(0x0D);
- port2.write(0x45);
- port2.write(0x0D);
- port3.println("directory check");
- timeOutCounter = 0;
- while(port2.available() <6){
- timeOutCounter ++;
- //port3.println(timeOutCounter);
- if (timeOutCounter == 5000){
- port3.println("timeout");
- logOK = false;
- return false;
- }
- }
- port3.println(timeOutCounter);
- //port3.println("timerout counter 1");
- //port3.println(timeOutCounter);
- timeOutCounter = 0;
- inByte = port2.read();
- //port3.write(inByte);
- while (inByte != 0x65){
- port3.println("0x65 check");
- inByte = port2.read();
- //port3.write(inByte);
- timeOutCounter ++;
- //port3.println(timeOutCounter);
- if (timeOutCounter == 150){
- logOK = false;
- return false;
- }
- }
- //port3.println("timerout counter 2");
- //port3.println(timeOutCounter);
- timeOutCounter = 0;
- parseString.begin();
- //verified up to this point
- port3.println("entering buffering dir");
- while(inByte != 0x45){
- //port3.println("buffering DIR");
- while (port2.available() <1){
- timeOutCounter++;
- if (timeOutCounter == 1000){
- port3.println(timeOutCounter);
- port3.println("timed out");
- logOK = false;
- return false;
- }
- }
- timeOutCounter = 0;
- inByte = port2.read();
- //port3.write(inByte);
- if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
- parseString += inByte;
- }
- }
- port2.read();//read the final 0x0D
- //port3.write(inByte);
- responseNumber = ParseResponse();
- if(responseNumber != 2){
- return false;//change to recursive with counter
- }
- else{
- port3.println("open file exit DIR \r\n Num of byte ava:");
- port3.println(port2.available());
- return true;
- }
- }
- else{
- return false;
- }
- }
- uint8_t ParseResponse(){
- //port3.println("strncmp");
- //port3.println(parseBuffer);
- //port3.println(strncmp(parseBuffer,"F0",2));
- //port3.println(strncmp(parseBuffer,"FO",2));
- //port3.println(strncmp(parseBuffer,"f0",2));
- //port3.println(strncmp(parseBuffer,"fO",2));
- if (strncmp(parseBuffer,">",1) == 0){
- //port3.println("strncmp2");
- //port3.println(parseBuffer);
- //port3.println(strncmp(parseBuffer,">",1));
- return 0;
- }
- if (strncmp(parseBuffer,"BC",2) == 0){
- return 1;
- }
- if (strncmp(parseBuffer,"LOG.CSV>",8) == 0){
- return 2;
- }
- if (strncmp(parseBuffer,"ND",2) == 0){
- return 3;
- }
- if (strncmp(parseBuffer,"CF",2) == 0){
- return 4;
- }
- if (strncmp(parseBuffer,"DF",2) == 0){
- return 5;
- }
- if (strncmp(parseBuffer,"FI",2) == 0){
- return 6;
- }
- if (strncmp(parseBuffer,"RO",2) == 0){
- return 7;
- }
- if (strncmp(parseBuffer,"FO",2) == 0){
- return 8;
- }
- if (strncmp(parseBuffer,"FN",2) == 0){
- return 9;
- }
- return 10;//not reckognized response
- //possibly DR2ND DD2NU> BCBC etc etc
- //these should not occur
- }
- boolean ReadyCheck(){
- port2.flushRx();
- port2.write(0x65);
- port2.write(0x0D);
- timeOutCounter = 0;
- while(port2.available() <2){
- //port3.println("in wait loop");
- timeOutCounter ++;
- if (timeOutCounter == 1000){
- port3.println("returning from timeout");
- logOK = false;
- return false;
- }
- }
- //check to see if the first response is going to be BC - consider for removal later
- if (inByte == 0x42){
- port2.flushRx();
- port2.write(0x65);
- port2.write(0x0D);
- inByte = port2.read();
- }
- timeOutCounter = 0;
- while (inByte != 0x65){
- //port3.println("in the loop");
- inByte = port2.read();
- //port3.write(inByte);
- timeOutCounter ++;
- if (timeOutCounter == 50){
- logOK = false;
- return false;
- }
- }
- port2.flushRx();
- //port3.println(port2.available());
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement