![Ratoc Systems REX-USB61 User Manual Download Page 74](http://html.mh-extra.com/html/ratoc-systems/rex-usb61/rex-usb61_user-manual_788102074.webp)
4. API function reference
Page.4-29
Example – I2cSlaveSample [I2C] :
This program operates as a slave device for REX-USB61.
When reading from the I2C master device, send the response data which prepared
in advance to the I2C master device.
The transmission of data from the I2C master device is notified by the event of
receipt or the callback function.
And it is displayed on the application program. (C++)
It does not include error handling.
HANDLE hDeviceHandle;
// Device handle
char
csSlaveAddr[16];
//
Character string for I2C slave address
ULONG ulSlaveAddr;
// I2C slave address
char
*stopstring;
USHORT
usFreq;
//
Operating
frequency
USHORT
usActualFreq;
//
Actual
frequency
BYTE ResponseBuf[255];
// response data
WORD ResponseBytes;
// length of response data
// Get device handle
hDeviceHandle
=
usb61_open(&rsStatus);
// Supplies a power supply from I2C master
// REX-USB61 does not supply power
usb61_power_control(hDeviceHandle, RS_PWRCTRL_OFF);
// I2C salve address
GetDlgItemText(hwnd,
IDE_SLAVE_ADDRESS, csSlaveAddr, sizeof(csSlaveAddr));
ulSlaveAddr = strtoul(csSlaveAddr, &stopstring, 16);
// Set I2C slave mode
usb61_mode_change(hDeviceHandle,
RS_DEVMODE_I2C | RS_DEVMODE_SLAVE,
(USHORT)ulSlaveAddr);
// Enable pull-up
usb61_i2c_pullup(hDeviceHandle, (RS_I2C_PULLUP)fPullup );
// Set frequency
usb61_i2c_set_freq_ex(hDeviceHandle, usFreq, &usActualFreq );
//
Set response data
//
usb61_i2c_set_response_data(hDeviceHandle, ResponseBytes, ResponseBuf );
// Waiting for receving data as I2C slave
// Save device handle
g_hwnd = hwnd;
// By using user-message, be notified the event of receipt
usb61_i2c_read_slave(hDeviceHandle, RS_NOTIFY_USER_MSG, NULL, g_hwnd );
:
(
When noitfied by event of receipt, receive user-message and display the transmission of data from the I2C
master device.
)