@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.
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;
}