element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Serial not receiving all available bytes or state machine hangs
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 11 replies
  • Subscribers 392 subscribers
  • Views 2103 views
  • Users 0 members are here
  • serial
  • msp430
  • serial.read
Related

Serial not receiving all available bytes or state machine hangs

kas.lewis
kas.lewis over 9 years ago

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.

image

 

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.

 

image

 

Once debugStateThree is enabled things really stop working. If left this ends up being an infinite loop.

 

image

 

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.

image

Data sent to the MCP39F511

image

 

Data received from the MCP39F511

image

 

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

  • Sign in to reply
  • Cancel

Top Replies

  • kas.lewis
    kas.lewis over 9 years ago in reply to kas.lewis +2
    The answer I found in my last post. Thanks to Robert Peter Oakes suggestion of trying less than 16 bytes I was able to see that the receiveCounter value a bit clearer. If you look in the last picture in…
Parents
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago

    I'm with shabazon this one.

     

    I was just helping a friend with a very similar issue but in this case it was the I2C library and it had a buffer limit of only 32 bytes (In two places)

     

    In order to preserve resources, it is often the case that the libraries will have fixed size buffers to allow as much ram as possible for your application

     

    If your not using much of the ram in your code, you can change the library buffer sizes and see if this fixes the problem or as shabaz stated, break up the message into 16byte chunks.

     

    either way this is most certainly a buffer issue,

     

    Regarding the break up of the data into small packets. this will be due to the hardware uart being able to take one or two bytes (Depends on the hardware used) but then the handler to give it the next one or two characters is several software levels up from it so it takes time to process and get back down to the hardware. This is especially try of environments like Arduino etc that are based on Wiring where there is a lot of abstraction between your application and the real hardware,

     

    to improve this you would have to code at a much lower level and talk direct to the hardware uarts with your code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kas.lewis
    0 kas.lewis over 9 years ago in reply to Robert Peter Oakes

    Hello Robert Peter Oakes & shabaz,

     

    Thank you for the reply, there are two things that make me think it's may not be a buffer issue. Firstly I only have an issue when the debug is enabled between lines 145 and 159. If the only output is "state #3" there is a critical issue where the system goes into an infinite loop. But, if I enable other debugs and not "State #3" I get less of an issue. The other reason I dont belive it to be a buffer issue is the comment for Serial.available():

     

    available()

    Description

    Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 128 bytes). available() inherits from the Stream utility class.

    It can be seen here that the buffer size is set to 128 bytes and since I am only receiving 35 there should be no issue. That being said if you can point me in the direction on how to change the buffer size to see if I am wrong (and I would love to be now so I can get this working already) I would be more then willing to give it a try. As for decreasing the read size to 16 bytes I would really not like to do that if at all possible

     

    Thanks

    Kas

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago in reply to kas.lewis

    I just looked in hardwareserial.h of the arduino library and found this image

    #if !defined(SERIAL_TX_BUFFER_SIZE)
    #if ((RAMEND - RAMSTART) < 1023)
    #define SERIAL_TX_BUFFER_SIZE 16
    #else
    #define SERIAL_TX_BUFFER_SIZE 64
    #endif
    #endif
    #if !defined(SERIAL_RX_BUFFER_SIZE)
    #if ((RAMEND - RAMSTART) < 1023)
    #define SERIAL_RX_BUFFER_SIZE 16
    #else
    #define SERIAL_RX_BUFFER_SIZE 64
    #endif
    #endif
    #if (SERIAL_TX_BUFFER_SIZE>256)
    typedef uint16_t tx_buffer_index_t;
    #else
    typedef uint8_t tx_buffer_index_t;
    #endif
    #if  (SERIAL_RX_BUFFER_SIZE>256)
    typedef uint16_t rx_buffer_index_t;
    #else
    typedef uint8_t rx_buffer_index_t;
    #endif

     

    This clearly shows that the buffer size depends on the ram size. and can be as low as 16 characters

     

    You dont specifically say what MSP430 your using so I don't know the actual RAM so cant be sure what this is evaluating to in your case

     

    looking into the Energia equivalent I fould this  ( C:\Program Files (x86)\energia\hardware\msp430\cores\msp430\HardwareSerial.cpp )

     

    #define SERIAL_BUFFER_SIZE 16

     

    Case Closed perhaps ?????

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago in reply to kas.lewis

    I just looked in hardwareserial.h of the arduino library and found this image

    #if !defined(SERIAL_TX_BUFFER_SIZE)
    #if ((RAMEND - RAMSTART) < 1023)
    #define SERIAL_TX_BUFFER_SIZE 16
    #else
    #define SERIAL_TX_BUFFER_SIZE 64
    #endif
    #endif
    #if !defined(SERIAL_RX_BUFFER_SIZE)
    #if ((RAMEND - RAMSTART) < 1023)
    #define SERIAL_RX_BUFFER_SIZE 16
    #else
    #define SERIAL_RX_BUFFER_SIZE 64
    #endif
    #endif
    #if (SERIAL_TX_BUFFER_SIZE>256)
    typedef uint16_t tx_buffer_index_t;
    #else
    typedef uint8_t tx_buffer_index_t;
    #endif
    #if  (SERIAL_RX_BUFFER_SIZE>256)
    typedef uint16_t rx_buffer_index_t;
    #else
    typedef uint8_t rx_buffer_index_t;
    #endif

     

    This clearly shows that the buffer size depends on the ram size. and can be as low as 16 characters

     

    You dont specifically say what MSP430 your using so I don't know the actual RAM so cant be sure what this is evaluating to in your case

     

    looking into the Energia equivalent I fould this  ( C:\Program Files (x86)\energia\hardware\msp430\cores\msp430\HardwareSerial.cpp )

     

    #define SERIAL_BUFFER_SIZE 16

     

    Case Closed perhaps ?????

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube