C# USB to I2C Issues

I am trying to communicate to a relay (PR13-30) using a USB to I2C converter (PR33-17). Following the Serial to I2C conversion guide (https://ncd.io/serial-to-i2c-conversion/) I am trying to send the first I2C command and receive the correct response. My code is in C# and I’m using the serial port class (https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport?view=dotnet-plat-ext-5.0). When I transmit the data I do not seem to receive a response, as the byte array I receive has a length of zero. My code is as follows:

public void test()
toolPort.Write(new byte[] { 0xAA, 0x02, 0xFE, 0x21, 0xCB },0 ,5);
int bytes = toolPort.BytesToRead;
byte[] buffer = new byte[bytes];
toolPort.Read(buffer, 0, bytes);

Any Idea why this isn’t working?


To simplify things to start I highly recommend using a terminal program to manually write commands and read responses back from the board. You can use something like Comm Operator here for that which is free for 30 days:

It actually has support for our API packet so it will add the AA, 0x02 and the checksum onto the end for you, just select NCD API. That will simplify your life quite a bit getting the commands down.

Please also be sure to post a photo of your hardware setup so we can see exactly how everything is put together.

@TravisE_NCD_Technica Here is my setup. The current sensor is connected but not in use and the relay is triggering an led. Power is supplied from the computer via a second usb.

I think I understand the NCD API structure but I am struggling to communicate with the usb converter and the relay. I will test using the Comm operator.

You might take a look at the samples provided in Alpha Station - Visual Basic (https://ncd.io/alpha), the source code handles all the encoding/decoding for you and uses simple I2C write commands, this might be the best place to start. We use our own libraries to communicate to our devices, they should work in VS C#.

@ryan1 I have tried to convert the NCDLib to a .DLL file so that I could call the alpha station functions in my c# program. When I tried to compile I always received many build errors. Is there another way to reference the NCDLib functions?

I have been successful in writing command to turn the relay on and off within my code, I am currently trying to read the current sensor output.

The NCDLib was written by another engineer that is no-longer with our company, so I am afraid I cannot provide any low-level support. Alpha Station does have complete support for current monitoring, it might help as a reference on how to parse data from the current sensor. Hope this helps.