Announcement

Collapse
No announcement yet.

C Code for Write String to uLCD-70DT

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C Code for Write String to uLCD-70DT

    Hey Everybody,

    I am writing code in C using an Arduino MEGA 2560 and a 4D Systems uLCD-70DT.

    I like to write my own code for many of the small operations that I do...I definitely understand everything better and I am glutton for punishment.

    I would like to write a string to a string object in Visi Genie. I created an function called updateHomeGPSCoords() to update the home GPS coordinates on the uLCD-70DT that I entered on a numeric keypad. This seems simple enough.

    In pseudocode, all I need to do is:

    write cmd (0x02)
    write id (0x02 for the particular string that I want to update)
    write the string length (or should this be string length + 1 ?
    write each character of the string
    write the checksum

    ...and this should work.

    I am a bit confused about whether or not I have to add 1 to the string length to account for the null character. I have tried both ways with no success.

    Let's say that my string is str[] = "+26.1619". Is my string actually "+26.1619\0"? What is the strlen(str)? 8 or 9? I still have to add a period in my code below. And I am not sure whether to use strcpy() to add the + sign and the decimal? Or maybe strcat(). I see that there are subtleties with both.

    In my case, my code looks like the following. I have tried this with and without the gpsHOME.latsgn. I have also tried this with just j and also with j+1. Nothing seems to get written to the uLCD-70DT.

    void updateHomeGPSCoords(byte CMD, byte ID)
    {
    //calculate checksum
    checksum = CMD;
    checksum ^= ID;
    checksum ^= j; // j is a counter used somewhere else in my program that is a measure of string length.
    checksum ^= gpsHOME.latsgn; //this is either a '+' or '-' sign to account for N-S or E-W for heading to precede the degree coordinates
    for(int i=0; i<j; i++)checksum ^= gpsHOME.latCoords[i];

    //write to uLCD-70DT
    uLCD.write(CMD);
    uLCD.write(ID);
    uLCD.write(j+1);
    uLCD.write(gpsHOME.latsgn);
    for(int i=0; i<j; i++)uLCD.write(gpsHOME.latCoords[i]);
    uLCD.write(checksum);
    }

    TIA,
    --Neal
    TIA,

    Neal

  • #2
    Hello Neal,


    I am a bit confused about whether or not I have to add 1 to the string length to account for the null character. I have tried both ways with no success.
    You do not have to add a null character on the message. If you are sending 5 characters, the length of the message is 5.

    I have also tried this with just j and also with j+1. Nothing seems to get written to the uLCD-70DT.
    I am unsure how are you processing the data at your end. Perhaps, you might want to create a simple message (e.g. Hello World) to send on the display so you can check if you are receiving the correct response. If not, maybe you should check the baud rate on both devices.

    Best Regards,
    Kevin
    Last edited by John Kevin; 28 August 2019, 06:02 PM.

    Comment


    • #3
      Why is it that I can use a command like:

      uLCD.write("hello world");


      But if my string is: str[] = {'h', 'e', 'l','l','o',' ','w','o','r','l','d','\0'}, then I need to individually write each char as in for(int i=0; i<strlen(str); i++)uLCD.write(str[i]);

      Why can't I just use uLCD.write(str);

      Aren't "hello world" and {'h', 'e', 'l','l','o',' ','w','o','r','l','d','\0'} essentially the same thing?
      TIA,

      Neal

      Comment


      • #4
        Hi Neal,

        That should be the same. I also tried to send the following conditions using an Arduino just to test it and I can receive the correct message to the display.
        Can you check again the parameters you used, maybe you just missed something in there?
        Code:
        char str[]={'h','e','l','l','o',' ','w','o','r','l','d','\0'};
        
          uLCD.write(byte(0x02));
          uLCD.write(byte(0x00));
          uLCD.write(byte(0xB));
        
          //uLCD.write("hello world"); //-this works
          uLCD.write(str);
          //for (int i=0; i<strlen(str); i++)  uLCD.write(str[i]); //this also works
          uLCD.write(byte(0x02^0x00^0xB)^104^101^108^108^111^32^119^111^114^108^100);
        You can also take a look at ViSi-Genie-Arduino-Library-master. It can also be implemented this way.
        Code:
        char str[]={'h','e','l','l','o',' ','w','o','r','l','d','\0'};
        void loop()
        {
          SendString(0,str);
          delay(2000);
          SendString(0,"Hello");
          delay(2000);
        }
        
        void SendString(uint16_t index, char *string ){
         char *p;
         unsigned int checksum;
         int len = strlen(string);
        
         checksum = CMD;
         uLCD.write(CMD); 
         checksum ^=ID;
         uLCD.write(byte(ID));
         checksum ^= len;
         uLCD.write((unsigned char)len);
        
         for (p = string ; *p ; ++p){
          checksum ^= *p;
          uLCD.write(*p);
         }
        
         uLCD.write(checksum);
        
        }
        Best regards,
        Kevin

        Comment


        • #5
          SOLVED!!!

          Thank you for all of your suggestions. My code now looks exactly like your code. Even with that, I was still having issues. I finally figured out that each element of the string that I was writing to the uLCD had to be declared as a char, NOT a byte. I believe that the uLCD protocol states that the checksum and write commands both require bytes, but an Arduino byte may be different from a GNU C compiler byte....not really sure.

          I solved this problem by explicitly creating a string (e.g. "Hello World") and writing it the uLCD. I then dissected the string and finally figured out this issue. So while the Arduino can output an explicit string like "Hello World" to the uLCD with no problem, if I receive GPS coordinates as bytes (which is what I was doing) and then try to perform a Write String of those bytes to the uLCD, this does not work. I had to essentially do a one-to-one transfer of bytes to chars like: for(i=0; i<strlen(str); i++)gpsChar[i] = gpsByte[i];
          TIA,

          Neal

          Comment


          • John Kevin
            John Kevin commented
            Editing a comment
            Hi Neal,

            I am glad you managed to sort it out.

            Best Regards,
            Kevin
        Working...
        X