ADC reading not working


I’m working with the Dobot Magician, trying to read an analog signal on port 9. With DoBot Studio, it works without a problem. I expect an ADC of around 3000 and that’s what it shows.

dType.SetIOMultiplexing(api, 9, 4, isQueued=0)
varX = dType.GetIOADC(api, 9)


But I’m trying to get it working in C# using the DobotDll. I can get basically every other function and pin to work properly but not port 9 ADC reading. Here’s the code I’m using.

//Disable Gripper
DobotDll.SetEndEffectorGripper(false, false, false, ref cmdIndex);
//Set up Input A/D pin
IOMultiplexing ioMux;
ioMux.address = (byte)9;
ioMux.multiplex = (byte)IOFunction.IOFunctionADC;
DobotDll.SetIOMultiplexing(ref ioMux, false, ref cmdIndex);
//Read the pin
inputADC.address = (byte)9;
DobotDll.GetIOADC(ref inputADC);
txtBox.Text = inputADC.value.ToString();

The output of the C# program is 15. The ADC reading changes if I change the input voltage. But it only has a range of 0-15. Pulling the ADC pin to ground -> 0, Pulling it up to 3.3V -> 15.

Why is this not working?


I figured it out. In the DobotTypeDll.cs file, add [StructLayout(LayoutKind.Sequential, Pack = 1)] above the struct IOADC definition. C# is making the IOADC struct 4 bytes but it’s members only add up to 3 bytes. This has to do with managed vs unmanaged code. Adding that line above the struct forces the compiler use only 3 bytes for the IOADC struct like the dll is expecting.