20x4 LCD display only displaying 63 char from Python serial [Solved]

Hi, I am using python to communicate with an Arduino board and then displaying text on the LCD attached to the arduino..

My problem is that the LCD is 20x4 but I am somehow getting only 63char not the total 80. I have tested solely with arduino script independent of python and it works as expected, but when I write the text using python, it works until char 64. I have tried numerous ser.write options and it still somehow still gets cut off.

I am wondering if there is something either in the serial bit communication limitation, or hidden character, string limitations that I am naive to.

Thanks in advance.

I am wondering if there is something either in the serial bit communication limitation, or hidden character, string limitations that I am naive to.

Could be. But, you didn't mention what version of the IDE you are using, on what operating system, and you posted no code.

Can't expect a lot of help in the Programming forum if you don't post your code.

HI Paul. Here is the code. in Arduino 1.0

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup()
{
    // set up the LCD's number of columns and rows: 
  lcd.begin(20, 4);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(1000);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }
}

and the python 2.7 code
** **import serial, time[/b] ser =serial.Serial('COM5', 9600) time.sleep(5) abc = "000000000000000000000000000000000000000000000000000000000000000000000000000" ser.write(abc) print abc** **
**Again, I have verified that if I do a standard print to LCD from arduino without python, the screen works fine. but the above colde only shows 63 chars with remaining ones cut off, I also tried doubling up on the ser.write(abc) to see what happens, same results. For some reason the last 17 chars do not display when I ser.write from python. **
Thanks for you help.

Once the first character arrives, you sit around doing nothing for a while. During that time, you expect the whole message to arrive. That is not a reasonable way to manage serial communications.

You should send something to indicate the start of a packet, the packet, and something to indicate the end of the packet.

Then, you should read the whole packet, reading everything that arrives up to the end of packet marker. When the end of packet marker arrives, use the data.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

On the Python side, send something besides a string of 0s. How can you tell which 0s are missing? It could be the first ones that are missing, or some from the middle. You haven't a clue with what you are sending.

Paul, thanks that worked beautifully. quick note to other beginners out there. lcd.write(inData); to dispaly the total serial input.

Paul, can you briefly explain why the difference in results. It is the one that I used previous has a serial gap, as the arduino continues to print LCD as long as there is serial input. So am in correct in assuming that maybe the serial comm is in bursts and not a constant open stream. Or does the arduino default to timeout after a certain number of bits of data or time. So with your solution, it forces it to continue to read it until it reads end of packet. Just a curious beginner with maybe a potentially naive question.

a side note, I previously used the LCD serial write code from the tutorials online. None of them mentioned any of this "gotcha" for a beginner.

Thanks for your help.

Paul, can you briefly explain why the difference in results.

I can only theorize.

Serial data transmission is ssslllooowww. Like the way I type. If I was typing, and the characters were being sent to you as I typed, and you saw a character, and then closed your eyes for a second, and then opened them, and read everything that had arrived while your eyes were closed, you might, or you might not, have received a complete word/sentence/paragraph in that time.

Assuming that you had would be a mistake.

If, instead, you simply looked at the characters as they arrived, and waited until a carriage return arrived, you would know that a paragraph was complete.

The code you were using was the "eyes closed for a second" method. The code I posted was the "wait for a carriage return" method.

You could put your code back on, and have python send something other than a string of 0s. Say, the alphabet, upper and lower case, the digits 0 to 9, and all the special characters. Repeat as need to fill 80 characters. See what characters get "missed" and which get displayed as the start of the second message instead of at the end of the second message.

Change the time that you wait for the whole packet to arrive.

I don't recommend that as a fix, but it is worth experimenting with.

Glad that it was not an obvious mistake of mine. Will play with it different settings to try to figure out what is going on as I build on my adruino project.