Announcement

Collapse
No announcement yet.

Serial Communications with Arduino....very close to getting it working

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

  • Serial Communications with Arduino....very close to getting it working

    I want to preface this by saying I don't want to be the lazy guy that posts his code and lets others solve its issues without doing some research. That said, I've read most of the threads where people are trying to do what I'm doing.

    What am I trying to do: Take a sensor measurement on an arduino, send it to a goldelox display module (using a Visi setup), and display the data on a round gauge.

    So, I used and adapted code from these posts:
    http://forum.4dsystems.com.au/forum/...cd-70-dt/page2
    and http://old.4dsystems.com.au/code/ind...?node=10&id=28
    and especially this one: http://forum.4dsystems.com.au/forum/...l-uoled-160-g2

    All the bit shifting and Hex using reminds me of CS classes I took a long time ago, but it makes sense for the most part.

    With the code below I can plug the arduino into the computer, send it the "ACK" of '!' or '21' in HEX from the terminal and get back a random number in the format I want (Bhexhex). It works repeatedly, etc. I can also plug in the display module to the computer and in the terminal and send it the same sequence and get back an ack and have a measurement show up on the gauge. However, with the display module plugged in to the arduino I get nothing.

    So, what's that one small silly thing that I'm missing.

    Arduino code:
    Code:
    #define DisplaySerial Serial
    
    void setup() {
      DisplaySerial.begin(9600);
      //delay(10000);
    }
    
    void loop() {
      //DisplaySerial.flush(); 
      while(!DisplaySerial.available()) ; 
      if (DisplaySerial.read() == '!'){
        int iBoost = getBoost();  
        DisplaySerial.print('B', HEX);
        DisplaySerial.print(0,HEX);
        DisplaySerial.print(0,HEX);
        if (iBoost < 16){ DisplaySerial.print(0,HEX);}
        DisplaySerial.print(iBoost, HEX);
        DisplaySerial.flush();
      }
    }
    
    int getBoost(){
      return random(0,17);
    }
    4DGL code
    Code:
    #platform "GOLDELOX"
    
    // Program Skeleton 1.0 generated 1/28/2016 9:13:29 PM
    
    #inherit "4DGL_16bitColours.fnc"
    
    #inherit "VisualConst.inc"
    
    #inherit "gaugepagesConst.inc"
    
    var buf[4];
    
    func main()
        var numx;
        var page;
        setbaud(BAUD_9600);
        com_Init(buf,8,0);
        print("Starting\n") ;
        //check for SD card...repeat forever
        while(!media_Init())
            putstr("Missing SD Card!");
            pause(200);
            gfx_Cls();
            pause(200);
        wend
    
        //sweep the gauge
        media_SetAdd(iAngularmeter1H, iAngularmeter1L);
        var i;
        repeat
            i := i + 2;
            media_VideoFrame(0,0,i);
        until (i >= 45);
        repeat
            i := i - 2;
            media_VideoFrame(0,0,i);
        until (i <= 0);
        media_VideoFrame(0,0,0);
        var cmd;                       
        serout('!');
        repeat
            if (com_Count() >= 3)
                print([STR]buf);  //debug print of a string of the buffer (should print 'B' and nothing else)
                print("\n");
                serout('!');    //ACK to the arduino saying it can take more data
                cmd := serin();
                if (cmd == 'B')
                    numx := serin() << 8 | serin();      //if you input 420015 (which is hex for Bnullnull21) you get an output of 21)
                    pause (5000);
                    com_Init(buf,8,0);  //reset the buffer
                endif
            endif
                //instead sit and wait for serial input from the arduino
                //numx := ABS (RAND() % 1000); //then the next 4 bytes are the float
                //scale the float to the dial positions
    
    
            // Angularmeter1 1.0 generated 1/29/2016 9:40:30 PM
            media_SetAdd(iAngularmeter1H, iAngularmeter1L) ;      // point to the Angularmeter1 image
            media_VideoFrame(0, 0, numx) ;          // where numx is 0 to 45 (for a displayed -15 to 30)
        forever
    endfunc

  • #2
    Can't spot anything obviously wrong, so I'm wondering if you have the TX and RX swapped over.

    Try running your 4DGL program using the Terminal program and the programming cable and get it going that way.

    You could also try exercising the Arduino program in much the same way.
    Mark

    Comment


    • #3
      You were partially correct...

      The RX and TX do not get crossed as I expected but instead match. Not I get some of my debug output on the screen but it looks like this:
      (edit: I took out the gauge frame redraw and it shows this as what it is getting in the buffer)
      42000B
      42000B42
      42000B42
      42000B42
      42000B42
      42000B42


      This leads me to believe that there is an issue with the way I'm sending the data as 42 is the numbers for the hex for 'B' but not the hex for 'B'...there is also a timing issue that i resolved (put the sending of the '!' at the beginning of the repeat loop and got rid of the extra line that initializes the sending with a '!')
      Last edited by stirobot; 5th June 2016, 08:03 AM.

      Comment


      • #4
        For posterity this is working and displays random values on a 45 step "angular meter":

        Visi/4DGL:
        Code:
        #platform "GOLDELOX"
        
        // Program Skeleton 1.0 generated 1/28/2016 9:13:29 PM
        
        #inherit "4DGL_16bitColours.fnc"
        
        #inherit "VisualConst.inc"
        
        #inherit "gaugepagesConst.inc"
        
        var buf[4];
        
        func main()
            var numx;
            var page;
            setbaud(BAUD_9600);
            com_Init(buf,5,0); //8
            print("Starting\n") ;
            //check for SD card...repeat forever
            while(!media_Init())
                putstr("Missing SD Card!");
                pause(200);
                gfx_Cls();
                pause(200);
            wend
        
            //sweep the gauge
            media_SetAdd(iAngularmeter1H, iAngularmeter1L);
            var i;
            repeat
                i := i + 2;
                media_VideoFrame(0,0,i);
            until (i >= 45);
            repeat
                i := i - 2;
                media_VideoFrame(0,0,i);
            until (i <= 0);
            media_VideoFrame(0,0,0);             
            var cmd;
            //serout('!');
            repeat
                serout('!');    //ACK to the arduino saying it can take more data
                if (com_Count() >= 3)
                    //print([STR]buf);  //debug print of a string of the buffer (should print 'B' and nothing else)
                    //print("\n");
                    cmd := serin();
                    if (cmd == 'B')
                        numx := serin() << 8 | serin();      //if you input 420015 (which is hex for Bnullnull21) you get an output of 21)
                        //pause (5000);
                        com_Init(buf,5,0);  //reset the buffer
                    endif
                endif
        
                // Angularmeter1 1.0 generated 1/29/2016 9:40:30 PM
                //media_SetAdd(iAngularmeter1H, iAngularmeter1L) ;      // point to the Angularmeter1 image
                media_VideoFrame(0, 0, numx) ;          // where numx is 0 to 45 (for a displayed -15 to 30)
            forever
        endfunc
        Araduino:
        Code:
        #define DisplaySerial Serial
        
        void setup() {
          DisplaySerial.begin(9600);
          //delay(10000);
        }
        
        void loop() {
          DisplaySerial.flush(); 
          while(!DisplaySerial.available()) ; 
          if (DisplaySerial.read() == '!'){
            int iBoost = getBoost();  
            //DisplaySerial.print('B', HEX);
            //DisplaySerial.print(0,HEX);
            //DisplaySerial.print(0,HEX);
            //if (iBoost < 16){ DisplaySerial.print(0,HEX);}
            //DisplaySerial.print(iBoost, HEX);
            DisplaySerial.write('B');
            DisplaySerial.write(0x00);
            //DisplaySerial.write(0x00);
            //DisplaySerial.write(0x00);
            //DisplaySerial.write(0x0A);
            DisplaySerial.write(lowByte(iBoost));
            //DisplaySerial.flush();
          }
        }
        
        int getBoost(){
          int bst = random(0,45);
          return random(0,45);
        }

        Comment

        Working...
        X