FWIW here is the C# code for communicating with the boards so you do not have to reinvent the wheel:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Timers;
namespace I2C_Development
{
public sealed class IOworker
{
public static IOworker Instance
{
get
{
lock (typeof(I2C_Development.IOworker))
{
if (_instance == null)
{
_instance = new IOworker();
}
return _instance;
}
}
}
public static IOworker _instance;
public IOworker()
{
Thread IOW = new Thread(new ThreadStart(this.Run));
IOW.IsBackground = true;
IOW.Priority = ThreadPriority.Lowest;
IOW.Start();
}
private static System.Timers.Timer channelDecode;
private static String[,] boardComm = new String[20, 20];
public static int[] analogRead = new int[10];
public static double[,] analogValue = new double[10,5];
public static String[] commandData = new String[30];
private static String[,] boardIoData = new String[10, 5];
public static int[,] digitalWorker = new int[20, 4];
private static int[,] digitalWriter = new int[20, 3];
public static double[,] pidValue = new double[10, 7];
private static int dataIn = 0;
private static StringBuilder digital = new StringBuilder(256);
private static string [] digSend = new string [10];
public static int[] digitalIn = new int[16];
private static int[] digitalOut = new int[16];
public static bool buttonPush = false;
private static byte[,] commands = new byte[20, 20];
private static int step = 0;
private static byte[] writeBuff = new byte[6];
private static byte[] readBuff = new byte[12];
private static StringBuilder readData = new StringBuilder(12);
private static string hexData;
public static string dataSend;
public static int dataRead;
private static int bytesRead;
private static SerialPort IOPort = new SerialPort();
public void Run()
{
TimerBuild();
Configure();
}
public static void IoStart()
{
Connect();
Config();
}
private static void TimerBuild()
{
channelDecode = new System.Timers.Timer(60);
channelDecode.AutoReset = true;
channelDecode.Elapsed += new ElapsedEventHandler(channelDecodeTimedEvent);
channelDecode.Enabled = false;
}
private void Configure()
{
try
{
IOPort.PortName = "COM4"; //= PortName;
IOPort.BaudRate = 9600;
IOPort.DataBits = 8;
IOPort.ReadTimeout = 500;
IOPort.WriteTimeout = 500;
IOPort.Parity = Parity.None;
IOPort.Handshake = System.IO.Ports.Handshake.None;
IOPort.StopBits = StopBits.One;
IOPort.Encoding = System.Text.Encoding.ASCII;
}
catch
(IOException iofail)
{
Console.WriteLine(iofail);
}
}
private static void Connect()
{
try
{
if (!IOPort.IsOpen)
{
IOPort.Open();
IOPort.DtrEnable = true;
IOPort.RtsEnable = true;
} // else already open
}
catch (Exception e1)
{
Console.WriteLine("Error: Connection is in use or is not available: \n\n" + e1);
}
}
private static void channelDecodeTimedEvent(object source, ElapsedEventArgs e) // 60 millisecond scan interval
{
if (step > 7)
{
step = 0;
}
if (buttonPush == false) // Analog scanning section
{
writeBuff[0] = commands[0, 0]; // sends take reading command to analog channel
writeBuff[1] = commands[0, 1];
writeBuff[2] = commands[0, 3];
writeBuff[3] = commands[3,step];
Write();
Thread.Sleep(11);
Read(); // reads result from command
writeBuff[0] = commands[0, 0]; // retieves analog input result as 2 hex bytes
writeBuff[1] = commands[0, 2];
writeBuff[2] = commands[0, 3];
writeBuff[3] = commands[4,step];
Write();
Thread.Sleep(31);
Read();
}
if (buttonPush == true) // Digital writing section, 1 = on 0 = off
{
digital.Remove(0, digital.Length); // for channels 0 - 7
digital.Insert(0, digitalWorker[0, 2]);
digital.Insert(0, digitalWorker[1, 2]);
digital.Insert(0, digitalWorker[2, 2]);
digital.Insert(0, digitalWorker[3, 2]);
digital.Insert(0, digitalWorker[4, 2]);
digital.Insert(0, digitalWorker[5, 2]);
digital.Insert(0, digitalWorker[6, 2]);
digital.Insert(0, digitalWorker[7, 2]);
digSend[0] = String.Format("{0:X2}", Convert.ToInt32(Convert.ToString(digital), 2));
commands[2, 7] = byte.Parse(digSend[0], System.Globalization.NumberStyles.AllowHexSpecifier);
digital.Remove(0, digital.Length); // for channels 8 - 15
digital.Insert(0, digitalWorker[8, 2]);
digital.Insert(0, digitalWorker[9, 2]);
digital.Insert(0, digitalWorker[10, 2]);
digital.Insert(0, digitalWorker[11, 2]);
digital.Insert(0, digitalWorker[12, 2]);
digital.Insert(0, digitalWorker[13, 2]);
digital.Insert(0, digitalWorker[14, 2]);
digital.Insert(0, digitalWorker[15, 2]);
digSend[1] = String.Format("{0:X2}", Convert.ToInt32(Convert.ToString(digital), 2));
commands[2, 8] = byte.Parse(digSend[1], System.Globalization.NumberStyles.AllowHexSpecifier);
writeBuff[0] = commands[2, 0];
writeBuff[1] = commands[2, 1];
writeBuff[2] = commands[2, 2];
writeBuff[3] = commands[2, 3];
writeBuff[4] = commands[2, 7];
writeBuff[5] = commands[2, 8];
Write();
buttonPush = false;
}
analogValue[(Convert.ToInt32(ControlWorker.inPoint[step, 1])-1), 3] =
Math.Round(((analogRead[step] * (Convert.ToDouble(ControlWorker.inPoint[step, 2])))
+ (Convert.ToDouble(ControlWorker.inPoint[step, 3]))), 2, MidpointRounding.ToEven);
step++;
}
private static void Write()
{
try
{
IOPort.Write(writeBuff, 0, writeBuff.Length);
IOPort.BaseStream.Flush();
}
catch (Exception ex)
{
Console.WriteLine("IO write malfunction " + ex);
}
}
private static void Read()
{
bytesRead = IOPort.BytesToRead;
IOPort.BaseStream.Read(readBuff, 0, bytesRead);
IOPort.BaseStream.Flush();
if (bytesRead > 2)
{
foreach (byte b in readBuff)
{
readData.AppendFormat(String.Format("{0:X2}", b));
}
hexData = readData.ToString().Substring(0, 4);
readData.Remove(0, readData.Length);
if (hexData != null)
{
dataRead = 0;
try
{
for (int ac = 0; ac < 1; ac++) // analog data converter from 2 Hex bytes to decimal 0-4095
{
try
{
analogRead[step] = (Convert.ToInt32(hexData.Substring((ac * 4), 4), 16)); // analog values
}
catch (FormatException ConvFail0)
{
Console.WriteLine("ConvFail0 " + ConvFail0 + " dataIn " + dataIn);
}
}
hexData.Remove(0, hexData.Length);
}
catch (FormatException ConvFail0)
{
Console.WriteLine("ConvFail0 " + ConvFail0 + " dataIn " + dataIn);
}
}
}
}
public static void Config()
{
if (IOPort.IsOpen)
{
for (int digset = 0; digset < 16; digset++)
{
IOworker.digitalWorker[digset, 2] = 1;
}
commands[0, 0] = 0x54; // usb board command
commands[0, 1] = 0x90; // analog IO board address and write command
commands[0, 2] = 0x91; // analog IO board address and read channel
commands[0, 3] = 0x03; // usb board bytes to write command
commands[2, 0] = 0x54; // usb board command
commands[2, 1] = 0x40; // digital IO board address and write command
commands[2, 2] = 0x03; // usb board bytes to write command
commands[2, 3] = 0x02; // digital IO board output write command
commands[2, 4] = 0x06; // digital IO board output write command
commands[2, 5] = 0x00; // digital IO board output write command
commands[2, 6] = 0xFF; // digital IO board output write command
commands[3, 0] = 0x84; // analog channel 1 address and take sample command
commands[3, 1] = 0xC4; // analog channel 2 address and take sample command
commands[3, 2] = 0x94; // analog channel 3 address and take sample command
commands[3, 3] = 0xD4; // analog channel 4 address and take sample command
commands[3, 4] = 0xA4; // analog channel 5 address and take sample command
commands[3, 5] = 0xE4; // analog channel 6 address and take sample command
commands[3, 6] = 0xB4; // analog channel 7 address and take sample command
commands[3, 7] = 0xF4; // analog channel 8 address and take sample command
commands[4, 0] = 0x80; // analog channel 1 address and retrieve data command
commands[4, 1] = 0xC0; // analog channel 2 address and retrieve data command
commands[4, 2] = 0x90; // analog channel 3 address and retrieve data command
commands[4, 3] = 0xD0; // analog channel 4 address and retrieve data command
commands[4, 4] = 0xA0; // analog channel 5 address and retrieve data command
commands[4, 5] = 0xE0; // analog channel 6 address and retrieve data command
commands[4, 6] = 0xB0; // analog channel 7 address and retrieve data command
commands[4, 7] = 0xF0; // analog channel 8 address and retrieve data command
commands[5, 0] = 0x94; // usb board PWM command
commands[5, 1] = 0x00; // digital IO board address and write command
commands[5, 2] = 0x0F; // usb board bytes to write command
commands[6, 0] = 0x95; // usb board command for PWM
commands[6, 1] = 0x00; // PWM 3 Khz select
writeBuff[0] = commands[2, 0]; // This configures digital points 0 - 7 as outputs
writeBuff[1] = commands[2, 1]; // Run once at startup
writeBuff[2] = commands[2, 2];
writeBuff[3] = commands[2, 4];
writeBuff[4] = commands[2, 5];
writeBuff[5] = commands[2, 5];
Write();
writeBuff[0] = commands[2, 0]; // This configures digital points 8 - 15 as outputs
writeBuff[1] = commands[2, 1]; // Run once at startup
writeBuff[2] = commands[2, 2];
writeBuff[3] = commands[2, 3];
writeBuff[4] = commands[2, 6];
writeBuff[5] = commands[2, 6];
Write();
Console.WriteLine("digital config completed");
channelDecode.Enabled = true; // this starts timer loop for analog scanning
}
}
}
}