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;
FTDI myFtdiDevice = new FTDI();
ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
if (ftStatus == FTDI.FT_STATUS.FT_OK)
{
Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString());
Console.WriteLine("");
}
else
{
Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
Console.ReadKey();
return;
}
if (ftdiDeviceCount == 0)
{
Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
return;
}
FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
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("");
}
}
ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
Console.ReadKey();
return;
}
byte[] dataToWrite = new byte[] { 0x00, 0x00, 0x07, 0xdf, 0x09, 0x02 };
UInt32 numBytesWritten = 0;
ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
Console.WriteLine("Failed to write to device (error " + ftStatus.ToString() + ")");
Console.ReadKey();
return;
}
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)
{
Console.WriteLine("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
Console.ReadKey();
return;
}
ftStatus = myFtdiDevice.Close();
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,