ReadSlaveRegister
Reads data from an EtherCAT slave's register.
Syntax
KsCommandStatus ReadSlaveRegister(
int SlaveId,
int Offset,
int Length,
BYTE* Data
);
Parameters
SlaveId: the index of a slave array. When EtherCAT is started, this index has the same value as the SlaveId, which corresponds to the position of the slave in the network. Please note that after EtherCAT is started in the Operational (Op) state, any addition or removal of slaves from the network will change the position of the slaves in the network (SlaveId). Nevertheless, the index of the slave will remain the same. Newly added devices will be added in the back of the slave array. For all slaves after the change, the index and SlaveId will no longer match. This behavior is only available for physical devices; simulated devices are inapplicable. Please refer to the use cases in EnableHotConnect for more details.
Offset: the offset of the register to read.
Length: the length in byte of the data to read.
Data: the pointer to the buffer to read the data.
Return value
Returns the KsCommandStatus structure.
Remarks
Used to check a slave register during slave development.
IMPORTANT: This function is meant for slave development only. Otherwise, do not use it.
NOTE: This function uses a slow access method that should not be called in the cyclic callback, or it causes a deadlock.
Usable EtherCAT states
ecatInit, ecatPreOP, ecatSafeOP, ecatOP
Example
// This sample demonstrates how to read address 0X1C of SII EEPROM by register reading and writing.
// This code snippets has the same functionality with the below code snippet:
//
// DWORD dataEEPROM = 0;
// WaitForCommand(3, TRUE, ReadSlaveEEprom(0, 0X1C, &dataEEPROM));
//
// Please read the ESC documents of your devices for more information.
//
BYTE data[6] = { 0 };
BYTE dataRead[4] = { 0 };
data[0] = 0; // Write Disabled
data[1] = 1; // Read
data[2] = 0x1C & 0xFF;
data[3] = (0x1C / 0x100) & 0xFF;
data[4] = (0x1C / 0x10000) & 0xFF;
data[5] = (0x1C / 0x1000000) & 0xFF;
Command = WaitForCommand(1, TRUE, WriteSlaveRegister(0, 0x502, 6, data)); // 0x502 EEPROM Control/Status
if (Command.Error || Command.ErrorId != KsError::errNoError)
{
RtPrintf("Failed to write slave register: 0x%x\n", Command.ErrorId);
}
else
{
RtPrintf("Register successfully written\n");
}
Sleep(100);
Command = WaitForCommand(1, TRUE, ReadSlaveRegister(0, 0x508, 4, dataRead)); // 0x508 EEPROM Data
if (Command.Error || Command.ErrorId != KsError::errNoError)
{
RtPrintf("Failed to write slave register: 0x%x\n", Command.ErrorId);
}
else
{
RtPrintf("Register successfully read\n");
}
Requirements
| RT | Win32 | |
|---|---|---|
| Minimum supported version | 4.0 | 4.0 |
| Header | ksapi.h | ksapi.h |
| Library | KsApi_Rtss.lib | KsApi.lib |
See also