The input parameters are Sensor Address and command.
The Sensor address is written in Hexadecimal on the Sensor.

 private int read_distance(int Sensor,byte cmd)
        {
 
            //byte cmd;
            byte addr_h,addr_m, addr_l;
            byte distance_h, distance_l;
            byte data=0;
            byte checksum;
            int tmp_checksum;
            int distance;
 
            addr_l = (byte)Sensor;
            Sensor = Sensor >> 8;
            addr_m = (byte)Sensor;
            Sensor = Sensor >> 8;
            addr_h = (byte)Sensor;
 
            tmp_checksum = ~(cmd + addr_h + addr_l + addr_m + data);
            checksum = (byte)tmp_checksum;
 
            // Break for 1ms
            serialPort1.BreakState = true;
            System.Threading.Thread.Sleep(1);
            serialPort1.BreakState = false;
            serialPort1.Write(new byte[6] { cmd, addr_h, addr_m, addr_l, data, checksum }, 0, 6);
 
            if (cmd == 94) //if reading command is given
            {
 
                distance_h = (byte)serialPort1.ReadByte();
                distance_l = (byte)serialPort1.ReadByte();
                distance = distance_h;
                distance = distance << 8;
                distance = distance | distance_l;
            }
            else
            {
                distance = 0;
            }
            return distance;
        }

First the distance needs to be measured.

read_distance(4643, 81);

After a waiting time of at least 70ms the measured distance can be read from the sensors.

System.Threading.Thread.Sleep(70);
sens1 = read_distance(4643, 94);

4 Responses to “SRF485 read distance C#”

  1. Charalambos says:

    Hi,
    Have been experimenting with one SRF485 for days now without any success.
    Using your code, we came up with the following program:

    class PortDataReceived
    {
    SerialPort serialPort1 = new SerialPort(“COM3″);
    Boolean port_is_open = false;

    private void connect_ultrasonic_rangers()
    {
    if (port_is_open)
    {
    serialPort1.Close();
    Console.WriteLine(“Closed serial port”);
    port_is_open = false;
    }
    else
    {
    try
    {
    serialPort1.BaudRate = 38400;
    serialPort1.StopBits = System.IO.Ports.StopBits.Two;
    //serialPort1.DataBits = 7;
    serialPort1.PortName = “COM3″;
    serialPort1.ReadTimeout = 500;
    serialPort1.Open();

    //serialPort1.DtrEnable = true;
    //serialPort1.RtsEnable = true;

    port_is_open = true;
    Console.WriteLine(“Serial port is now open”);
    }
    catch (Exception Ex)
    {
    Console.WriteLine(“Problem when attempting to open serial port”);
    }
    }
    }

    private void send_command(int Sensor, byte cmd, byte data)
    {
    byte addr_h, addr_m, addr_l;
    //byte data = 0;
    byte checksum;
    int tmp_checksum;
    addr_l = (byte)Sensor;
    Sensor = Sensor >> 8;
    addr_m = (byte)Sensor;
    Sensor = Sensor >> 8;
    addr_h = (byte)Sensor;

    tmp_checksum = ~(cmd + addr_h + addr_l + addr_m + data);
    checksum = (byte)tmp_checksum;

    Console.WriteLine(“Sending command: ” + cmd + “-” + addr_h+ “-“+ addr_m+ “-“+ addr_l+ “-“+ data+ “-“+ checksum);

    // Break for 1ms
    serialPort1.BreakState = true;
    System.Threading.Thread.Sleep(1);
    serialPort1.BreakState = false;
    serialPort1.Write(new byte[6] { cmd, addr_h, addr_m, addr_l, data, checksum }, 0, 6);
    Console.WriteLine(“To send: ” + serialPort1.BytesToWrite + ” ” + serialPort1.WriteBufferSize);
    }

    private int read_response()
    {
    byte reply=10;
    Console.WriteLine(“Reading response”);
    if (serialPort1.IsOpen)
    {
    if (serialPort1.BytesToRead > 0)
    {
    reply = (byte)serialPort1.ReadByte();
    Console.WriteLine(“Reading complete”);
    }
    else
    Console.WriteLine(“Serial port – no data available”);
    }
    else
    Console.WriteLine(“Serial port was not open”);
    return reply;
    }

    public static void Main(string[] args)
    {
    Console.WriteLine(“Starting application – press key”);
    Console.WriteLine();
    Console.ReadKey();
    PortDataReceived rcvr = new PortDataReceived();
    rcvr.connect_ultrasonic_rangers();
    rcvr.send_command(0, 101, 0);
    System.Threading.Thread.Sleep(100);
    rcvr.send_command(65535, 102, 0);
    System.Threading.Thread.Sleep(100);
    int r = rcvr.read_response();
    Console.WriteLine(“Response: ” + r);
    System.Threading.Thread.Sleep(100);

    rcvr.send_command(12744, 100, 1);
    System.Threading.Thread.Sleep(100);
    r = rcvr.read_response();
    Console.WriteLine(“Response: ” + r);
    System.Threading.Thread.Sleep(100);

    int d = rcvr.read_distance(12744,81,0);
    Console.WriteLine(“Distance read: ” + d);
    System.Threading.Thread.Sleep(100);
    d = rcvr.read_distance(12744,94,0);
    Console.WriteLine(“Distance read: ” + d);
    }

    Monitoring the serial port we can see the data going out but no data is
    ever received. The SRF is connected to the PC via a usb-to-rs232 cable
    and a rs232-rs485 converter.

    In some other serial port codes they recommend the use of the DataReceivedHandler
    but you don’t seem to utilise it in your working code.
    Do you have any kind of idea what might be wrong ?

    Thanks and sorry for the long comment.

  2. Christoph says:

    I added a sample program, reading 4 SRF485s and visualizing the measured data to the Downloads section of the Blog.

    I think the problem with your code is that you dont use the correct Sensor Address. The Address is written on your SRF485.

    rcvr.send_command(12744, 100, 1);
    Is 12744 the correct Sensor Address??

    I have also seen problems using USB converters, but with other applications where i was exchanging data with a microcontroller.

  3. Charalambos says:

    Thanks for the reply.

    We have seen the program. Taking it a step at a
    time we are first trying to talk to one sensor and
    then move on to more 😉

    The address written on the sensor is 31C8.
    We verified it when looking at what goes out on the
    serial port. We also tried sending out this ‘find sensors’
    command as defined on the robot electronics page
    to get some sort of reply but nothing.

    Thanks.

  4. Charalambos says:

    Hi,
    One more question:
    do you have a picture of the connections you made ?
    We used 4 wires: soldered 1 pin on 12v, 1 pin on ground
    and the 2 middle pins. Did we do the right thing or
    is the 10-pin connector a must ?

Leave a Reply