Unable to Write to PR33-17 with Visual Studio

I’m trying to write to a non-NCD I2C Device via PR33-17 using Visual Studio. Hardware is set up and works properly. I can read and write using ANYI2C software, and can monitor the I2C Bus with an oscilloscope and see all communication is fine.

As soon as I switch from ANYI2C software to C-code in Visual Studio, without touching the hardware setup or oscilloscope, I don’t see any activity whatsoever on the I2C bus. Clearly the ANYI2C software is saying something different to the FTDI Chip than my C code is saying to the FTDI chip, but I can’t find documentation that says what that should be.

Here’s my C code, which runs without errors (other than no I2C bus activity):

/* LIDAR Reader.c - Writes to and Reads from Garmin I2C LIDAR via NCD USB-I2C Converter (PR33-17) */

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

int main()
{
unsigned char data[16];
FT_HANDLE handle;
DWORD bytes;

puts("Sending Data to USB Port");

/* Open device */
if (FT_Open(0, &handle) != FT_OK) {
    puts("Can't open device");
    return 1;
}

/* Set Bit Bang Mode */
if (FT_SetBitMode(handle, 0xff, 1) != FT_OK) puts("Can't Set Bit Bang Mode");

/* Set Baud Rate */
if(FT_SetBaudRate(handle, 9600) != FT_OK) puts("Can't Set Baud Rate");  /* Actually 16 * 9600 */

/* Create NCD API Stream */
data[0] = 0xAA; /* NCD API Header Byte */
data[1] = 0x04; /* NCD API Byte Count */
data[2] = 0xBE; /* NCD API Write Command */
data[3] = 0x62; /* 7-bit Device Address for Write */
data[4] = 0x00; /* Register Address for Begin Measure Command */
data[5] = 0x04; /* Measure in Auto Mode */
data[6] = 0xD2; /* Checksum */

/* Write to USB Port */
if ((FT_Write(handle, data, 7, &bytes) == FT_OK) && (bytes == 7)) puts("Successful write.");
else puts("Data not sent.");

}

Any ideas?

your baudrate is wrong
its should be 115200

Thank you for the response.

Can you clarify what the command should be? Per FTDI:

I changed the code to:

if(FT_SetBaudRate(handle, 7200) != FT_OK) puts(“Can’t Set Baud Rate”); /* Actually 16 * 7200 = 115200 */

No change.

I changed the code to:

if(FT_SetBaudRate(handle, 115200) != FT_OK) puts(“Can’t Set Baud Rate”); /* Actually 115200 */

No change.

It would seem to me that access to your device via Microsoft C would be one of the most basic things that folks will want to do. Do you not have a sample of Visual C code that actually works?

Thank you again.

Please download Alpha Station at https://ncd.io/alpha
Please use an older version (go back to version 1.0.2.3) from the download page as the latest version of Alpha needs some testing on this and a few other devices.
From here you can use the I2C Scan function to at least see if your device is detected.
Source code is included, but more importantly, please review the documentation provided below, as a basic 2-way communications test should be successfully completed prior to I2C communications just to validate your software is communicating. Be sure to set the baud rate in your C software, don’t do this at the FTDI driver level as this will have zero effect and will not fix your communication problems.
https://ncd.io/serial-to-i2c-conversion/
Thanks,
Ryan

[Mostly Solved]

The most important thing you said was “don’t do this at the FTDI driver level as this will have zero effect and will not fix your communication problems.” Prior to your response I ditched all the FTDI functions and replaced them with CreateFileA(), SetCommState(), WriteFile(), etc. and data now reliably comes out the USB port. It seems there may be more than one aspect of FTDI drivers that do not work with Windows 10/Visual Studio C.

I haven’t successfully communicated with the Serial/I2C Converter yet, but as long as I can send data out USB, I’m confident I will be able to adjust the data to get the Converter to respond. I will try out your Alpha suggestion to improve my code.