#11 Numout Behaviour

NXC (53)

Hope it is ok to post this here !

I am testing a custom I2C device in NXT. It tested ok with RobotC and am now testing with NXC.

Code below reads two values from device with different ranges (num is 0-8) and (max is 0-256) and displays on screen. The behaviour is unusual and does not seem to be related to I2C device.

Values displayed one separate lines by NumOut command are the same number even though the actual values are different with different ranges.
Plus the number that is displayed essentially twice (even though it is a different variable) seems to flip between one variable and the other periodically.

It as if there is some mix up in pointers in the NumOut statement but I suspect it may have something to do with I2CBytes command.

Just wondering if there is a known bug of this nature.
I have loaded the Enhanced Firmware by John Hansen and am running the latest NXC

Regards Brett

#include "NXCDefs.h"

byte NumC0[] = {0xC0, 0x10};
byte MaxC0[] = {0xC0, 0x12};

task main()

byte num[];
byte max[];
byte count;
bool result;
string msg;

SetSensorMode(S1, SENSOR_MODE_RAW);

TextOut(0, LCD_LINE2, "test1");

while (true) {
count = 1;
I2CBytes(S1, NumC0, count, num);
TextOut(0, LCD_LINE3, "num C0: ");
NumOut(50, LCD_LINE3, num[0]);
count = 1;
I2CBytes(S1, MaxC0, count, max);
TextOut(0, LCD_LINE5, "max C0: ");
NumOut(50, LCD_LINE5, max[0]);



  • John Hansen

    John Hansen - 2007-05-01

    Logged In: YES
    Originator: NO

    What I think you are seeing is that the I2CBytes routine returns the "last good read" if any sort of error occurs while it is trying to read from the I2C sensor (and, by "read", I mean write, check status, read). Since it returns the last good read it is possible for the value that you get in the first call to be the value actually read in the second call (and vice versa). I need to change I2CBytes so that it returns an error condition if anything goes wrong during its attempt to read the sensor.

    Can you try to get the sensor working using the LowSpeedWrite, LowSpeedStatus, and LowSpeedRead functions?


  • John Hansen

    John Hansen - 2007-05-04

    Logged In: YES
    Originator: NO

    In beta 29 of NBC/NXC I have changed the I2CBytes function so that it returns false if any error occurs during the read attempt. It will still return the last good read in most cases (depending on exactly where the error occurs) but you will be able to tell by checking the return value of the function call whether you should use that value or not. For your purposes you may want to consider using the low level LowspeedRead, LowspeedWrite, and LowspeedStatus functions. Also in beta 29 all the I2C functions are now safe to use across multiple threads simultaneously. Also I have added I2CRead, I2CWrite, and I2CStatus as aliases for the aforementioned Lowspeed* functions.

  • John Hansen

    John Hansen - 2007-05-04
    • status: open --> closed-fixed
  • Nobody/Anonymous

    Logged In: NO

    Thanks John,

    I did do some tests with LowSpeedWrite,LowSpeedStatus, and LowSpeedRead before I used I2CBytes and was checking status. However, I only borrowed a NXT for testing and have returned it now. So I need to see if I can source another to continue testing. As I said we had no problems with RobotC or NXT-G but I really wanted to used NXC as we are using NQC for connection to RCX.

    I did go through the macro in NXCdefs.h and NXTdefs.h so I can see what is going on.

    I will try to source an NXT so I can do more testing and let you know how I get on !

    Thanks again

  • brett

    brett - 2007-05-15
    • status: closed-fixed --> open-fixed
  • brett

    brett - 2007-05-15

    Logged In: YES
    Originator: YES

    Hope it was ok to re-open this one so I can discuss with you what I am seeing with our I2C sensor.

    I have now procured my our NXT for testing so I can go back to my NXC testing.

    Have relooked at the LowSpeedWrite, LowSpeedStatus, and LowSpeedRead as you suggested
    and I am pretty sure you were right about the last good value. What I have found
    is that I am getting an error result on checking status every second write/status/read.
    The error result is 221. I am pretty sure it is my problem, to help me track it down can you let me know what that error might mean.

    Thanks for your help

  • John Hansen

    John Hansen - 2007-08-22
    • status: open-fixed --> closed-fixed

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks