Jump to content
43oh

Serial.print(i, DEC) prints garbage


Recommended Posts

I'm getting started with an MSP-EXP430G2 board with an msp430g2231 on Ubuntu 20.10 groovy (64 bit).

I can use Serial.print and Serial.println just fine to print strings. But if I use it to print integers the Serial monitor just shows a lot of backward question marks (⸮⸮⸮⸮⸮⸮⸮⸮).

Assuming it was a bug in the print code, I copied print(long, int) and printNumber(long, int) functions into my sketch. To my surprise these work fine!

Using Serial.print often goes well up to 9, but breaks when it reaches 10. Sometimes it halts completely (image of serial monitor)

I already downloaded energia again to a different disk, but I got the same problem there. I don't know how to debug this any further..

void setup() {  
  Serial.begin(4800);    
}

// Copied from msp430/Print.cpp
size_t printNumberCopied(unsigned long n, uint8_t base) {
  char buf[8 * sizeof(long) + 1];  // Assumes 8-bit chars plus zero byte.
  char *str = &buf[sizeof(buf) - 1];

  *str = '\0';

  // prevent crash if called with base == 1
  if (base < 2) base = 10;

  do {
    unsigned long m = n;
    n /= base;
    char c = m - base * n;
    *--str = c < 10 ? c + '0' : c + 'A' - 10;
  } while (n);

  return Serial.write(str);
}

// Copied from msp430/Print.cpp
size_t printCopied(long n, int base) {
  if (base == 0) {
    return Serial.write(n);
  } else if (base == 10) {
    if (n < 0) {
      int t = Serial.print('-');
      n = -n;
      return printNumberCopied(n, 10) + t;
    }
    return printNumberCopied(n, 10);
  } else {
    return printNumberCopied(n, base);
  }
}

void loop() {
  for (int i = 0; i<64; i++){
    delay(200);
    printCopied(i, 10); // <-- works
    //Serial.print(i, 10); // <-- garbage written into serial port when uncommented
    Serial.println("hi");
  }
}

 

Link to post
Share on other sites

I set the baud rate at 4800 as even "Serial.println("hello");" printed garbage when the monitor was set to 9600, but 4800 works fine when printing text, so I figured it would be better to keep it at 4800.

This code here:

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < 64; i++) {
    delay(100);
    Serial.println(i, DEC);
  }
}

Prints 0 up to 9 and then halts:
https://imgur.com/a/CGrg5Ks (screenshot)

I also replaced the msp430g2231 with a msp430g2211, but I see the same problem. Could the MSP-EXP430G2 be broken?

 

I tried downloading the binary from the chip again to compare it with the hex its uploading, as I now suspect the code is fine but some bits are getting flipped. But I could not figure out how (I tried using MSPFlasher).

$ ./MSP430Flasher -n msp430g2231 -r [hex.hex, MAIN] -v -i ttyACM0
* -----/|-------------------------------------------------------------------- *
*     / |__                                                                   *
*    /_   /   MSP Flasher v1.3.20                                             *
*      | /                                                                    *
* -----|/-------------------------------------------------------------------- *
*
* Evaluating triggers...done
* Checking for available FET debuggers: 
* Couldn't find any connected USB FETs!
* Powering down...done
* Disconnecting from device...done
* 
* ----------------------------------------------------------------------------
* Driver      : closed (MSP DebugStack not initialized)
* ----------------------------------------------------------------------------
*/

 

Link to post
Share on other sites

Likely  a dumb question/observation, but wondering about uint8_t:

size_t printNumberCopied(unsigned long n, uint8_t base) 

Why uint8_t? I believe the Arduino code from which this was likely drawn is an 8-bit mcu.  The msp430 are 16-bit. Is the printNumberCopied running into an overflow???

 

Link to post
Share on other sites

Thanks for all your responses!

Quote

What happens if you narrow the prblem. Remove the loop and just print a 10.

Just printing 10 works fine. This works perfectly as well:

void loop() {
    int i=10;
    Serial.println(i, DEC);
    Serial.println(i, DEC);
    Serial.println(i, DEC);
}

But this sometimes prints a line that says 'A'.

void loop() {
    int i=10;
    Serial.println(i, DEC);
    Serial.println(i, DEC);
    Serial.println(i, DEC);
    Serial.println(i, DEC);
}

So I think I'll assume I got a hardware problem ¯\_(ツ)_/¯. I uploaded it from my laptop as well, but I got the same problem there.

Quote

Why uint8_t?

I don't know why uint8_t, but I don't think it's overflowing since it only breaks after adding the forth exact same Serial.println.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...