Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2012
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.95 KB | None | 0 0
  1. #include <SerialPort.h>
  2. #include <PString.h>
  3.  
  4.  
  5.  
  6.  
  7. SerialPort<2,64,64> port2;
  8. SerialPort<3,128,128> port3;
  9. //Serial.write chokes on a zero for some reason
  10. const uint8_t hexZero = 0x00;
  11.  
  12. char inByte;
  13. //byte syncBuffer[2];
  14.  
  15. char parseBuffer[50];
  16. char outBuffer[200];
  17.  
  18.  
  19.  
  20. PString parseString(parseBuffer,50);
  21. PString outString(outBuffer,200);
  22.  
  23. boolean USBReady = false;
  24. boolean fileOpen = false;
  25. boolean logOK = true;
  26. boolean fileClosed = false;
  27. boolean fileWritten = false;
  28. uint16_t timeOutCounter = 0;
  29. uint8_t openCounter = 0;
  30. uint8_t readyCounter = 0;
  31. uint8_t responseNumber = 0;
  32.  
  33. union{
  34.   unsigned long outBytes;
  35.   byte outBytesHex[4];
  36. }
  37. outBytesUnion;
  38.  
  39. float fakeData = 54.23;
  40. unsigned long timeStampStart = 0;
  41. unsigned long timeStampEnd = 0;
  42.  
  43. int i;
  44. int j;
  45.  
  46. void setup(){
  47.   delay(5000);//wait for the logger to boot
  48.   //the logger can run at 3Mbaud but I was only able to interface at 2Mbaud
  49.   port3.begin(2000000);
  50.   //switch to short commands
  51.   port2.begin(9600);
  52.   port2.write(0x10);
  53.   port2.write(0x0D);
  54.   port2.flush();
  55.   //change baud rate to 2Mbaud
  56.   port2.write(0x14);
  57.   port2.write(0x20);
  58.   port2.write(0x01);
  59.   port2.write(hexZero);
  60.   //port2.write(0x00);
  61.   //sketch_jun13b:13: error: call of overloaded 'write(int)' is ambiguous ???
  62.   port2.write(hexZero);
  63.   port2.write(0x0D);
  64.   port2.flush();
  65.   port2.end();
  66.   //wait then restart the serial port
  67.   delay(500);
  68.   port2.begin(2000000);
  69.   port2.write(0x0D);
  70.   delay(5);
  71.   port2.flushRx();
  72.  
  73. }
  74.  
  75. void loop(){
  76.   /*while (port3.available() > 0){
  77.    inByte = port3.read();
  78.    port2.write(inByte);
  79.    }
  80.    while (port2.available() > 0){
  81.    inByte = port2.read();
  82.    port3.write(inByte);
  83.    }*/
  84.  
  85.   /*USBReady = ReadyCheck();
  86.    port3.println("\r\nmain loop");
  87.    //port3.println(true);
  88.    //port3.println(false);
  89.    port3.println(USBReady);
  90.    port3.println("end of main loop");*/
  91.   //delay(1000);
  92.   port3.println("pause 0***********************************************");
  93.   pause();
  94.  
  95.   USBReady = ReadyCheck();
  96.  
  97.   if (USBReady == true){
  98.     port3.println("device responded ready");    
  99.   }
  100.   else{
  101.     port3.println("no response");
  102.   }
  103.  
  104.   port3.println("pause 1***********************************************");
  105.   pause();
  106.   fileOpen = OpenFile();
  107.   if (fileOpen == true){
  108.     port3.println("the file is opened");
  109.  
  110.   }
  111.   else{
  112.     port3.println("the file is not opened");
  113.   }
  114.  
  115.   port3.println("pause 2***********************************************");
  116.    pause();
  117.    timeStampStart = millis();
  118.    fileWritten = WriteFile();
  119.    timeStampEnd = millis();
  120.    if (fileWritten == true){
  121.    port3.println("the file was written to");
  122.    }
  123.    else{
  124.    port3.println("the write was not successful");
  125.    }
  126.  
  127.   port3.println("pause 3***********************************************");
  128.   pause();
  129.   fileClosed = CloseFile();
  130.   if (fileClosed == true){
  131.     port3.println("the file is closed");
  132.   }
  133.   else{
  134.     port3.println("the flie is not closed");
  135.   }
  136.  
  137.  
  138. }
  139.  
  140. void pause(){
  141.   while(digitalRead(6)==0){
  142.   }//wait for the toggle
  143.   delay(1000);//debounce
  144. }
  145.  
  146. boolean WriteFile(){
  147.   /*readyCounter = 0;
  148.   port3.println("entering ready check from write");
  149.   while(ReadyCheck() != true){
  150.     readyCounter++;
  151.     if (readyCounter == 50){
  152.       logOK = false;
  153.       return false;
  154.     }
  155.   }  */
  156.  
  157.   outString.begin();
  158.   outString+=timeStampStart;
  159.   outString+=",";
  160.   outString+=fakeData;
  161.   outString+=",";
  162.   outString+=fakeData;
  163.   outString+=",";
  164.   outString+=fakeData;
  165.   outString+=",";
  166.   outString+=fakeData;
  167.   outString+=",";
  168.   outString+=fakeData;
  169.   outString+=",";
  170.   outString+=fakeData;
  171.   outString+=",";
  172.   outString+=fakeData;
  173.   outString+=",";
  174.   outString+=fakeData;
  175.   outString+=",";
  176.   outString+=fakeData;
  177.   outString+=",";
  178.   outString+=timeStampEnd;
  179.   outString+="\r\n";
  180.   outBytesUnion.outBytes = outString.length();
  181.   //port3.println("string length");
  182.   //port3.println(outBytesUnion.outBytes);
  183.  
  184.   //port3.println("pre write avaialabe bytes");
  185.   //j=port2.available();
  186.   //port3.println(j);
  187.   /*for(i=0;i<j;i++){
  188.    inByte = port2.read();
  189.    port3.print("byte number: ");
  190.    port3.println(i);
  191.    port3.write(inByte);
  192.    port3.println("");
  193.    }
  194.    port3.println("");*/
  195.  
  196.   port2.write(0x65);
  197.   port2.write(0x0D);
  198.   port2.write(0x08);
  199.   port2.write(0x20);
  200.   port2.write(outBytesUnion.outBytesHex[3]);
  201.   port2.write(outBytesUnion.outBytesHex[2]);
  202.   port2.write(outBytesUnion.outBytesHex[1]);
  203.   port2.write(outBytesUnion.outBytesHex[0]);
  204.   port2.write(0x0D);
  205.   //port2.write(outString);
  206.   //port3.println("writing data ");
  207.   for(i=0;i<outBytesUnion.outBytes;i++){
  208.  
  209.     //port3.println(i);
  210.     port2.write(outBuffer[i]);
  211.     delayMicroseconds(50);
  212.  
  213.   }
  214.   //delay(100);
  215.   port2.write(0x45);
  216.   port2.write(0x0D);
  217.   //port2.write(0x0D);  
  218.  
  219.   //port3.println("post write avaialabe bytes");
  220.   //port3.println(port2.available());
  221.   //port3.println("freeze point1");
  222.  
  223.   /*timeOutCounter = 0;
  224.   while(port2.available() <2){
  225.     timeOutCounter ++;
  226.     //port3.println(timeOutCounter);
  227.     if (timeOutCounter == 300){
  228.       port3.println("timeout 1");
  229.       logOK = false;
  230.       return false;
  231.     }
  232.   }
  233.   //port3.println("freeze point2");
  234.  
  235.   //port3.println(timeOutCounter);
  236.   timeOutCounter = 0;
  237.   //port3.println("initial read for 0x65");
  238.   inByte = port2.read();
  239.   //port3.write(inByte);
  240.   //port3.println("");
  241.  
  242.   timeOutCounter = 0;
  243.   while (inByte != 0x65){
  244.     //port3.println("looking for 0x65");
  245.     inByte = port2.read();
  246.     //port3.write(inByte);
  247.     timeOutCounter ++;
  248.     if (timeOutCounter == 500){
  249.       port3.println("timeout 2");
  250.       logOK = false;
  251.       return false;
  252.     }
  253.   }
  254.   //port3.println("freeze point3");
  255.   //port3.println("timeout counter ");
  256.   //port3.println(timeOutCounter);
  257.  
  258.   //port3.println("looking for 0x45");
  259.   timeOutCounter = 0;
  260.   parseString.begin();
  261.   //port3.println("freeze point1");
  262.   while(inByte != 0x45){
  263.     //port3.println("freeze point2");
  264.     while(port2.available() <1){
  265.       // port3.println(timeOutCounter);
  266.       // port3.println("freeze point3");
  267.  
  268.       timeOutCounter++;
  269.       if (timeOutCounter == 5000){
  270.         port3.println("timeout 3");
  271.         logOK = false;
  272.         return false;
  273.       }
  274.     }
  275.     // port3.println("freeze point4");
  276.     port3.println("time out 3");
  277.     port3.println(timeOutCounter);
  278.     timeOutCounter = 0;
  279.     //port3.println("buffering response");
  280.     inByte = port2.read();
  281.     //port3.println("freeze point5");
  282.     //port3.write(inByte);
  283.     if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
  284.       //port3.write(inByte);
  285.       parseString += inByte;
  286.     }
  287.     //port3.println("freeze point6");
  288.     //port3.println("current input buffer");
  289.     //port3.println(parseString);
  290.   }
  291.   //port3.println("freeze point7");
  292.   port2.read();//read the final 0x0D from the buffer
  293.   //port3.write(inByte);
  294.  
  295.   responseNumber = ParseResponse();
  296.   //port3.println("parse buffer write");
  297.   //port3.println(parseBuffer);
  298.  
  299.   if (responseNumber == 0){
  300.     port3.println("exit from write file num ava:");
  301.     port3.println(port2.available());
  302.     return true;
  303.   }
  304.   else{
  305.     return false;
  306.   }  */
  307.  
  308.  
  309.  
  310. }
  311. boolean CloseFile(){
  312.  
  313.   readyCounter = 0;
  314.   port3.println("entering ready check from close");
  315.   while(ReadyCheck() != true){
  316.     readyCounter++;
  317.     if (readyCounter == 50){
  318.       logOK = false;
  319.       return false;
  320.     }
  321.   }  
  322.  
  323.   port2.write(0x65);
  324.   port2.write(0x0D);
  325.   port2.write(0x0A);
  326.   port2.write(0x20);
  327.   port2.write("LOG.CSV");
  328.   port2.write(0x0D);
  329.   port2.write(0x45);
  330.   port2.write(0x0D);
  331.  
  332.   //port3.println("command to close file given");
  333.   timeOutCounter = 0;
  334.   while(port2.available() <2){
  335.     timeOutCounter ++;
  336.     //port3.println(timeOutCounter);
  337.     if (timeOutCounter == 3000){
  338.       port3.println("timeout 1");
  339.       logOK = false;
  340.       return false;
  341.     }
  342.   }
  343.   port3.println(timeOutCounter);
  344.  
  345.   //port3.println(timeOutCounter);
  346.   timeOutCounter = 0;
  347.   //port3.println("initial read for 0x65");
  348.   inByte = port2.read();
  349.   //port3.write(inByte);
  350.  
  351.   while (inByte != 0x65){
  352.     //port3.println("looking for 0x65");
  353.     inByte = port2.read();
  354.     //port3.write(inByte);
  355.     timeOutCounter ++;
  356.     if (timeOutCounter == 500){
  357.       port3.println("timeout 2");
  358.       logOK = false;
  359.       return false;
  360.     }
  361.   }
  362.   port3.println(timeOutCounter);
  363.  
  364.   timeOutCounter = 0;
  365.   parseString.begin();
  366.   while(inByte != 0x45){
  367.     while(port2.available() <1){
  368.       timeOutCounter++;
  369.       if (timeOutCounter==100000){
  370.         port3.println("timeout 3");
  371.         logOK = false;
  372.         return false;
  373.       }
  374.     }
  375.     port3.println("timeout 3");
  376.     port3.println(timeOutCounter);
  377.     timeOutCounter = 0;
  378.     //port3.println("buffering response");
  379.     inByte = port2.read();
  380.     //port3.write(inByte);
  381.     if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
  382.       //port3.write(inByte);
  383.       parseString += inByte;
  384.     }
  385.     //port3.println("current input buffer");
  386.     //port3.println(parseString);
  387.   }
  388.   port2.read();//read the final 0x0D from the buffer
  389.   //port3.write(inByte);
  390.  
  391.   responseNumber = ParseResponse();
  392.   //port3.println(parseBuffer);
  393.  
  394.   if (responseNumber == 0){
  395.     port3.println("close file exit num ava:");
  396.     port3.println(port2.available());
  397.     return true;
  398.   }
  399.   else{
  400.     return false;
  401.   }
  402.  
  403.   //if (responseNumber == 8){
  404.   // return true;
  405.   // }  
  406.  
  407. }
  408.  
  409. boolean OpenFile(){
  410.  
  411.   port3.println("start of open file");
  412.   readyCounter = 0;
  413.   port3.println("entering ready check from open");
  414.   while(ReadyCheck() != true){
  415.     readyCounter++;
  416.     port3.println("ready counter");
  417.     port3.println(readyCounter);
  418.     if (readyCounter == 50){
  419.       logOK = false;
  420.       return false;
  421.     }
  422.  
  423.   }
  424.  
  425.   //port2.flushTx();
  426.   //port2.flushRx();
  427.  
  428.   port2.write(0x65);
  429.   port2.write(0x0D);
  430.   port2.write(0x09);
  431.   port2.write(0x20);
  432.   port2.write("LOG.CSV");
  433.   port2.write(0x0D);
  434.   port2.write(0x45);
  435.   port2.write(0x0D);
  436.  
  437.   port3.println("command to open file given");
  438.   timeOutCounter = 0;
  439.   while(port2.available() <2){
  440.     timeOutCounter ++;
  441.     //port3.println(timeOutCounter);
  442.     if (timeOutCounter == 3000){
  443.       port3.println(timeOutCounter);
  444.       port3.println("timed out");
  445.       logOK = false;
  446.       return false;
  447.     }
  448.   }
  449.   port3.println(timeOutCounter);
  450.   timeOutCounter = 0;
  451.   port3.println("initial read for 0x65");
  452.   inByte = port2.read();
  453.   //port3.write(inByte);
  454.  
  455.   while (inByte != 0x65){
  456.     port3.println("looking for 0x65");
  457.     inByte = port2.read();
  458.     //port3.write(inByte);
  459.     timeOutCounter ++;
  460.     if (timeOutCounter == 500){
  461.       logOK = false;
  462.       return false;
  463.     }
  464.   }
  465.  
  466.  
  467.  
  468.   timeOutCounter = 0;
  469.   parseString.begin();
  470.   while(inByte != 0x45){
  471.     while(port2.available() <1){
  472.       timeOutCounter++;
  473.       if (timeOutCounter == 10000){
  474.         port3.println(timeOutCounter);
  475.         port3.println("timed out");
  476.         logOK = false;
  477.         return false;
  478.       }
  479.     }
  480.     port3.println(timeOutCounter);
  481.     timeOutCounter=0;
  482.     port3.println("buffering response");
  483.     inByte = port2.read();
  484.     //port3.write(inByte);
  485.     if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
  486.       //port3.write(inByte);
  487.       parseString += inByte;
  488.     }
  489.     //port3.println("current input buffer");
  490.     //port3.println(parseString);
  491.   }
  492.   port2.read();//read the final 0x0D from the buffer
  493.   //port3.write(inByte);
  494.   port3.println("parse string 1");
  495.   port3.println(parseBuffer);
  496.  
  497.   responseNumber = ParseResponse();
  498.   if (responseNumber == 8){
  499.     port3.println("open file exit FO \r\n Num of byte ava:");
  500.     port3.println(port2.available());
  501.     return true;
  502.   }
  503.  
  504.   //port3.println("response number:");
  505.   //port3.println(parseBuffer);
  506.   //port3.println(responseNumber);
  507.   /*if (responseNumber == 0){
  508.    return true;
  509.    
  510.    }else{
  511.    return false;
  512.    }*/
  513.  
  514.   if (responseNumber == 0){//recieved a prompt - check if file is there
  515.     port2.write(0x65);
  516.     port2.write(0x0D);
  517.     port2.write(0x01);
  518.     port2.write(0x0D);
  519.     port2.write(0x45);
  520.     port2.write(0x0D);
  521.     port3.println("directory check");
  522.     timeOutCounter = 0;
  523.     while(port2.available() <6){
  524.       timeOutCounter ++;
  525.       //port3.println(timeOutCounter);
  526.       if (timeOutCounter == 5000){
  527.         port3.println("timeout");
  528.         logOK = false;
  529.         return false;
  530.       }
  531.     }
  532.     port3.println(timeOutCounter);
  533.     //port3.println("timerout counter 1");
  534.     //port3.println(timeOutCounter);
  535.     timeOutCounter = 0;
  536.     inByte = port2.read();
  537.     //port3.write(inByte);
  538.     while (inByte != 0x65){
  539.       port3.println("0x65 check");
  540.       inByte = port2.read();
  541.       //port3.write(inByte);
  542.       timeOutCounter ++;
  543.       //port3.println(timeOutCounter);
  544.       if (timeOutCounter == 150){
  545.         logOK = false;
  546.         return false;
  547.       }
  548.     }
  549.     //port3.println("timerout counter 2");
  550.     //port3.println(timeOutCounter);
  551.     timeOutCounter = 0;
  552.     parseString.begin();
  553.     //verified up to this point
  554.     port3.println("entering buffering dir");
  555.     while(inByte != 0x45){
  556.       //port3.println("buffering DIR");
  557.       while (port2.available() <1){
  558.         timeOutCounter++;
  559.         if (timeOutCounter == 1000){
  560.           port3.println(timeOutCounter);
  561.           port3.println("timed out");
  562.           logOK = false;
  563.           return false;
  564.         }
  565.       }
  566.       timeOutCounter = 0;
  567.       inByte = port2.read();
  568.       //port3.write(inByte);
  569.       if (inByte != 0x0D && inByte != 0x65 && inByte != 0x45){
  570.         parseString += inByte;
  571.       }
  572.     }
  573.  
  574.     port2.read();//read the final 0x0D
  575.     //port3.write(inByte);
  576.     responseNumber = ParseResponse();
  577.     if(responseNumber != 2){
  578.       return false;//change to recursive with counter
  579.     }
  580.     else{
  581.       port3.println("open file exit DIR \r\n Num of byte ava:");
  582.       port3.println(port2.available());
  583.       return true;
  584.     }
  585.   }
  586.   else{
  587.     return false;
  588.   }
  589.  
  590.  
  591.  
  592. }
  593.  
  594. uint8_t ParseResponse(){
  595.   //port3.println("strncmp");
  596.   //port3.println(parseBuffer);
  597.   //port3.println(strncmp(parseBuffer,"F0",2));
  598.   //port3.println(strncmp(parseBuffer,"FO",2));
  599.   //port3.println(strncmp(parseBuffer,"f0",2));
  600.   //port3.println(strncmp(parseBuffer,"fO",2));
  601.  
  602.   if (strncmp(parseBuffer,">",1) == 0){
  603.     //port3.println("strncmp2");
  604.     //port3.println(parseBuffer);
  605.     //port3.println(strncmp(parseBuffer,">",1));
  606.     return 0;
  607.   }
  608.  
  609.   if (strncmp(parseBuffer,"BC",2) == 0){
  610.     return 1;
  611.   }
  612.  
  613.   if (strncmp(parseBuffer,"LOG.CSV>",8) == 0){
  614.     return 2;
  615.   }
  616.  
  617.   if (strncmp(parseBuffer,"ND",2) == 0){
  618.     return 3;
  619.   }
  620.  
  621.   if (strncmp(parseBuffer,"CF",2) == 0){
  622.     return 4;
  623.   }
  624.  
  625.   if (strncmp(parseBuffer,"DF",2) == 0){
  626.     return 5;
  627.   }
  628.  
  629.   if (strncmp(parseBuffer,"FI",2) == 0){
  630.     return 6;
  631.   }
  632.  
  633.   if (strncmp(parseBuffer,"RO",2) == 0){
  634.     return 7;
  635.   }
  636.  
  637.   if (strncmp(parseBuffer,"FO",2) == 0){
  638.     return 8;
  639.   }
  640.  
  641.   if (strncmp(parseBuffer,"FN",2) == 0){
  642.     return 9;
  643.   }
  644.  
  645.   return 10;//not reckognized response
  646.   //possibly DR2ND DD2NU> BCBC etc etc
  647.   //these should not occur
  648.  
  649. }
  650.  
  651. boolean ReadyCheck(){
  652.  
  653.   port2.flushRx();
  654.  
  655.   port2.write(0x65);
  656.   port2.write(0x0D);
  657.  
  658.   timeOutCounter = 0;
  659.   while(port2.available() <2){
  660.     //port3.println("in wait loop");
  661.     timeOutCounter ++;
  662.     if (timeOutCounter == 1000){
  663.       port3.println("returning from timeout");
  664.       logOK = false;
  665.       return false;
  666.     }
  667.   }
  668.  
  669.   //check to see if the first response is going to be BC - consider for removal later
  670.   if (inByte == 0x42){
  671.     port2.flushRx();
  672.     port2.write(0x65);
  673.     port2.write(0x0D);
  674.     inByte = port2.read();
  675.   }
  676.  
  677.   timeOutCounter = 0;
  678.   while (inByte != 0x65){
  679.     //port3.println("in the loop");
  680.     inByte = port2.read();
  681.     //port3.write(inByte);
  682.     timeOutCounter ++;
  683.     if (timeOutCounter == 50){
  684.       logOK = false;
  685.       return false;
  686.     }
  687.   }
  688.  
  689.   port2.flushRx();
  690.  
  691.   //port3.println(port2.available());
  692.   return true;
  693.  
  694. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement