Tuesday, June 24, 2008

Opening a serial port in C#

Just a quick tutorial, I was trying to communicate with my LCD in C# and heres the gist of how i got it up and running:

Use the Header using System.IO.Ports; we create an object using the name serial_port which looks like:
SerialPort serial_port = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One);

Here we specify the required vars - Com port name, Baud rate, Parity bit, Data bits and stop bits. Now we simply open the com port serial_port.Open(); and this allows us to read/write from the selected port. For my purposes I used write commands so as to write data to my external LCD screen. Keep in mind the write command accepts only strings so I had to improvise as I was sending it a integer value.
serial_port.Write( i.ToString() );
where 'i' is the integer I am sending accross the COM7 port.

Sunday, June 08, 2008

Establishing a basic connection with the Wiimote

Been working on coding for the Wiimote using C# and have thankfully made some progress. Import the wiimote library and create an object for the wiimote

using WiimoteLib;

Now in the main function
wm.Connect();
wm.SetReportType(InputReport.IRAccel, true);
wm.SetLEDs(true, false, false, false);

This establishes the connection with the wiimote and the SetLeds tells the wiimote to show the first of four lights on the controller.
x = wm.WiimoteState.IRState.IRSensors[0].Position.X.ToString();
y = wm.WiimoteState.IRState.IRSensors[0].Position.Y.ToString();

This saves the coordinates in our strings x and y. You can take a printout or do whatever you want with these values now, still not sure what rate the wiimote is streaming the data at though.