Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

16 Jan 2010

Programming the Bus Pirate with C#

A while ago i received my Bus Pirate from Seeed Studio Depot. In essence it is a universal serial bus interface and i would love to program it using c#. I know that i can use the DataReceived event and then fiddle with bits (read here if you’re into that kind of self-punishment) but spawning a separate thread to do the blocking work is ten times less work to get it up and running

static void Main()
{
	using (var serialPort = new SerialPort("COM9", 115200, Parity.None, 8, StopBits.One))
	{
		serialPort.Open();
		new Thread(() => WriteLinesFrom(serialPort)).Start();

		while (true)
		{
			var command = Console.ReadLine();
			if (command == "EXIT") break;
			serialPort.WriteLine(command);
		}
	}
}

static void WriteLinesFrom(SerialPort serialPort)
{
	try
	{
		while (true) Console.WriteLine(serialPort.ReadLine());
	}
	catch (Exception)
	{
		break;
	}
}