Here you go, CyberQ II USB Connection source. Enjoy...
using System;
using System.IO.Ports;
using System.Threading;
namespace CyberQUSBConnection.USB
{
///
/// This class deals with:
/// 1. Opening/Closing USB COM-Port connection
/// 2. Send commands to the Hardware device
/// 3. Utility methods to read returned values
/// 4. Utility methods to do any miscellaneous operations
///
public class USBConnection
{
public static int HardwareUpdateAck = -1;
public static bool COMPortFound = false;
public static SerialPort Port = new SerialPort();
public static int SleepTime = 50;
public static int UpgradeSleepTime = 2;
public static void SendData(string usbinput)
{
// return if no connection
if (!Port.IsOpen)
{
return;
}
Port.Write(usbinput);
}
public static void SendData(char[] usbinput)
{
// return if no connection
if (!Port.IsOpen)
{
return;
}
Port.Write(usbinput, 0, usbinput.Length);
}
///
/// Get the latest data from the Hardware
///
public static bool GetData()
{
try
{
// return if no connection
if (!Port.IsOpen)
{
return true;
}
Port.Write("~[");
Thread.Sleep(SleepTime);
Port.Write("~A");
Thread.Sleep(SleepTime);
Port.Write("~B");
Thread.Sleep(SleepTime);
Port.Write("~C");
Thread.Sleep(SleepTime);
Port.Write("~D");
Thread.Sleep(SleepTime);
Port.Write("~E");
Thread.Sleep(SleepTime);
Port.Write("~F");
Thread.Sleep(SleepTime);
Port.Write("~G");
Thread.Sleep(SleepTime);
Port.Write("~H");
Thread.Sleep(SleepTime);
Port.Write("~I");
Thread.Sleep(SleepTime);
Port.Write("~J");
Thread.Sleep(SleepTime);
Port.Write("~K");
Thread.Sleep(SleepTime);
Port.Write("~L");
Thread.Sleep(SleepTime);
Port.Write("~M");
Thread.Sleep(SleepTime);
Port.Write("~N");
Thread.Sleep(SleepTime);
Port.Write("~O");
Thread.Sleep(SleepTime);
Port.Write("~P");
Thread.Sleep(SleepTime);
Port.Write("~Q");
Thread.Sleep(SleepTime);
Port.Write("~R");
Thread.Sleep(SleepTime);
Port.Write("~S");
Thread.Sleep(SleepTime);
Port.Write("~T");
Thread.Sleep(SleepTime);
Port.Write("~U");
Thread.Sleep(SleepTime);
Port.Write("~V");
Thread.Sleep(SleepTime);
Port.Write("~W");
Thread.Sleep(SleepTime);
Port.Write("~X");
Thread.Sleep(SleepTime);
Port.Write("~Y");
Thread.Sleep(SleepTime);
Port.Write("~Z");
Thread.Sleep(SleepTime);
}
catch (TimeoutException timeoutEx)
{
// ignore this guy
System.Diagnostics.Debug.WriteLine("GetData() TimeOutException: " + timeoutEx.Message);
System.Diagnostics.Debug.WriteLine("end time " + DateTime.Now.ToLongTimeString());
return false;
}
catch (Exception ex)
{
//throw ex;
System.Diagnostics.Debug.WriteLine("GetData() Exception: " + ex.Message);
return false;
}
return true;
}
///
/// Find the valid COM port number to connect to
///
private static bool FindCOMPort(decimal commPort)
{
// open the Port
if (!Port.IsOpen)
{
foreach (string comPortname in SerialPort.GetPortNames())
{
// Set SerialPort properties
Port.WriteTimeout = 200;
Port.ReadTimeout = 200;
//open port
if (commPort > 0)
{
Port.PortName = "COM" + commPort.ToString();
}
else
{
Port.PortName = comPortname;
}
// send test data
try
{
Port.Open();
Port.Write("~@");
Thread.Sleep(200);
if (COMPortFound)
{
return true;
}
else
{
// close and try the next one
Port.Close();
}
}
catch(Exception ex)
{
string error = ex.ToString();
}
//open port
if (commPort > 0)
{
break;
}
}
}
return COMPortFound;
}
private static bool FindCOMPortUpgrade(char[] testdata, decimal commPort)
{
// open the Port
if (!Port.IsOpen)
{
foreach (string comPortname in SerialPort.GetPortNames())
{
// Set SerialPort properties
Port.WriteTimeout = 200;
Port.ReadTimeout = 200;
//open port
if (commPort > 0)
{
Port.PortName = "COM" + commPort.ToString();
}
else
{
Port.PortName = comPortname;
}
// send test data
try
{
Port.Open();
// Send the first 100 characters and wait for the 19 message to come back
// Get the content from the web site or the file system
Port.Write(testdata, 0, 100);
Thread.Sleep(200);
if (USBConnection.HardwareUpdateAck == 19)
{
return true;
}
else
{
Port.Close();
}
}
catch (Exception ex)
{
string error = ex.ToString();
}
if (commPort > 0)
{
break;
}
}
}
return COMPortFound;
}
///
/// Figure out the proper Port and open the Port
///
///
public static bool OpenConnection(decimal commPort)
{
try
{
if (!FindCOMPort(commPort))
{
throw new Exception("Cannot find a valid COM Port");
}
// Set SerialPort properties
Port.WriteTimeout = 1000;
Port.ReadTimeout = 1000;
if (Port.IsOpen)
{
return true;
}
if (Port == null)
{
Port = new SerialPort();
if (!FindCOMPort(commPort))
{
throw new Exception("Cannot find a valid COM Port");
}
}
Port.Open();
return true;
}
catch
{
return false;
}
}
///
/// Close the connection
///
public static void CloseConnection()
{
try
{
if (Port.IsOpen)
{
COMPortFound = false;
Port.Close();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
}
}
}