Writing a double to I2C device












1















When I try to use i2cset on my raspberryPI with mode for SMBus block data it is saying Error: Adapter does not have SMBus block write capability.



# sudo i2cset -y 3 0x20 0x01 0xDE 0xAD 0xC0 0xDE 0x00 0xFF s
Error: Adapter does not have SMBus block write capability


I looked at the WiringPi code and it has 2 functions to write data on I2C. The first uses byte data and the second a word data. It is missing the (SMBus block data) and the (I2C block data). So I suppose that RaspberryPi's do not support it.



I want to implement a C++ code that write a double to the i2c on my raspberryPi. To write an int I am already doing.



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00
0x1f


C++ code:



int I2CTools::write8(int value) {
union i2c_smbus_data data;

data.byte = value;

return i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, 0x00, I2C_SMBUS_BYTE_DATA, &data);
}
int I2CTools::read8() {

union i2c_smbus_data data;

if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, 0x00, I2C_SMBUS_BYTE_DATA, &data))
return -1;
else
return data.byte & 0xFF;
}


Now I suppose that I have to loop over the address like this:



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f ; sudo i2cset -y 3 0x20 0x01 0x85 ; sudo i2cset -y 3 0x20 0x02 0xeb ; sudo i2cset -y 3 0x20 0x03 0x51 ; sudo i2cset -y 3 0x20 0x04 0xb8 ; sudo i2cset -y 3 0x20 0x05 0x1e ; sudo i2cset -y 3 0x20 0x06 0x09 ; sudo i2cset -y 3 0x20 0x07 0x40
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00 ;sudo i2cget -y 3 0x20 0x01 ; sudo i2cget -y 3 0x20 0x02 ; sudo i2cget -y 3 0x20 0x03 ; sudo i2cget -y 3 0x20 0x04 ; sudo i2cget -y 3 0x20 0x05 ; sudo i2cget -y 3 0x20 0x06 ; sudo i2cget -y 3 0x20 0x07
0x1f
0x85
0xeb
0x51
0xb8
0x1e
0x09
0x40


But in C++ of course... Is this make sense for you? I am newbie with i2c devices and I suppose it is this direction that I have to do in my code. Thanks!



int I2CTools::writeDouble(double value) {
union i2c_smbus_data data;

doubleToCharArray.original = value;

std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
std::cout << (int) doubleToCharArray.output[address] << ' ';
data.byte = (int) doubleToCharArray.output[address];
// std::cout << hex(doubleToCharArray.output[address]) << ' ';
i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, address, I2C_SMBUS_BYTE_DATA, &data);
}
return 0;
}
double I2CTools::readDouble() {

unsigned char charArray[sizeof(double)];
std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
union i2c_smbus_data data;
if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, address, I2C_SMBUS_BYTE_DATA, &data)) {
return -1;
} else {
std::cout << (int) data.byte << ' ';
charArray[address] = data.byte;
}
}
return *reinterpret_cast<double *>(charArray);
}









share|improve this question























  • Have you tried using option "i" instead of "s"? You can also easily use bash or Python to implement it by calling i2cget directly

    – Adashi
    Nov 21 '18 at 9:58


















1















When I try to use i2cset on my raspberryPI with mode for SMBus block data it is saying Error: Adapter does not have SMBus block write capability.



# sudo i2cset -y 3 0x20 0x01 0xDE 0xAD 0xC0 0xDE 0x00 0xFF s
Error: Adapter does not have SMBus block write capability


I looked at the WiringPi code and it has 2 functions to write data on I2C. The first uses byte data and the second a word data. It is missing the (SMBus block data) and the (I2C block data). So I suppose that RaspberryPi's do not support it.



I want to implement a C++ code that write a double to the i2c on my raspberryPi. To write an int I am already doing.



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00
0x1f


C++ code:



int I2CTools::write8(int value) {
union i2c_smbus_data data;

data.byte = value;

return i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, 0x00, I2C_SMBUS_BYTE_DATA, &data);
}
int I2CTools::read8() {

union i2c_smbus_data data;

if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, 0x00, I2C_SMBUS_BYTE_DATA, &data))
return -1;
else
return data.byte & 0xFF;
}


