Society of Robots - Robot Forum

Software => Software => Topic started by: yukolin on July 24, 2008, 02:43:06 AM

Title: VC++ programming for I2C bus and SRF08
Post by: yukolin on July 24, 2008, 02:43:06 AM

Can someone help me to get the reading from SRF08 sonar by using VC++ programming? I also need the DLL files(library code)...

Now i have a RF04 telemetry module connected with USB port on PC... and with its companion CM02 radio communication module, form a complete interface between PC and I2C module.

The SRF08 sonar is connected on I2C bus... And what I am going to do is to get the range reading of this SRF08 sonar by the Visual C++...

My C++ is very weak, i dont know how to write this C++ programming... So can somebody kindly share me with your experience?
another important thing is the library code of I2C bus and SRF08 sonar.... I can't find it from net...

Thanks for everyone!!!
Title: Re: VC++ programming for I2C bus and SRF08
Post by: Admin on July 26, 2008, 06:21:18 PM
I'm not entirely clear on your hardware setup. How is the sonar connected to your PC?
Title: Re: VC++ programming for I2C bus and SRF08
Post by: izua on July 26, 2008, 07:11:02 PM
According to CM02 - Radio Communications Module Technical Specification, all CM02 does is interface between i2c and radio. you need RF04 to connect to the radio thing (which i understand you have).
So, technically, you can connect to i2c via the radio link (although i'm not sure how exactly its done) and connect to other i2c devices from your computer, i2c abstracted over the radio link, abstracted again, over usb (which in turns appears just as a com port).

tbh all these layers of abstraction mean a lot of headache. you'll have to experiment with getting each module to work - first check the usb communication, then check if it works as a com port, then check the radio link, then, at last, send adequate commands to the radio-i2c interface, to get reply from an i2c device.

now, since it looks like a serial port, there might be some libs out there, but i doubt it, unless the manufacturer gave you some. so, with the risk of beeing a bearer of bad news, you're on your own. which isn't a very simple thing to do, considering you have no idea of C++. However, I assume you know another language, since most of them can access a COM port (and that would take vc++ out of the problem)
Title: Re: VC++ programming for I2C bus and SRF08
Post by: yukolin on August 08, 2008, 02:00:59 AM
I have come out the programming, but now new problems encountered: the programming can anyhow run no matter whether i connect the RF04 to my PC or not!! It has ouput "101cm" always and even i moved the obstacle it will still "101cm"!!! It seems like my hardware has nothing to do with this programming.... (the programming has no error).
The following code is my programming, can anyone of u kindly go through it and help me find out the problem and give me some idea??

#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <string>


#define I2C_CMD 0x55 // direct I2C control command
#define CM02_CMD 0x5a // CM02 command

BYTE SerialBuf[200];
BYTE BackData=0, Battery, Compass;
void SetupCommPort(LPCTSTR comport);
void ConvertData(void);
void Clean_Screen(void);

int main(void)
int k;

SerialBuf[0] = I2C_CMD; // send SRF08 setup command
SerialBuf[1] = 0xE0;
SerialBuf[2] = 0x00;
SerialBuf[3] = 0x01;
SerialBuf[4] = 0x51;

WriteFile(hCom, &SerialBuf, 5, &n, NULL);
ReadFile(hCom, &SerialBuf, 1, &n, NULL);

SerialBuf[0] = CM02_CMD; // send SCAN8 command
SerialBuf[1] = SCAN1;
SerialBuf[2] = 0;
SerialBuf[3] = 0;
WriteFile(hCom, &SerialBuf, 4, &n, NULL);

ReadFile(hCom, &SerialBuf, 6, &n, NULL);


printf("%d cm \n", BackData);
return 1;

void SetupCommPort(LPCTSTR comport)
DCB dcb;

hCom = CreateFile(comport, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
GetCommState(hCom, &dcb);
dcb.BaudRate = CBR_19200;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = FALSE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
SetCommState(hCom, &dcb);

GetCommTimeouts(hCom, &ct);
ct.ReadIntervalTimeout = 500;
ct.ReadTotalTimeoutMultiplier =500;
ct.ReadTotalTimeoutConstant = 500;
SetCommTimeouts(hCom, &ct);

SetCommMask(hCom, EV_RXCHAR);

void ConvertData(void)
int x=0;
x = (SerialBuf[4]<<8) + SerialBuf[5]; // range in uS
x /= 58; // convert to cm
if(x==0) x=600; // make max if no echo
BackData = x;


void Clean_Screen(void)



Thanks u all very much and hope someone will help me with this!!!
Title: Re: VC++ programming for I2C bus and SRF08
Post by: izua on August 08, 2008, 02:35:56 AM
You have no guarantee that your hardware setup is good.
You get the same value because of the way i2c works.

Until you will test ALL the layers, as I have explained you above, starting to write a program is a bad thing. But since you already did it, now you can test everyhing, and you'll eventually find out why it isn't working.
Title: Re: VC++ programming for I2C bus and SRF08
Post by: popayseaman on August 08, 2008, 01:55:18 PM
have you tried this page
Title: Re: VC++ programming for I2C bus and SRF08
Post by: yukolin on August 10, 2008, 12:30:36 PM
Thanks for all you guys help.

Actually, i am quite sure my hardware setup is correct. And about this project, i have no choice but must complete it in VC++ according to the requirement.

Do you all think there is anything wrong with my ComPort setup part and the serialbuf part? Or i got omit something? Or i must write some command to open up the commport first before i download the programming into it? I really confuse about these 2 portions and i cant find out what extactly is the problem.

Looking forwards for your replies and i really thank you all very much!!!