Problem with reading I2C

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Re: Problem with reading I2C

Postby matejdro » Wed Mar 28, 2012 8:02 am

New firmware works! Thank you.

I had some problems with I2CTransaction (returning always 0), while getData currently works.
matejdro
Novice
 
Posts: 54
Joined: Wed Mar 14, 2012 9:10 am

Re: Problem with reading I2C

Postby skoehler » Wed Mar 28, 2012 9:51 am

matejdro wrote:I had some problems with I2CTransaction (returning always 0), while getData currently works.

getData is based on i2cTransaction. There's no reaosn to prefer the former over the latter.
skoehler
leJOS Team Member
 
Posts: 1421
Joined: Thu Oct 30, 2008 4:54 pm

Re: Problem with reading I2C

Postby gloomyandy » Wed Mar 28, 2012 10:11 am

Hi Sven.
There are cases when getData is not suitable.... getData assumes that the read operation on a device requires that a register address needs to be sent (there is no version of getData without a register parameter), so it will always perform a 1 byte write of this address to the device before issuing a read. If the device does not follow this convention and simply needs a read request then you need to use i2cTransaction, similarly some devices require a multiple byte register address which again will require i2cTransaction. getData is fine for most Lego sensors, but my understanding is that this home made sensor falls into the first category and does not require that a read be preceded by a write of the register address to be read, in which case i2cTransfer may be a better option...

As to i2cTransaction returning zero, I'm rather puzzled by that. I can't really see any case in which this can happen (assuming the read parameters are correct), either the read will fail (in which case you get a negative status code), or it works (in which case you get the length of the read returned). There is really no way for an i2c device to not provide the number of bytes requested other than by issuing an error...


Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4072
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Problem with reading I2C

Postby matejdro » Wed Mar 28, 2012 10:39 am

I2ctransaction works works as long as I put setAddress() before it.
matejdro
Novice
 
Posts: 54
Joined: Wed Mar 14, 2012 9:10 am

Re: Problem with reading I2C

Postby skoehler » Wed Mar 28, 2012 11:24 am

gloomyandy wrote:Hi Sven.
There are cases when getData is not suitable.... getData assumes that the read operation on a device requires that a register address needs to be sent (there is no version of getData without a register parameter), so it will always perform a 1 byte write of this address to the device before issuing a read.


Yes, I read what you posted further up in this thread. I just wanted to emphasize, that is irrational to say that getData() works while i2cTransaction is broken, since the former is based on the latter (unless sending that one byte is somehow improveing things).
skoehler
leJOS Team Member
 
Posts: 1421
Joined: Thu Oct 30, 2008 4:54 pm

Re: Problem with reading I2C

Postby matejdro » Wed Mar 28, 2012 1:25 pm

As to i2cTransaction returning zero, I'm rather puzzled by that. I can't really see any case in which this can happen (assuming the read parameters are correct), either the read will fail (in which case you get a negative status code), or it works (in which case you get the length of the read returned). There is really no way for an i2c device to not provide the number of bytes requested other than by issuing an error...


Oh, I wrote it wrong. All bytes from i2ctransaction are zero and it returns -1. I can fix that by adding setAddress() before i2Ctransaction.
matejdro
Novice
 
Posts: 54
Joined: Wed Mar 14, 2012 9:10 am

Previous

Return to NXJ Software

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

more stuff