Announcement

Collapse
No announcement yet.

Visi and Serial communication not working

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

  • Visi and Serial communication not working

    I am trying to interface to an arduino to send data from arduino to be displayed on 9" Diablo LCD. At the moment I am doing the simplest level possible to just ensure communication is working and it is not. I have the TX1 of Arduino Mega connected to RX of display and RX1 connected to TX of display. There is also a ground wire. I wrote a simple sketch to repeatedly send 1234E over Serial 1 every second. I verified with a scope that the RX pin of display is seeing the data from the Arduino. The Serial1 of Mega is set to 115200.

    I am using the Visi program and started a fresh project. To set up the program in Visi I have the following set as global variables
    var values[10];
    var gotMessage := 0;
    var tempMessage[15];
    var tMcount;
    var combuf[100];

    Then inside main function I set up com, then set up a getSerial function to keep checking input. The getSerial is something I found in another post on this forum.

    var n;
    com_Init(combuf, 100, 0);
    n :=com_SetBaud(COM0, 11520);
    print("Baudrate Status = ");print(n,"\r\n");
    repeat

    getSerial(); // Get any serial and check for message
    forever
    *******************************************************
    func getSerial()

    var recbyte;

    n := com_Count();
    print("ComCount = ");print(n,"\r\n");

    while(com_Count() > 0)

    recbyte := serin();
    if(recbyte == 69) gotMessage := 1; // check for "E" and set message complete flag
    if(gotMessage != 1) // if message not complete keep building
    tempMessage[tMcount] := recbyte;
    tMcount ++;
    endif
    wend

    if(gotMessage == 1)
    gotMessage := 0;
    var n;
    var multiplier;
    var tempval;
    tempval := 0;
    multiplier := 1;
    for(n := tMcount - 1; n > 0; n--)
    if(tempMessage[n] > 47 && tempMessage[n] < 58)
    tempval := tempval + ((tempMessage[n] - 48) * multiplier);
    multiplier := multiplier *10;
    endif
    next
    if(tempMessage[0] == 97) values[0] := tempval; // if message starts with 'a' 97 being ASCII for a
    // add morechecks here in the same format eg 'b600\r\n'
    tMcount := 0;
    // ledDigitsDisplay(tempval, iLeddigits0+1, 304, 4, 2, 30, 1) ;
    endif

    endfunc

    The program compiles no errors. I upload to display and then disconnect rx/tx of programming cable and connect display rx/tx to the Arduino.
    I added some debug code, when setting the com port, it is supposed to return true however depending on baudrate, I get a different decimal number. In this case I get a 36.

    Then I put in a simple print statement to output on display the number of bytes in ComCount and it constantly says 0. The Arduino is running in a loop.

    I cant see what could be wrong. I have tried reversing RX/TX as well and it seems no serial data is being captured. This is a very simple routine and should be running.

    Any ideas?

  • #2
    Hi,

    I can't see anything obviously wrong. The only thing I can see that I wouldn't do myself is declaring n as a variable after it's been used

    var recbyte;

    n := com_Count();
    print("ComCount = ");print(n,"\r\n");

    I would put var n; before n := com_Count();

    Would it be possible to post your Arduino code as well and I'll test both together

    Best regards

    Paul

    Comment


    • #3
      It is crazy that it doesnt work. It cant be any more simple.

      The interface board I made connects between the 4D USB programming cable and the little red 5 pin breakout board supplied with the display. I have a small SPDT slide switch which will either connect the RX/TX of red board to programming cable or Arduino board. Plus I have a ground wire. Programming display from cable works fine and if I have switch in wrong position, it certainly lets me know when I go to program. When the switch is in Arduino position, I can measure serial signal at the RX pin of red breakout board.

      I assume there is no conflict doing it this way and using COM0 for programming and data input (not at same time). The baudrate response is a little odd since it is supposed to be a true value which I always assume is 1 but any number above 0 can be true. Is 36 the right response? Since this is the Visi designer, am I able to change the baudrate to any value when it comes to the use of COM0 as data input or must I stay with a specific setting? If so, what is that setting? I have tried both 9600 and 115200 on both Arduino and display. I have also reversed RX/TX on Arduino side.

      I am using a Arduino Mega, serial port 1. Here is the code. I am taking a number, converting to string value plus adding an E at end.

      #include "string.h"

      char value[6];

      void setup() {
      // put your setup code here, to run once:
      Serial1.begin(115200);
      Serial.begin(115200);
      }

      void loop() {

      memset(value,'\0',6);
      for (int x = 0; x<9999; x++)
      {
      sprintf(value,"%dE",x);
      Serial.println(x);
      Serial1.println(x);
      delay(1000);
      }

      Thanks
      Jim

      Comment


      • #4
        Hi Jim,

        This works for me but it looks like your comCount is zero due to it all being read. If you try this as a function you will see the comCount increase

        func getSerial()

        var recbyte, n;

        n := com_Count();
        gfx_MoveTo(0, 0);
        print("ComCount = ");print(n," \r\n");

        endfunc

        if you change your whole function to

        func getSerial()

        var recbyte;

        n := com_Count();
        if(n > 0)
        print("ComCount = ");print(n,"\r\n");

        while(com_Count() > 0)

        recbyte := serin();
        if(recbyte == 69) gotMessage := 1; // check for "E" and set message complete flag
        if(gotMessage != 1) // if message not complete keep building
        tempMessage[tMcount] := recbyte;
        tMcount ++;
        endif
        wend

        if(gotMessage == 1)
        gotMessage := 0;
        //var n;
        var multiplier;
        var tempval;
        tempval := 0;
        multiplier := 1;
        for(n := tMcount - 1; n > 0; n--)
        if(tempMessage[n] > 47 && tempMessage[n] < 58)
        tempval := tempval + ((tempMessage[n] - 48) * multiplier);
        multiplier := multiplier *10;
        endif
        next
        if(tempMessage[0] == 97) values[0] := tempval; // if message starts with 'a' 97 being ASCII for a
        // add morechecks here in the same format eg 'b600\r\n'
        tMcount := 0;
        // ledDigitsDisplay(tempval, iLeddigits0+1, 304, 4, 2, 30, 1) ;
        endif
        endif
        endfunc

        Comment


        • #5
          Hi

          Something is finally coming across but still not working correctly. One error I found on my end was I connected TX of arduino to RX of display and RX of Arduino to TX of display. It appears TX goes to TX and RX goes to RX, then I was seeing comCount change. Now the issue is the data isnt there, just garbage. From the Arduino I am sending 1234E. I added a print statement right after
          recbyte := serin();
          print (recbyte'\n\r");

          The readout I get is
          0
          130
          130
          130
          13240
          130
          130
          13240
          13

          Comment


          • #6
            Here is what I found. I have two different projects. The simple one which now works correctly with the serial com.The other project was created in Genie and then saved as a visi project. When I run the same code inside this project is when I get the odd data. I t appears to be reading data from somewhere else but triggered on my incoming data from arduino. I see a lot of extra programming code in the genie to visi conversion than I would if I just recreate the various widgets. I may try creating new widgets in my simple program and see what I get.

            Comment


            • #7
              With this command in the Arduino code you sent me you don't seem to be sending 1234E and there will line ending as well so if you used

              Serial1.print("1234E");

              It will finish with the 'E" and no line ending

              or

              if you wanted to send the value of x followed by E then

              Serial1.print(x); Serial1.print("E");

              Comment


              • #8
                I found that too, the above replies are after fixing that.

                Comment


                • #9
                  Here is another strange result. I am sticking to just building the display from scratch using Visi. Thanks to your help the serial port is working and I can get correct data.
                  I created an led digit (# decimals=1, Digits = 4, no leading zero). I pasted code into Visi and added the inherit file. I update the display with my incoming data. Everything is working except the decimal is not there. Here is the full code. The IDE screen shows display as 0.0 but when it appears on real LCD, it is 00. I am assuming since the display only handles integers, if I sent 1234 via serial, the display would be 123.4

                  // Program Skeleton 1.4 generated 3/2/2020 7:27:02 PM

                  #inherit "4DGL_16bitColours.fnc"

                  #inherit "VisualConst.inc"

                  #inherit "SerialTestConst.inc"
                  #inherit "ledDigitsDisplay.inc"


                  var values[10];
                  var gotMessage := 0;
                  var tempMessage[15];
                  var tMcount;
                  var combuf[100];

                  func main()
                  var hstrings ; // Handle to access uSD strings, uncomment if required
                  var hFontx ; // Handle to access uSD fonts, uncomment if required and change n to font number
                  // Uncomment the following if uSD images, fonts or strings used.

                  putstr("Mounting...\n");
                  if (!(file_Mount()))
                  while(!(file_Mount()))
                  putstr("Drive not mounted...");
                  pause(200);
                  gfx_Cls();
                  pause(200);
                  wend
                  endif
                  gfx_TransparentColour(0x0020); // uncomment if transparency required, please understand why and when you need to use this
                  gfx_Transparency(ON); // uncomment if transparency required, as generally there is a performance hit when using it

                  //hFontn := file_LoadImageControl("SERIAL~1.dan", "SERIAL~1.gcn", 1); // Open handle to access uSD fonts, uncomment if required and change n to font number dropping a and c if > 9
                  hstrings := file_Open("SERIAL~1.txf", 'r') ; // Open handle to access uSD strings, uncomment if required
                  hndl := file_LoadImageControl("SERIAL~1.dat", "SERIAL~1.gci", 1);


                  var numx :=0;

                  // Leddigits1 1.0 generated 3/3/2020 3:05:59 PM
                  img_Show(hndl, iLeddigits1); // show all digits at 0, only do this once
                  ledDigitsDisplay(numx, iLeddigits1+1, 304, 4, 2, 30, 1) ;



                  gfx_Set(SCREEN_MODE,LANDSCAPE) ;
                  gfx_Cls();
                  ledDigitsDisplay(numx, iLeddigits1+1, 304, 4, 2, 30, 1) ;


                  com_SetBaud(COM0, 11520);
                  com_Init(combuf, 100, 0);


                  repeat

                  getSerial(); // Get any serial and check for message

                  forever
                  endfunc


                  func getSerial()

                  var recbyte, n;


                  while(com_Count() > 0)

                  recbyte := serin();

                  if(recbyte == 69) gotMessage := 1; // check for "E" and set message complete flag
                  if(gotMessage != 1) // if message not complete keep building
                  tempMessage[tMcount] := recbyte;
                  tMcount ++;
                  endif
                  wend

                  if(gotMessage == 1)
                  gotMessage := 0;
                  //var n;
                  var multiplier;
                  var tempval;
                  tempval := 0;
                  multiplier := 1;
                  for(n := tMcount-1; n >= 0; n--)
                  if(tempMessage[n] > 47 && tempMessage[n] < 58)
                  tempval := tempval + ((tempMessage[n] - 48) * multiplier);
                  multiplier := multiplier *10;
                  endif
                  next
                  if(tempMessage[0] == 97) values[0] := tempval; // if message starts with 'a' 97 being ASCII for a
                  // add morechecks here in the same format eg 'b600\r\n'
                  tMcount := 0;
                  ledDigitsDisplay(tempval, iLeddigits1+1, 304, 4, 2, 30, 1) ;
                  endif

                  endfunc

                  Comment


                  • #10
                    Can you post the SerialTestConst.inc file pse?

                    Comment


                    • #11
                      Hi,

                      Welcome to the forum,

                      SerialTestConst.inc is usually generated within the Visi environment and is named using the projects saved name + 'Const'. Could you please try pressing SHIFT and F9 to see if this file is re-generated. If this doesn't solve the issue would it be possible to send me your project so I can see why it wasn't generated.

                      Best regards

                      Paul

                      Comment


                      • #12
                        Thanks. I am using the same code as above in Workshop4 Visi with a gen4-uLCD-43DT to try and get Arduino serial to write to LCD but can't get workshop to compile. Shift F9 just produces an 'L'
                        Last edited by Doris; 22 May 2020, 09:53 PM.

                        Comment


                        • #13
                          Hi,

                          Ok, the code above is from a Visi project and includes an LedDigits object which is why the SerialTestConst.inc is needed. In order to make this work in Visi you will need to create the LedDigits object in the object inspector in Visi. Once created you will need to press the paste code button and put it in place of this code in the three places this appears,

                          ledDigitsDisplay(numx, iLeddigits1+1, 304, 4, 2, 30, 1) ;

                          Also, comment out this line

                          hstrings := file_Open("SERIAL~1.txf", 'r') ; // Open handle to access uSD strings, uncomment if required.

                          Save the project as SerialTest and then press compile.

                          I am not able to send you SerialTestConst.inc as it is genrated based on the position size and style of the LedDigits object.

                          I hope this helps

                          Best regards

                          Paul



                          Comment


                          • #14
                            Yes it does help, lots, thank you. Did as you said and now getting the following errors:

                            Error: 'com_Count' not found (line 79 file:SerialTest.4Dg)
                            Error: context error for unknown 'com_Count' (line 79 file:SerialTest.4Dg)
                            Error: error in 'while' expression (line 81 file:SerialTest.4Dg)
                            Error: 'serin' not found (line 81 file:SerialTest.4Dg)
                            Error: context error for unknown 'serin' (line 81 file:SerialTest.4Dg)
                            Error: 'wend' without matching 'while' (line 88 file:SerialTest.4Dg)
                            Error: 'img_Show' not found (line 108 file:SerialTest.4Dg)
                            Error: context error for unknown 'img_Show' (line 108 file:SerialTest.4Dg)
                            Error: 'numx' not found (line 109 file:SerialTest.4Dg)
                            Error: context error for unknown 'numx' (line 109 file:SerialTest.4Dg)
                            Error: 'putstr' not found (line 22 file:SerialTest.4Dg)
                            Error: context error for unknown 'putstr' (line 22 file:SerialTest.4Dg)
                            Error: 'file_Mount' not found (line 23 file:SerialTest.4Dg)
                            Error: context error for unknown 'file_Mount' (line 23 file:SerialTest.4Dg)
                            Error: 'file_Mount' not found (line 24 file:SerialTest.4Dg)
                            Error: context error for unknown 'file_Mount' (line 24 file:SerialTest.4Dg)
                            Error: error in 'while' expression (line 25 file:SerialTest.4Dg)
                            Error: 'putstr' not found (line 25 file:SerialTest.4Dg)
                            Error: context error for unknown 'putstr' (line 25 file:SerialTest.4Dg)
                            Error: 'pause' not found (line 26 file:SerialTest.4Dg)
                            Fatal error: too many errors, terminating

                            Comment


                            • #15
                              Hi,

                              Ok, can you add this line as the very first line of code

                              #platform "Gen4-uLCD-43DT"

                              Comment

                              Working...
                              X