• Announcements

    • bluehash

      Forum Upgrade   03/11/2017

      Hello Everyone, Thanks for being patient while the forums were being fixed and upgraded. Please see details and report issues in this thread. Thanks!
TheArduinoGuy

Unexplained Serial behaviour

11 posts in this topic

I have the following simple code set up to echo whatever I type into the Serial Monitor, but as HEX with a comma separating each byte (So I can also see control codes) :

int inByte = 0;          // incoming serial byte
byte buffer[30]; // buffer for bytes coming from HMI.

void setup()
{                         // Enable USCI_A1 RX interrupt

// start serial port at 9600 bps:
      Serial1.begin(9600);
      Serial1.print("READY");
}

void loop()
{
        int bytesread = 0;
        // if we get a valid byte, read analog ins:

        if (Serial1.available()>0) { // there are bytes in the serial buffer to read
                while(Serial1.available()>0) { // every time a byte is read it is expunged
                        // from the serial buffer so keep reading the buffer until all the bytes
                        // have been read.
                        byte inbyte = Serial1.read();
                        buffer[bytesread] = inbyte;
                        bytesread++;
                }
        }

        for (int x = 0; x < bytesread; x++)
        {
                Serial1.print(buffer[x], HEX);
                Serial1.print(",");

        }
}

However, it is behaving a bit weird and unexplained.

 

If I enter ABCDE and press ENTER I would expect the output to be:

41,42,43,44,45,D,A

But instead it just prints

41,

When I enter ABCDE and ENTER a second time I then get:

42,43,44,45,D,A,41

Which is the missing characters from the first entry pluis the first character only from the second.

 

This is not what I expected.

 

Device is an MSP430-5969

 

Any ideas please?

 

Thanks,

 

Mike

Share this post


Link to post
Share on other sites

OK that was something at the terminal side rather the MSP430 side.

 

However, I have discovered something weird going on...

 

I have a Nextion display that communicates over serial at 9600 baud.

 

When you press a button it sends a text string with codes to show which button on which page has been pressed etc.

 

When the screen is connected to an Arduino Nano I get valid strings back which look like:

"65 0 4 0 ffff ffff ffff"

But when connected to the MSP430 the message back from the display is just garbled.

 

I can send data FROM the MSP430 to the display perfectly fine and control it no problem. But data received back from the display is garbled. The display is fine (as shown when connecting it to a Nano) so what is going on here?

Share this post


Link to post
Share on other sites

Has anybody else with an MSP430 (even better still the FR5969 variant) managed to get serial at 9600 baud to be read successfully by the device?

Share this post


Link to post
Share on other sites

 

Has anybody else with an MSP430 (even better still the FR5969 variant) managed to get serial at 9600 baud to be read successfully by the device?

Yes, of course.  When you ask for advice, always specify the compiler/version you are using as well as the LaunchPad.  Also, describe or post a photo of the circuit where it makes sense. Fully describe the problem and where possible give sufficient information for others to duplicate the problem.  There is a button below posts in 43oh where you can thank others when you get help.

 

The following was done in Energia V17.  First, set up a source for Serial1.  I used a F5529.  TX for Serial1 is on pin 4.  Here is the code, it outputs the 255 characters  on one line and then starts over:

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

void loop()
{
  int i;
  for(i=0; i<255; i++){
    Serial1.write(i);
    delay(100);
  }
}

Second, set up a receiver, in this case a FR5969.  RX for Serial1 is on pin3 and it is attached to pin 4 (TX) on the F5529.  I am powering both LaunchPads off of the FR5969.  Be sure to place a ground wire between the two LaunchPads.

post-45284-0-38357200-1487886429_thumb.jpg

 

Here is the code for the receiver:

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial.println("Started...");
  Serial1.begin(9600);
  
}

void loop() {
    if (Serial1.available()) {
      int inByte = Serial1.read();
      Serial.write(inByte); 
    }
}

Run and the output will look something like this....

post-45284-0-17824500-1487886079_thumb.jpg

 

energia likes this

Share this post


Link to post
Share on other sites

Thanks for those suggestions. I have already done something similar but I will try again. I don't have another MSP430 board so will send the serial via an Arduino Nano. I will let you know how I get on.

 

Cheers,

 

Mike

Share this post


Link to post
Share on other sites

You may want to try adding a small delay to your loop as @@Fmilburn did in his example. I have found it necessary to add delay loops in my code as well when dealing with slow (9600 baud) serial. Sometimes just blinking a "character received" LED inside the loop will accomplish the same thing.

Share this post


Link to post
Share on other sites

As far as I know the Arduino Nano is a 5 Volt device. I think the same is true for the Nextion display. The MSP430 however is a 3.3 Volt device. So be very carful with that. The I/O's on the MSP430 are NOT 5 Volt tolerant and you might have burned out the RX pin on the MSP430. To test this you can connect the TX pin to the RX pin on the MSP430 to see if it is still operational. 

 

Robert

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now