Now I suppose that I have to loop over the address like this:



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f ; sudo i2cset -y 3 0x20 0x01 0x85 ; sudo i2cset -y 3 0x20 0x02 0xeb ; sudo i2cset -y 3 0x20 0x03 0x51 ; sudo i2cset -y 3 0x20 0x04 0xb8 ; sudo i2cset -y 3 0x20 0x05 0x1e ; sudo i2cset -y 3 0x20 0x06 0x09 ; sudo i2cset -y 3 0x20 0x07 0x40
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00 ;sudo i2cget -y 3 0x20 0x01 ; sudo i2cget -y 3 0x20 0x02 ; sudo i2cget -y 3 0x20 0x03 ; sudo i2cget -y 3 0x20 0x04 ; sudo i2cget -y 3 0x20 0x05 ; sudo i2cget -y 3 0x20 0x06 ; sudo i2cget -y 3 0x20 0x07
0x1f
0x85
0xeb
0x51
0xb8
0x1e
0x09
0x40


But in C++ of course... Is this make sense for you? I am newbie with i2c devices and I suppose it is this direction that I have to do in my code. Thanks!



int I2CTools::writeDouble(double value) {
union i2c_smbus_data data;

doubleToCharArray.original = value;

std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
std::cout << (int) doubleToCharArray.output[address] << ' ';
data.byte = (int) doubleToCharArray.output[address];
// std::cout << hex(doubleToCharArray.output[address]) << ' ';
i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, address, I2C_SMBUS_BYTE_DATA, &data);
}
return 0;
}
double I2CTools::readDouble() {

unsigned char charArray[sizeof(double)];
std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
union i2c_smbus_data data;
if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, address, I2C_SMBUS_BYTE_DATA, &data)) {
return -1;
} else {
std::cout << (int) data.byte << ' ';
charArray[address] = data.byte;
}
}
return *reinterpret_cast<double *>(charArray);
}









share|improve this question























  • Have you tried using option "i" instead of "s"? You can also easily use bash or Python to implement it by calling i2cget directly

    – Adashi
    Nov 21 '18 at 9:58
















1












1








1








When I try to use i2cset on my raspberryPI with mode for SMBus block data it is saying Error: Adapter does not have SMBus block write capability.



# sudo i2cset -y 3 0x20 0x01 0xDE 0xAD 0xC0 0xDE 0x00 0xFF s
Error: Adapter does not have SMBus block write capability


I looked at the WiringPi code and it has 2 functions to write data on I2C. The first uses byte data and the second a word data. It is missing the (SMBus block data) and the (I2C block data). So I suppose that RaspberryPi's do not support it.



I want to implement a C++ code that write a double to the i2c on my raspberryPi. To write an int I am already doing.



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00
0x1f


C++ code:



int I2CTools::write8(int value) {
union i2c_smbus_data data;

data.byte = value;

return i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, 0x00, I2C_SMBUS_BYTE_DATA, &data);
}
int I2CTools::read8() {

union i2c_smbus_data data;

if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, 0x00, I2C_SMBUS_BYTE_DATA, &data))
return -1;
else
return data.byte & 0xFF;
}


Now I suppose that I have to loop over the address like this:



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f ; sudo i2cset -y 3 0x20 0x01 0x85 ; sudo i2cset -y 3 0x20 0x02 0xeb ; sudo i2cset -y 3 0x20 0x03 0x51 ; sudo i2cset -y 3 0x20 0x04 0xb8 ; sudo i2cset -y 3 0x20 0x05 0x1e ; sudo i2cset -y 3 0x20 0x06 0x09 ; sudo i2cset -y 3 0x20 0x07 0x40
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00 ;sudo i2cget -y 3 0x20 0x01 ; sudo i2cget -y 3 0x20 0x02 ; sudo i2cget -y 3 0x20 0x03 ; sudo i2cget -y 3 0x20 0x04 ; sudo i2cget -y 3 0x20 0x05 ; sudo i2cget -y 3 0x20 0x06 ; sudo i2cget -y 3 0x20 0x07
0x1f
0x85
0xeb
0x51
0xb8
0x1e
0x09
0x40


But in C++ of course... Is this make sense for you? I am newbie with i2c devices and I suppose it is this direction that I have to do in my code. Thanks!



int I2CTools::writeDouble(double value) {
union i2c_smbus_data data;

doubleToCharArray.original = value;

std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
std::cout << (int) doubleToCharArray.output[address] << ' ';
data.byte = (int) doubleToCharArray.output[address];
// std::cout << hex(doubleToCharArray.output[address]) << ' ';
i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, address, I2C_SMBUS_BYTE_DATA, &data);
}
return 0;
}
double I2CTools::readDouble() {

unsigned char charArray[sizeof(double)];
std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
union i2c_smbus_data data;
if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, address, I2C_SMBUS_BYTE_DATA, &data)) {
return -1;
} else {
std::cout << (int) data.byte << ' ';
charArray[address] = data.byte;
}
}
return *reinterpret_cast<double *>(charArray);
}









