Serial freezing program

Hi Folks,

I've come across something that's puzzling me and is preventing me progressing very far!. As a demo, I'm using the following code:

void setup() {
  pinMode(13, OUTPUT); 
  
  Serial.begin(9600);
  Serial.println("Testing 1, 2, 3");
  Serial.println("Testing 4 5, 6");
  Serial.println("Testing 7, 8, 9");
  Serial.println("Testing 10, 11, 12");
  Serial.println("Testing 13, 14, 15");
}

void loop() {
  Serial.print("Loop");
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off  
  delay(1000);
}

And this is the output to the serial monitor:

Testing 1, 2, 3
Testing 4 5, 6
Testing 7, 8, 9
Testing 10, 11,

It doesn't print from loop() and the LED doesn't blink. If I comment out a few of the "Testing..." lines, it runs through as I'd expect and the LED flashes.

Any thoughts, please?

Thanks,

John.

What version of the IDE are you using? What is on the other end of the serial port?

version 1.0 - just the Serial Monitor, no physical hardware.

Edit: I've just tried it on the laptop and it works perfectly. I've checked that I have the latest motherboard drivers installed but re-installed them anyway. I've also re-installed the Arduino USB driver. I've even unplugged all other USB devices (except the mouse).

Very odd!

John.

I've discovered the cause of this problem. I had edited HardwareSerial.cpp as mentioned in Reply #4 here: http://arduino.cc/forum/index.php/topic,81070.0.html

struct ring_buffer
{
  unsigned char buffer[RX_BUFFER_SIZE];
 
 //  volatile int head;  // Original declarations
 //  volatile int tail;

  unsigned char head;  // New declarations cause the problem
  unsigned char tail;
};

I'm not sure what the connection is but I have tried "old" and "new" declarations a few times which seems to confirm it. Maybe someone else would like to confirm it's the case.

John.

Serial is HardwareSerial. At the bottom of HardwareSerial.cpp:

// Preinstantiate Objects //////////////////////////////////////////////////////

#if defined(UBRRH) && defined(UBRRL)
  HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
#elif defined(UBRR0H) && defined(UBRR0L)
  HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
#elif defined(USBCON)
  // do nothing - Serial object and buffers are initialized in CDC code
#else
  #error no serial port defined  (port 0)
#endif

So, Serial is an instance of class HardwareSerial.

 //  volatile int head;  // Original declarations
 //  volatile int tail;

  unsigned char head;  // New declarations cause the problem
  unsigned char tail;

I think I would put "volatile" back, as this is being done in an ISR. So, try:

  volatile unsigned char head;  // New declarations cause the problem
  volatile unsigned char tail;

Also I would check that the code doesn't expect the head/tail variables to be signed. An example would be this sort of test:

if (head < 0)
  ...

Thanks, that fixed it!

With the original declarations, the sketch used 2688 bytes. With my faulty modification, it used 2486 and with your working version, it 2506 bytes. So the saving in memory isn't quite as great (180 bytes versus 202) but it works :slight_smile:

For consistency, I think I'll stick with the original unless I get desperate for memory.

I'm happy programming in Delphi (Pascal) which is sufficiently different to C/C++ to confuse me!

Thanks again for the fix.

John.