FB_Init initialization of I2C device, Exception Access violation

DavidBo
2020-01-14
2020-01-14
  • DavidBo - 2020-01-14

    I have a CFC block which is supposed to read a value from a I2C device regularly, but before that, the device (actually an ADS1115) has to be configured once before the the evaluation of the block starts. I thought that could be done in the method FB_Init for the block. But it can't. The code compiles without any problems, but after download I get an Exception which says AccessViolation. Why? And how do I configure the device only once?

    METHOD FB_Init: BOOL
    VAR_INPUT
    Β  Β  bInitRetains: BOOL; // TRUE: the retain variables are initialized (reset warm / reset cold)
    Β  Β  bInCopyCode: BOOL;Β  // TRUE: the instance will be copied to the copy code afterward (online change)Β  Β 
    END_VAR
    VAR
    Β  Β usiAddress : USINT:= 16#48;
    Β  Β byWriteBufferC : ARRAY [0..2] OF BYTE :=[16#01,16#84,16#83]; //Sets continous mode
    Β  Β write :DINT;
    END_VAR
    write:=I2C_master.write(usiAddress, ADR(byWriteBufferC),3); //Generates an AccessViolation
    FB_Init:=TRUE;
    
     
  • bjh - 2020-01-14

    It is a bad idea to utilize or reference any (external) objects in FB_Init other than the FB that you are initializing as you run the risk of the object (I2C_master in this case) not "existing" in memory yet when the FB is initialized.

    Did you write a driver for the ADS1115? If you do, you can set the initial config of the ADS1115 using the state machine built into the template found at https://forge.codesys.com/drv/io-driver ... ?version=1 m

     

Log in to post a comment.