share|improve this question














When I try to use i2cset on my raspberryPI with mode for SMBus block data it is saying Error: Adapter does not have SMBus block write capability.



# sudo i2cset -y 3 0x20 0x01 0xDE 0xAD 0xC0 0xDE 0x00 0xFF s
Error: Adapter does not have SMBus block write capability


I looked at the WiringPi code and it has 2 functions to write data on I2C. The first uses byte data and the second a word data. It is missing the (SMBus block data) and the (I2C block data). So I suppose that RaspberryPi's do not support it.



I want to implement a C++ code that write a double to the i2c on my raspberryPi. To write an int I am already doing.



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00
0x1f


C++ code:



int I2CTools::write8(int value) {
union i2c_smbus_data data;

data.byte = value;

return i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, 0x00, I2C_SMBUS_BYTE_DATA, &data);
}
int I2CTools::read8() {

union i2c_smbus_data data;

if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, 0x00, I2C_SMBUS_BYTE_DATA, &data))
return -1;
else
return data.byte & 0xFF;
}


Now I suppose that I have to loop over the address like this:



root@sense4:~/i2c-tests# sudo i2cset -y 3 0x20 0x00 0x1f ; sudo i2cset -y 3 0x20 0x01 0x85 ; sudo i2cset -y 3 0x20 0x02 0xeb ; sudo i2cset -y 3 0x20 0x03 0x51 ; sudo i2cset -y 3 0x20 0x04 0xb8 ; sudo i2cset -y 3 0x20 0x05 0x1e ; sudo i2cset -y 3 0x20 0x06 0x09 ; sudo i2cset -y 3 0x20 0x07 0x40
root@sense4:~/i2c-tests# sudo i2cget -y 3 0x20 0x00 ;sudo i2cget -y 3 0x20 0x01 ; sudo i2cget -y 3 0x20 0x02 ; sudo i2cget -y 3 0x20 0x03 ; sudo i2cget -y 3 0x20 0x04 ; sudo i2cget -y 3 0x20 0x05 ; sudo i2cget -y 3 0x20 0x06 ; sudo i2cget -y 3 0x20 0x07
0x1f
0x85
0xeb
0x51
0xb8
0x1e
0x09
0x40


But in C++ of course... Is this make sense for you? I am newbie with i2c devices and I suppose it is this direction that I have to do in my code. Thanks!



int I2CTools::writeDouble(double value) {
union i2c_smbus_data data;

doubleToCharArray.original = value;

std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
std::cout << (int) doubleToCharArray.output[address] << ' ';
data.byte = (int) doubleToCharArray.output[address];
// std::cout << hex(doubleToCharArray.output[address]) << ' ';
i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_WRITE, address, I2C_SMBUS_BYTE_DATA, &data);
}
return 0;
}
double I2CTools::readDouble() {

unsigned char charArray[sizeof(double)];
std::cout << "nBytes: ";
for (unsigned long address = 0; address < sizeof(double); address++) {
union i2c_smbus_data data;
if (i2c_smbus_access(i2cFileDescriptor, I2C_SMBUS_READ, address, I2C_SMBUS_BYTE_DATA, &data)) {
return -1;
} else {
std::cout << (int) data.byte << ' ';
charArray[address] = data.byte;
}
}
return *reinterpret_cast<double *>(charArray);
}






c++ raspberry-pi i2c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 11:18









Felipe Oliveira GutierrezFelipe Oliveira Gutierrez

98921228




98921228













  • Have you tried using option "i" instead of "s"? You can also easily use bash or Python to implement it by calling i2cget directly

    – Adashi
    Nov 21 '18 at 9:58





















  • Have you tried using option "i" instead of "s"? You can also easily use bash or Python to implement it by calling i2cget directly

    – Adashi
    Nov 21 '18 at 9:58



















Have you tried using option "i" instead of "s"? You can also easily use bash or Python to implement it by calling i2cget directly

– Adashi
Nov 21 '18 at 9:58







Have you tried using option "i" instead of "s"? You can also easily use bash or Python to implement it by calling i2cget directly

– Adashi
Nov 21 '18 at 9:58














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298980%2fwriting-a-double-to-i2c-device%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298980%2fwriting-a-double-to-i2c-device%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine