Added interface to read/write register contents; re-worked register requests.

This commit is contained in:
Florian Pose
2012-11-14 22:12:57 +01:00
parent 139168474d
commit 4773e88bf6
25 changed files with 1220 additions and 325 deletions

View File

@@ -2,7 +2,7 @@
*
* $Id$
*
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH
* Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
@@ -48,20 +48,20 @@ string CommandRegRead::helpString(const string &binaryBaseName) const
stringstream str;
str << binaryBaseName << " " << getName()
<< " [OPTIONS] <OFFSET> [LENGTH]" << endl
<< " [OPTIONS] <ADDRESS> [SIZE]" << endl
<< endl
<< getBriefDescription() << endl
<< endl
<< "This command requires a single slave to be selected." << endl
<< endl
<< "Arguments:" << endl
<< " OFFSET is the register address. Must" << endl
<< " be an unsigned 16 bit number." << endl
<< " LENGTH is the number of bytes to read and must also be" << endl
<< " an unsigned 16 bit number. OFFSET plus LENGTH" << endl
<< " may not exceed 64k. The length is ignored (and" << endl
<< " can be omitted), if a selected data type" << endl
<< " implies a length." << endl
<< " ADDRESS is the register address. Must" << endl
<< " be an unsigned 16 bit number." << endl
<< " SIZE is the number of bytes to read and must also be" << endl
<< " an unsigned 16 bit number. ADDRESS plus SIZE" << endl
<< " may not exceed 64k. The size is ignored (and" << endl
<< " can be omitted), if a selected data type" << endl
<< " implies a size." << endl
<< endl
<< typeInfo()
<< endl
@@ -81,7 +81,7 @@ string CommandRegRead::helpString(const string &binaryBaseName) const
void CommandRegRead::execute(const StringVector &args)
{
SlaveList slaves;
ec_ioctl_slave_reg_t data;
ec_ioctl_slave_reg_t io;
stringstream strOffset, err;
const DataType *dataType = NULL;
@@ -93,9 +93,9 @@ void CommandRegRead::execute(const StringVector &args)
strOffset << args[0];
strOffset
>> resetiosflags(ios::basefield) // guess base from prefix
>> data.offset;
>> io.address;
if (strOffset.fail()) {
err << "Invalid offset '" << args[0] << "'!";
err << "Invalid address '" << args[0] << "'!";
throwInvalidUsageException(err);
}
@@ -104,18 +104,18 @@ void CommandRegRead::execute(const StringVector &args)
strLength << args[1];
strLength
>> resetiosflags(ios::basefield) // guess base from prefix
>> data.length;
>> io.size;
if (strLength.fail()) {
err << "Invalid length '" << args[1] << "'!";
err << "Invalid size '" << args[1] << "'!";
throwInvalidUsageException(err);
}
if (!data.length) {
if (!io.size) {
err << "Length may not be zero!";
throwInvalidUsageException(err);
}
} else { // no length argument given
data.length = 0;
} else { // no size argument given
io.size = 0;
}
if (!getDataType().empty()) {
@@ -125,19 +125,19 @@ void CommandRegRead::execute(const StringVector &args)
}
if (dataType->byteSize) {
// override length argument
data.length = dataType->byteSize;
// override size argument
io.size = dataType->byteSize;
}
}
if (!data.length) {
err << "The length argument is mandatory, if no datatype is " << endl
<< "specified, or the datatype does not imply a length!";
if (!io.size) {
err << "The size argument is mandatory, if no datatype is " << endl
<< "specified, or the datatype does not imply a size!";
throwInvalidUsageException(err);
}
if ((uint32_t) data.offset + data.length > 0xffff) {
err << "Offset and length exceeding 64k!";
if ((uint32_t) io.address + io.size > 0xffff) {
err << "Address and size exceeding 64k!";
throwInvalidUsageException(err);
}
@@ -148,25 +148,25 @@ void CommandRegRead::execute(const StringVector &args)
if (slaves.size() != 1) {
throwSingleSlaveRequired(slaves.size());
}
data.slave_position = slaves.front().position;
io.slave_position = slaves.front().position;
data.data = new uint8_t[data.length];
io.data = new uint8_t[io.size];
try {
m.readReg(&data);
m.readReg(&io);
} catch (MasterDeviceException &e) {
delete [] data.data;
delete [] io.data;
throw e;
}
try {
outputData(cout, dataType, data.data, data.length);
outputData(cout, dataType, io.data, io.size);
} catch (SizeException &e) {
delete [] data.data;
delete [] io.data;
throwCommandException(e.what());
}
delete [] data.data;
delete [] io.data;
}
/*****************************************************************************/

View File

@@ -2,7 +2,7 @@
*
* $Id$
*
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH
* Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
@@ -57,7 +57,7 @@ string CommandRegWrite::helpString(const string &binaryBaseName) const
<< "This command requires a single slave to be selected." << endl
<< endl
<< "Arguments:" << endl
<< " OFFSET is the register address to write to." << endl
<< " ADDRESS is the register address to write to." << endl
<< " DATA depends on whether a datatype was specified" << endl
<< " with the --type option: If not, DATA must be" << endl
<< " either a path to a file with data to write," << endl
@@ -83,7 +83,7 @@ string CommandRegWrite::helpString(const string &binaryBaseName) const
void CommandRegWrite::execute(const StringVector &args)
{
stringstream strOffset, err;
ec_ioctl_slave_reg_t data;
ec_ioctl_slave_reg_t io;
ifstream file;
SlaveList slaves;
@@ -95,22 +95,22 @@ void CommandRegWrite::execute(const StringVector &args)
strOffset << args[0];
strOffset
>> resetiosflags(ios::basefield) // guess base from prefix
>> data.offset;
>> io.address;
if (strOffset.fail()) {
err << "Invalid offset '" << args[0] << "'!";
err << "Invalid address '" << args[0] << "'!";
throwInvalidUsageException(err);
}
if (getDataType().empty()) {
if (args[1] == "-") {
loadRegData(&data, cin);
loadRegData(&io, cin);
} else {
file.open(args[1].c_str(), ifstream::in | ifstream::binary);
if (file.fail()) {
err << "Failed to open '" << args[1] << "'!";
throwCommandException(err);
}
loadRegData(&data, file);
loadRegData(&io, file);
file.close();
}
} else {
@@ -123,30 +123,30 @@ void CommandRegWrite::execute(const StringVector &args)
}
if (dataType->byteSize) {
data.length = dataType->byteSize;
io.size = dataType->byteSize;
} else {
data.length = 1024; // FIXME
io.size = 1024; // FIXME
}
data.data = new uint8_t[data.length];
io.data = new uint8_t[io.size];
try {
data.length = interpretAsType(
dataType, args[1], data.data, data.length);
io.size = interpretAsType(
dataType, args[1], io.data, io.size);
} catch (SizeException &e) {
delete [] data.data;
delete [] io.data;
throwCommandException(e.what());
} catch (ios::failure &e) {
delete [] data.data;
delete [] io.data;
err << "Invalid value argument '" << args[1]
<< "' for type '" << dataType->name << "'!";
throwInvalidUsageException(err);
}
}
if ((uint32_t) data.offset + data.length > 0xffff) {
err << "Offset and length exceeding 64k!";
delete [] data.data;
if ((uint32_t) io.address + io.size > 0xffff) {
err << "Address and size exceeding 64k!";
delete [] io.data;
throwInvalidUsageException(err);
}
@@ -154,22 +154,22 @@ void CommandRegWrite::execute(const StringVector &args)
try {
m.open(MasterDevice::ReadWrite);
} catch (MasterDeviceException &e) {
delete [] data.data;
delete [] io.data;
throw e;
}
slaves = selectedSlaves(m);
if (slaves.size() != 1) {
delete [] data.data;
delete [] io.data;
throwSingleSlaveRequired(slaves.size());
}
data.slave_position = slaves.front().position;
io.slave_position = slaves.front().position;
// send data to master
try {
m.writeReg(&data);
m.writeReg(&io);
} catch (MasterDeviceException &e) {
delete [] data.data;
delete [] io.data;
throw e;
}
@@ -177,13 +177,13 @@ void CommandRegWrite::execute(const StringVector &args)
cerr << "Register writing finished." << endl;
}
delete [] data.data;
delete [] io.data;
}
/*****************************************************************************/
void CommandRegWrite::loadRegData(
ec_ioctl_slave_reg_t *data,
ec_ioctl_slave_reg_t *io,
const istream &in
)
{
@@ -201,11 +201,11 @@ void CommandRegWrite::loadRegData(
err << "Invalid data size " << contents.size() << "!";
throwInvalidUsageException(err);
}
data->length = contents.size();
io->size = contents.size();
// allocate buffer and read file into buffer
data->data = new uint8_t[data->length];
contents.copy((char *) data->data, contents.size());
io->data = new uint8_t[io->size];
contents.copy((char *) io->data, contents.size());
}
/*****************************************************************************/