Hello All,
I am attempting to use Energia (I know it's not Arduino but they are pretty much the same) to read data from a serial device that has some interesting quirks. The first issue I'm having is when using the debug code (debug = 1) the program only captures a portion of the serial data from the external device.
#define ACK 0x06 #define NACK 0x15 #define CSFAIL 0x51 #define debug 1 #define debugState 1 #define debugStateThree 0 byte requestData[] = {0xA5, 0x08, 0x41, 0x00, 0x3E, 0x4E, 0x20, 0x9A}; int timeSlot = 200; unsigned long time0 = 0; unsigned long time1 = 0; int receiveCounter = 0; int bytesToReceive = -1; char reveivedData[50]; //Input array boolean stringComplete = false; // whether the string is complete int outputCounter = 0; int i = 0; int state = 0; char inChar; int availableBytes = 0; void setup() { // initialize both serial ports: Serial.begin(115200); Serial1.begin(115200); // reserve 200 bytes for the inputString: // inputString.reserve(200); // prints title with ending line break Serial.println("Read Data from MCP39F511"); } void loop() { //send read data command to MCP39F511 time1 = millis(); switch(state){ case 0: if((time1 - time0) >= 1000){ for(i = 0; i < sizeof(requestData); i++){ Serial1.write(requestData[i]); delayMicroseconds(timeSlot); } time0 = millis(); #if debugState Serial.println("0 Send Command"); #endif state++; } break; case 1: availableBytes = Serial1.available(); if(availableBytes > 0){ inChar = (char)Serial1.read(); if(inChar == ACK){ reveivedData[receiveCounter] = inChar; receiveCounter++; state++; #if debug Serial.print("1 Receive first byte"); Serial.print(" - inChar "); Serial.print(inChar, HEX); Serial.print(" - receiveCounter "); Serial.print(receiveCounter, HEX); Serial.print(" - state "); Serial.print(state, DEC); Serial.print(" - availableBytes "); Serial.println(availableBytes, DEC); #endif } else if(inChar == NACK){ //Output error message Serial.println("Command received with success command not understood or another error in command bytes"); } else if(inChar == CSFAIL){ //Output error message Serial.println("Frame received with success, however, checksum did not match bytes in frame"); } } #if debugState Serial.println("State #1"); #endif break; case 2: availableBytes = Serial1.available(); if(availableBytes > 0){ inChar = (char)Serial1.read(); reveivedData[receiveCounter] = inChar; bytesToReceive = inChar; receiveCounter++; state++; #if debug Serial.print("2 Receive number of bytes to receive"); Serial.print(" - inChar "); Serial.print(inChar, HEX); Serial.print(" - receiveCounter "); Serial.print(receiveCounter, HEX); Serial.print(" - bytesToReceive "); Serial.print(bytesToReceive, DEC); Serial.print(" - state "); Serial.print(state, DEC); Serial.print(" - availableBytes "); Serial.println(availableBytes, DEC); #endif } #if debugState Serial.println("State #2"); #endif break; case 3: availableBytes = Serial1.available(); if(availableBytes > 0){ inChar = (char)Serial1.read(); reveivedData[receiveCounter] = inChar; receiveCounter++; if(receiveCounter == bytesToReceive){ state++; #if debugState Serial.println("State #3 -> #4"); #endif } #if debug Serial.print("3 Receive remaining bytes"); Serial.print(" - inChar "); Serial.print(inChar, HEX); Serial.print(" - receiveCounter "); Serial.print(receiveCounter, HEX); Serial.print(" - bytesToReceive "); Serial.print(bytesToReceive, DEC); Serial.print(" - state "); Serial.print(state, DEC); Serial.print(" - availableBytes "); Serial.println(availableBytes, DEC); #endif } #if debugStateThree Serial.println("State #3"); #endif break; case 4: for(outputCounter = 0; outputCounter < receiveCounter; outputCounter++){ Serial.print(reveivedData[outputCounter], HEX); Serial.print(" - outputCounter "); Serial.print(outputCounter, DEC); Serial.print(" - receiveCounter "); Serial.println(receiveCounter, HEX); } state = 0; #if debugState Serial.println("State #4"); #endif break; // default : // break; } }
The device returns 35 bytes but the MSP430 device only sees 16 of them.
When I turn off debug (debug = 0) but still output the states (except for state 3) the output looks a lot more acceptable but only runs once. It starts to receive the second set of data but stops at state 3.
Once debugStateThree is enabled things really stop working. If left this ends up being an infinite loop.
The data format from the MCP39F511 is a bit odd in that the baud rate is set to 115200 yet the data cannot be sent continuously at that clock speed without breaks, notice the time delay in the send portion of the code, delayMicroseconds(timeSlot); without this the MCP39F511 returns a NACK. Also the returned data is returned in blocks of two bytes.
Data sent to the MCP39F511
Data received from the MCP39F511
I did attempt to use SerialEvent but that did not seem to run correctly or how I was hoping it would. If anyone can give me some pointers as to what I may be doing wrong and why the code appears to run erratically I would very much appreciate the help.
Thankfully
Kas