Society of Robots - Robot Forum

Software => Software => Topic started by: Reznoir on June 26, 2010, 10:36:59 AM

Title: C# and SRF08 Ultrasonic sensor?
Post by: Reznoir on June 26, 2010, 10:36:59 AM
Hello,

I am trying to write a program in C# that can read the distance from an SRF08 ultrasonic sensor,
I know this shouldn't be too difficult, but I need help.

The SRF08 is connected to a USB-I2C module and communicates via rs232.

I have attached my code below. I think the problem is something
to do with the values actually sent to the SRF08, but im not sure.

Any help would be most appreciated.

Thanks

Code: [Select]

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;


namespace ConsoleApplication1
{
    class Program
    {
        const byte I2CD_CMD = 0x53;      // direct I2C control command
        const byte I2C_CMD  = 0x55;         // registered I2C control command
        const byte CM01_CMD = 0x5A;      // CM01 command

        byte device_addr = 0xE0;

        byte[] sbuf;

        private System.IO.Ports.SerialPort port;


        public void run()
        {
            device_addr = 0xE0;

            sbuf = new byte[100];
            setupCommPort("COM3");                  // set up Com Port

                                                               
            sbuf[0] = I2C_CMD;                         // send sonar rangeing in cm
            sbuf[1] = device_addr;                     // SRF08 address
            sbuf[2] = 0x00;
            sbuf[3] = 0x01;
            sbuf[4] = 0x51;                              // set to cm
            port.Write(sbuf, 0, 5);
            port.Read(sbuf, 0, 1);

            sbuf[0] = I2C_CMD;                         // send sonar read command
            sbuf[1] = 0xE1;                              // SRF08 address + 1 read bit
            sbuf[2] = 0x00;
            sbuf[3] = 0x04;
            port.Write(sbuf, 0, 4);

            Thread.Sleep(70);                          // delay before read
            port.Read(sbuf, 0, 12);

            Console.Write(sbuf[0]);                  // write output

            Console.Read();
        }
        
        static void Main(string[] args)
        {
            Program prog = new Program();
            prog.run();
        }

        private void setupCommPort(String comport)
        {
            port = new SerialPort();
            this.port.BaudRate = 19200;
            this.port.Parity = Parity.None;
            this.port.StopBits = StopBits.Two;
            this.port.Handshake = Handshake.None;
            this.port.RtsEnable = false;
            this.port.ReadTimeout = 500;
            this.port.WriteTimeout = 500;
            port.PortName = comport;
            port.Open();
            
            if(port.IsOpen)
            {
                Console.WriteLine("Com Port 3 setup");
            }
        }
    }
}
Title: Re: C# and SRF08 Ultrasonic sensor?
Post by: zuppaman on November 23, 2010, 04:30:32 AM
Hey Reznoir,


Any chance that you fixed this problem because I've spend some days looking at it with out any result.

Did you found a solution by now?


Thx
Kris