REXYGEN Community Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Login

    Solved I2C EZO component

    Communication (RS232, RS485, I2C, SPI, UDP, TCP, ...)
    4
    20
    2.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      reitinge
      last edited by

      Error -106 means Invalid parameter. Error codes are documented here: https://www.rexygen.com/doc/ENGLISH/MANUALS/BRef/BRef_ENGap3.html

      • Please, delete the space between Trace and (.
      • Check value of the parameter p0 in the Rexlang block (attached figure). 24b4564a-93f8-4b58-8b55-551866ceadce-image.png
        This is the value which is set into the variable i2c_dev. In my case, it is the bus /dev/i2c-1. This depends on your wiring.
      • Are you sure, you are running the Rexygen program on your Raspberry? The Error: REXLANG open this file type is not possible on this platform indicate, that you might run on your local PC and there is no I2C bus.

      Cheers,
      Jan

      B 1 Reply Last reply Reply Quote 0
      • B
        belgacom @reitinge
        last edited by

        @reitinge

        Hello, Happy new year. Thanks for your help and sorry for the time lost for my com issue. I was targeting Localhost and not my Pi. Except the fact that command need to be send in Hex and not in Dec, your code works find.

        I'm able to send command and read values back.
        Thanks a lot.

        I still have a couple of question for you :

        1. now I have the reading of my probe in bufRx [1] to [5] in DEC. and I need to convert it in ASCII. (51 46 56 57 54 = 3.896 )
          Is there a function for that, in Rexlang or another FB to use ?

        2. Between a write and a read, I have for example 900ms to wait . So currently I have done 2 Rexlang, 1 to write, 1 to read.
          And I use a timer between both FB. Is there a way to do it directly in Rexlang, or do I have to continue to use 2 block ?
          I didn't find answer in the doc.

        Have a good day.

        Arnaud.

        R 1 Reply Last reply Reply Quote 0
        • R
          reitinge @belgacom
          last edited by reitinge

          @belgacom
          Happy new year to you too. You are welcome. I'm sorry for decadic <-> hexadecimal converting trouble. I usually send values as a hex, but I thought the decimal values would work as well.

          1. For converting DEC/HEX values into string, you can use something like this:
              int i;    
              char message[5];    
              /*bufRx[1] = 51;    
              bufRx[2] = 46;    
              bufRx[3] = 56;    
              bufRx[4] = 57;    
              bufRx[5] = 54;*/    
              for (i=1; i<6; i++)    
            	message[i-1] = bufRx[i];    
              Trace(1,message); 
            
          2. Yes, you can use one Rexlang block for reading / writing. The easiest way is to use some type of simple if-else or switch-case structure in the main() function. Rexlang provides functions as Sleep() or Suspend() (see documentation https://www.rexygen.com/doc/ENGLISH/MANUALS/BRef/REXLANG.html#x270-26900015) but I think the right way is to send a request and periodically checking if the answer received. You should change the period of your task where the Rexlang block is too. https://www.rexygen.com/doc/ENGLISH/MANUALS/BRef/EXEC.html The Rexlang block source code (and all others blocks in the task) are executed only once per period time.

          Best regards,
          Jan

          B 1 Reply Last reply Reply Quote 0
          • B
            belgacom @reitinge
            last edited by

            @reitinge

            Hello, Happy new year. Hope you are going .
            I tried to use trace with you code up here, but even if the block is returning the correct value of my Ezo components,
            I have an 508 error.

            errore 508 .jpg
            therefore no trace is seen in the log . any idea why 508 error ?

            Anyway even if I hand write 5 values into message[5], how after do I see it on the HMI page.
            If I use a declaration of output like :

            long output(x) message; or string output(x) I can't manage to see values in message.
            How do I proceed ?

            thx

            Arnaud

            1 Reply Last reply Reply Quote 0
            • R
              reitinge
              last edited by

              Hi Arnaud,
              thanks, I hope you are fine too. Can you post here your actual code from Rexlang, please? When an error occurs, code execution stops, so the outputs may not be updated. We will try to find and fix the Numeric range check error.

              Cheers,
              Jan

              B 1 Reply Last reply Reply Quote 0
              • B
                belgacom @reitinge
                last edited by belgacom

                @reitinge

                Hi.

                I had time to dig a littl bit, and I find solution to do the write and read in 1 rexlang.
                After that no more issue with alarm and I'm able to trace " message"

                It works fine.
                But still I don't understand how to display the value of "message" in the HMI designer.

                trace.jpg

                I tried with standard display and string display.
                I would like also to trend the ASCII value ( 3.xxxx)
                Can you give me a hand on this ?

                Thx.

                Arnaud.

                my code :

                /[5]*************************************
                *

                • REXLANG - Reading data from MCP3422 AD converter via I2C

                *************************************************************/

                string parameter(0) i2c_dev; // the I2C bus is defined by the p0 parameter

                //assigning variables to outputs, these variables are WRITE-ONLY
                long output(0) adc_value;
                long output(1) adc_value1;
                long output(2) adc_value2;
                long output(3) adc_value3;
                long output(4) adc_value4;
                long output(5) adc_value5;
                long output(6) adc_value6;
                long output(7) message;
                string output(8) message;

                //declaration of variables
                long i2c_bufTx[4]; //buffer for transmitting data
                long i2c_bufRx[7]; //buffer for receiving data
                long i2c_bus_handle;
                long i2c_chip_address;
                long i2c_write_count;
                long i2c_read_count;
                long i2c_ret_fun;
                int i;
                char message[5];

                //the init procedure is executed once when the REXLANG function block initializes
                int init(void)
                {
                i2c_bus_handle = OpenI2C(i2c_dev); // open I2C bus
                Trace(1,i2c_bus_handle);

                i2c_chip_address = 0x63; // 7-bit address of the I2C device

                return 0;
                

                }

                //the main procedure is executed once in each sampling period
                long main(void)
                {
                //i2c_bufTx[0] = 0x46;
                //i2c_bufTx[1] = 0x69;
                //i2c_bufTx[2] = 0x6E;
                //i2c_bufTx[3] = 0x64;

                i2c_bufTx[0] = 0x52;

                // i2c_bufTx[0] = 0x53;
                //i2c_bufTx[1] = 0x6C;
                //i2c_bufTx[2] = 0x65;
                //i2c_bufTx[3] = 0x65;
                //i2c_bufTx[4] = 0x70;

                i2c_write_count = 1;
                i2c_read_count = 0;

                //Sending data via I2C
                i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);

                Suspend (3);
                i2c_write_count = 0;
                i2c_read_count = 7;

                i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);

                adc_value = (i2c_bufRx[0]);
                adc_value1 = (i2c_bufRx[1]);
                adc_value2 = (i2c_bufRx[2]);
                adc_value3 = (i2c_bufRx[3]);
                adc_value4 = (i2c_bufRx[4]);
                adc_value5 = (i2c_bufRx[5]);

                for (i=1; i<6; i++)
                message[i-1] = i2c_bufRx[i];
                Trace(1,message);

                return 0;
                }

                //the exit procedure is executed once when the task is correctly terminated
                // (system shutdown, downloading new control algorithm, etc.)
                long exit(void)
                {
                if(i2c_bus_handle>=0) Close(i2c_bus_handle); // close I2C bus
                return 0;
                }

                B R 2 Replies Last reply Reply Quote 0
                • B
                  belgacom @belgacom
                  last edited by

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • R
                    reitinge @belgacom
                    last edited by reitinge

                    @belgacom
                    Hi Arnaud,
                    congratulations to your first received message! 🙂

                    Your problem with publishing message outside of Rexlang is due to the names of your variables. Output 7, 8 and variable message have the same name and this is a problem. Try something like this:

                    • Rename outputs:
                    double output(7) outMessageDouble;
                    string output(8) outMessageStr;
                    
                    • In the main() method, assign message variable value to output(8) and you can also convert the string value into double and assign it into output(7):
                    outMessageStr = message;
                    outMessageDouble = str2double(message);
                    

                    Please resize message variable length from 5 to 6. In the Rexlang, the last char array index contains "end-of-string indicator". If you write something to the last index of the char array, you will have a memory leak problem.

                    Cheers,
                    Jan

                    B 1 Reply Last reply Reply Quote 0
                    • B
                      belgacom @reitinge
                      last edited by

                      @reitinge

                      Everything works fine !
                      Thank you.

                      R 1 Reply Last reply Reply Quote 0
                      • R
                        reitinge @belgacom
                        last edited by

                        @belgacom You are welcome 🙂

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post

                        This is a community forum for REXYGEN users and fans. Detailed information can be found at REXYGEN homepage.

                        There is also an outdated REXYGEN community forum.

                        Powered by NodeBB.