Click here to Skip to main content
15,912,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to communicate with CANBUS using FTDI(Serial Port Converter) through OBD Connector. I need to just send OBD-PID commands through SerialPort.

https://i.stack.imgur.com/7NNok.png[^]

What I have tried:

I tried to use FTD2XX_NET Library, with following code :

UInt32 ftdiDeviceCount = 0;
    FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

    // Create new instance of the FTDI device class
    FTDI myFtdiDevice = new FTDI();

    // Determine the number of FTDI devices connected to the machine
    ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
    // Check status
    if (ftStatus == FTDI.FT_STATUS.FT_OK)
    {
        Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString());
        Console.WriteLine("");
    }
    else
    {
        // Wait for a key press
        Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
        Console.ReadKey();
        return;
    }

    // If no devices available, return
    if (ftdiDeviceCount == 0)
    {
        // Wait for a key press
        Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
   
        return;
    }

    // Allocate storage for device info list
    FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

    // Populate our device list
    ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);

    if (ftStatus == FTDI.FT_STATUS.FT_OK)
    {
        for (UInt32 i = 0; i < ftdiDeviceCount; i++)
        {
            Console.WriteLine("Device Index: " + i.ToString());
            Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
            Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString());
            Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
            Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
            Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
            Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString());
            Console.WriteLine("");
        }
    }


    // Open first device in our list by serial number
    ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
    if (ftStatus != FTDI.FT_STATUS.FT_OK)
    {
        // Wait for a key press
        Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
        Console.ReadKey();
        return;
    }



    // Perform loop back - make sure loop back connector is fitted to the device
    // Write string data to the device
    byte[] dataToWrite = new byte[] { 0x00, 0x00, 0x07, 0xdf, 0x09, 0x02 };
    UInt32 numBytesWritten = 0;
    // Note that the Write method is overloaded, so can write string or byte array data

    ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
    if (ftStatus != FTDI.FT_STATUS.FT_OK)
    {
        // Wait for a key press
        Console.WriteLine("Failed to write to device (error " + ftStatus.ToString() + ")");
        Console.ReadKey();
        return;
    }


    // Check the amount of data available to read
    // In this case we know how much data we are expecting, 
    // so wait until we have all of the bytes we have sent.
    UInt32 numBytesAvailable = 0;
    UInt32 numBytesRead = 0;

    string readData = "";
    while (readData == "")
    {
        ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);
    }
    if (ftStatus != FTDI.FT_STATUS.FT_OK)
    {
        // Wait for a key press
        Console.WriteLine("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
        Console.ReadKey();
        return;
    }


    // Close our device
    ftStatus = myFtdiDevice.Close();

    // Wait for a key press
    Console.WriteLine(readData + " Finished.");



This code suppsoes to send (
0x00, 0x00, 0x07, 0xdf, 0x09, 0x02 
) which returns VIN, but the connector does not seem to send and not getting any response from the OBD.

I am not sure what am I doing wrong, I would appreciate your help.

Thanks,
Posted
Comments
The Other John Ingram 13-Dec-20 19:35pm    
what is the baud rate of the serial port?
Could you not be waiting long enough and it is timing out?
Member 12300005 13-Dec-20 19:42pm    
I tried all baud rates, and for sure it is not timing out, as there is a led on the connector that indicatres either sending or reciving data, which does not turn on during the execution of the code
The Other John Ingram 13-Dec-20 21:01pm    
have you checked the pin outs on the serial cable and the device your talking to.
Make sure the transmit on one side is connected to the receive on the other.
Member 12300005 14-Dec-20 3:19am    
The cerial cable has its own software where it can sends and reads, what I am doing now I am just trying to make another version on C#.
[no name] 14-Dec-20 1:04am    
Are you also verifying with a hand held / other device or just flying blind?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900