Introduction: Introducing the Wixel USB Wireless Module

EDIT: I've entered this into the makerbot contest https://www.instructables.com/contest/makerbot/ , so please vote if you liked it. What am I 'making' you ask, hopefully ideas and desire to create. :]   /EDIT

I have been playing with a Arduino microcontroller and robotics bits and pieces for a while now.

I'm still designing my first robot and wanted a two-way wireless capability, but until now they were either too expensive , or too basic .

BUT NOW I HAVE THE SOLUTION: The Pololu Wixel programable USB wireless module!

It comes in a variety of packages, the one I chose was the assembled combination deal, http://www.pololu.com/catalog/product/1338 (picture two ), which has a pair of modules and a mini USB cable for $42 USD + shipping. Or you can get a single module either assembled (with breadboard pins) or not for $21 or $20 (pictures three & four ).

So what can it do you ask?

Well to quote Pololu:

"The Pololu Wixel is a general-purpose programmable module featuring a 2.4 GHz radio and USB. The Wixel is based on the CC2511F32 microcontroller from Texas Instruments, which has an integrated radio transceiver, 32 KB of flash memory, 4 KB of RAM, and a full-speed USB interface. A total of 15 general-purpose I/O lines are available, including 6 analog inputs "

there are very flexible fancy uses, but for starters simply download one of six (and growing) precompiled, open-source 'apps';

a. Blink an LED (Wow you say sarcasticly ! - this is just an example/test app),
b. Wireless Serial, a bidirectional lossless serial link, either end can be USB, or UART,
c. USB to TTL Serial adapter (@ 3.3V), no need to get a FTDI cable/converter,
d. I/O Repeater, wirelessly send/receive up to 15 microcontroller I/O pin states,
e. Wireless ShiftBrite RGB LED control (http://www.pololu.com/catalog/product/1240) ,
f.  Wireless tilt mouse, using an 3-axis accelerometer.

Details of these apps can be found at http://www.pololu.com/docs/0J46/9 .

There is also much more it can do, including a SDK, which I will mention in a later step.

You should note now its main flaw; IT IS NOT 5V I/O TOLLERANT (Argh!). However this is easily managed, but be carefull connecting it on 5V projects or you'll fry a pin. You have been WARNED!

The other flaw is that when on the breadboard you can't see the pinouts, they are on the bottom! They could have used longer pins, to clear the USB connector, on the opposite side, thus breadboardable with the pinouts facing up. Version 2 Mr Pololu? You could buy the unassembled version and get some longer header pins.

Next - What you need.

(some images courtesy of Pololu)

Step 1: What Else Do You Need?

So you've got a Wixel in your hot little hands , what do you need to make it do something?

Of course you don't need a Wixel to proceed, this instructable will introduce you to most of the capabilities of a Wixel.

As I mentioned I have been learning the Arduino so I will be focusing on this platform, you could use Wixels with other microcontrollers, or just use the built-in TI CC2511F32 microcontroller, but I don't want to learn a new platform just now.

So you will need an Arduino or compatible microcontroller for the more complex examples. I have a Duemilanove, but a new Uno is good, or you can get cheap bare bones kits, just search instructables for 'Arduino' and you will see lots of options. See http://www.arduino.cc/ if you are unfamillier with this platform.

Note: This is not an Introduction to Arduino, again search and you will find many 'ables to help you out.

For the examples in the following steps you will need;

2 x Wixels, http://www.pololu.com/catalog/product/1336 ,
1 x USB A to Mini-B cable or adapters, http://www.pololu.com/catalog/product/130 ,
(or the Combo Deal http://www.pololu.com/catalog/product/1338 )

1 x Arduino microcontroller, with USB cable or programmer/FTDI cable to suit, http://www.pololu.com/catalog/product/1616
1 x Windows PC/Notebook with two USB ports (with Arduino software installed),

2 x Medium Prototyping breadboards (270+ points, but I'd get 400 points), http://www.pololu.com/catalog/category/28 ,
Various prototyping wires such as
(http://www.pololu.com/catalog/category/29 and/or http://www.pololu.com/catalog/category/68 ),

4 x AA batteries, either normal 1.5V AA, or rechargable 1.2V AA (http://www.pololu.com/catalog/product/1003 ),
1 x Four AA battery pack, with bare wires for breadboard connection,
This one has a switch built in http://www.pololu.com/catalog/product/1159
I have one without a switch, so I used this http://www.sparkfun.com/products/102 ,
(or a breadboard connectable 5V regulated DC source).

Discrete Components:
1 x 1.2k Ohm resisters,
1 x 2.2k Ohm resisters,
(If you're just starting & don't have resisters I recommend this kit http://www.sparkfun.com/products/9258, or get individual packs at http://www.futurlec.com/Res14W.shtml )

2 x Momentory pushbutton micro switches, http://www.pololu.com/catalog/product/1400 ,
4 x LEDs, any will do but two colours will look better, (http://www.pololu.com/catalog/product/1070 & http://www.pololu.com/catalog/product/1071 ),

Optional - only get these if you have another use for them:
1 x PIR Motion Sensor,
http://www.dfrobot.com/index.php?route=product/product&path=36_58&product_id=119
(or any which can run on 3.3V and has a digital out pin, Pololu have this one http://www.pololu.com/catalog/product/1635 but you will need to follow their instructions to run at 3.3V)
1 x 3 Pin male headers, http://www.pololu.com/catalog/product/965 & a pair of pliers to save the ouch factor.

Naturally if you're experienced, use whatever you have at hand.

In the next steps I will start with simple uses and expand to more complex examples.

Next - Setup the Wixels.

[Note: I have linked to Pololu above for simplicity, many of these components are also available elsewhere, use your favourite sources. I have no connection with Pololu apart from liking their products. (but happy to get any free samples though, Mr Pololu... ;). Ditto for Sparkfun, DFRobot, Littlebird and Futurlec etc.]

Step 2: Setup the Wixels - Step 1


Pololu have excellent online documentation including step by step examples. Picture one .

I won't re-invent the wheel here, I will link to the appropriate page where you can follow the instructions. I will however, point out any difficulties I had when i first used my Wixels. If you feel confident you can go to the user guide, http://www.pololu.com/docs/0J46 , and go for it, and come back here when your done.

So here we go...

Wixel currently only supports Windows (XP+SP3 thru W7). Linux & Mac OS are planned to be supported.

See http://www.pololu.com/docs/0J46/3.a , follow the instructions to download and Install Drivers & Software. For Windows 7 or Vista, don't stop after step 4, continue after the XP section, ie after step 9. Particularly the bit on using Device Manager to find the Wixels COM port (picture two ).

Next - Install an app & blink that LED baby

Step 3: Setup the Wixels - Install an App & Flash That LED!

Next follow http://www.pololu.com/docs/0J46/3.d to install the Example_Blink_LED app; rather than double clicking the WXL file, you should use the Wixel Configuration Utility (WCU) to get used to it. See the WCU at picture one .

I have only used this with Windows 7, note the mention of specific things you need to do with XP. Sorry I can't test with XP but if you have an issue post a comment, I or someone may be able to help.

When you have completed the 3.d steps in the above link, you have a Blinking LED, see picture two .

Pretty straight foward.

So we now have a working Wixel installation.

You should now swap to the second Wixel and set it up with the Blinking LED app. This is so we can use either of them in the following steps without having to initialise it.

While we are using the WCU, press the 'Get apps...' link and download the USB-to-Serial & Wireless Serial apps. We will be using them in following steps.

Also download the Wixel_Instructable.zip file below, right click & Save As to give it a meaningfull filename rather than 'able randomness. This has a few demo programs for the next steps.

Next - we get to plug stuff in and do something real!

Step 4: Plug Stuff in and Do Something Real - USB

To begin with we will use the PC to power the Wixel via USB.

Firstly look at the pinouts in the first picture.

Initially disregard the coloured boxes, just focus on the pin labels next to the board. The pins are either power related (red ones & Gnd) or I/O pins (and lonely /RESET pin).

Power.
When connected via USB the VALT (5V) and 3.3V output pins can supply up to 500ma and 150ma respectively. Another step will introduce the VIN voltage input pin. If you will use both 5V and 3.3V remember to have a common ground, and that the I/O pins are NOT 5V tollerant (I'll show you how to handle this later).

If you want more details on power see http://www.pololu.com/docs/0J46/5.a .

I/O.
These pins can be digital inputs or outputs, now note the yellow box, these pins can be analog inputs. According to a TI forum regular I/O pins are designed to be able to source 4 mA while P1_0 and P1_1 are designed for 20 mA (enough to drive an LED).

More details of the pinouts (and LED behaviour) is at http://www.pololu.com/docs/0J46/1.a .

So can we do something real now?

Get your breadboard and wiring ready...

Next - we are going to blink another LED! Wow - you say, but there's more...


Step 5: Plug Stuff in and Do Something Real - USB Serial Part 1

We are going to control two LEDs from the PC, and use two switches to provide input to the PC. This will use the USB-to-Serial app, but not for serial, that's the next step.

The Serial app allows the use of two inputs (/DSR & /CD) & two outputs (/DTR & /RTS), these are part of old serial communication protocols (such as RS232) and are not normally used in the TTL serial I/O by microcontrollers, hence they can be used for general I/O. See http://www.pololu.com/docs/0J46/9.c if you want more details.

Pinouts:
P1_0  /DTR
P1_1  /RTS
P1_2  /DSR
P1_3  /CD

These are inverted logic levels, meaning 0V is a HIGH (or ON) signal level, and 3.3V is a LOW (OFF) level. The "/" represents a line above the pin label, such as on the /RESET pin in the pinout picture, meaning inverted logic.

These signals appear to be somewhat slow to change, the doco suggests they are suitable for 5Hz, ie five changes a second, not so good for high speed circuits, but good for switching things at 'human' speeds, such as lights, or turning devices on/off etc, with appropriate circuitry.

So lets build it, we need two microswitches, two LEDs, your breadboard and some wires, wire them up as shown in the first, second & third pictures , note the polarity of the LEDs. You may want to use resisters for the LEDs, see comment below, their wiring is explained in the pictures.

As you can see pins P1_0 & 1 (DTR & RTS) are sourcing the LEDs to GND, and the two switches take P_2 & 3 (DSR & CD) to GND.

Connect a Wixel and use the Wixel Configuration Utility load the USB-to-Serial app. Note the COM port displayed by the WCU.

Now we need a program on the PC to drive the Wixel. I have written a simple script, compiled to SerialPortGeneralIO.exe in the zip file you downloaded in step 2. The source is also in the zip file with a .ahk extension.

Ahk is AutoHotkey see http://www.autohotkey.com/ , I use and recommend AutoHotkey for automation and simple knockups. Programming Serial I/O is beyond the scope of this 'able, I give some hints in the last step. Unless you know AutoHotkey you may not make much of the source code.

Run SerialPortGeneralIO.exe , see picture four . First set the COM port to that from the WCU.

Now the Inputs show the state of the microswitches, click the Beep box if you want it to beep when you press a switch, and the outputs control the LEDs, click to change the state, click the Blink box to flash the LED. Ignore the Invert boxes, they are for another step.

Note: You should close programs accessing the Wixel before unplugging them. If you unplug the Wixel while the program is running, you need to use Device Manager to disable/enable the COM port before you can use it again. Anyone know an easier way to clear this condition?

So pretty straight foward, you can see how you can acheive general I/O in conjunction with a serial connection.

This means you can connect any sensor with a digital output to your PC. For example you could have a motion sensor and a program to detect when you arive at the PC and clear the screen saver and do some automated tasks. Add an IR sensor to detect your presence and do stuff when you leave the PC. You could also drive output to a relay circuit to, for example, power down peripherals when you are absent, and/or turn lights on & off.

Next - we do something real with USB Serial and a Motion Sensor.

p.s. I have had a comment that there should be resisters in the LED cuircits. I chose to not use them for this quick and dirty demo. My LEDs have been lighted for hours without issue. If you were doing this for a long term device you should use a 58 Ohm resister from the -ive LED pin to the -ive (Gnd) bus instead of wire jumpers. For projects using higher voltages this becomes more important.

Step 6: Plug Stuff in and Do Something Real - USB Serial Part 2

This is optional, don't buy a PIR Sensor just for this. But if you're into electronics or robotics, get one, they have lots of uses.

This step shows how to do something real, detect motion with your PC and react to it.

Most PIR sensors have a digital out pin which signals when movement is detected.

The DFRobot Sensor takes 3-5V to the left pin, Gnd to the right pin and digital out from the middle pin. Unlike the info on their web site, output is HIGH when movement is detected. I did notice a soldered jumper with H & L (bottom left corner in picture two ), so I suspect this can be customised if needed.

Grab the breadboard from the previous step, remove the switch furthest away from the USB connector. Next we connect the PIR Sensor.

When I ordered my DFRobot sensor it looked like picture one , what got delivered was as shown in pictures two & three , they added a small board with a JST connector and supplied a JST to female 0.1" pin connector.

If your sensor is like picture one , plug it into the breadboard on the side with the fewer wires, lined up to where the switch was (after you do the following wiring or it gets in the way).

Then use jumper wire to connect the +ive pin to the +ive bus, -ive pin to the -ive (Gnd) bus, and  the Output pin to the row that was connected to pin P1_3 (/CD), i.e. row 18. As I don't have one like this I can't show you a picture, but see picture four where I have tagged the rows to use.

If you have the sensor with JST wires, you need to pull-out three header pins and insert them into the 0.1" pin sockets, see picture three and read the tags; you definitely need pliers for this. This allows you to connect it to the breadboard. This is a hack as I didn't have the appropriate cable or adapter, don't do this for anything important.

Luckily they line-up with the current wiring of the board, plug it in where the switch was, with the black wire furthest away from the USB socket, wire a jumper to the middle row to the +ive bus, See picture four .

OK, now run SerialPortGeneralIO.exe , you should see /DCD change when motion is detected, you can tick the beep box and you have a basic movement alarm. Remember /DCD is inverted, so check the Invert box - so that it beeps only when motion is detected. See picture five .

Of course you could do other stuff like sending an email alert, playing the sounds of guns being loaded and someone wispering 'their here, you ready, I'll take the first guy " ;)

Enough of the simple stuff.

Next - plug stuff in and do something real - with an Arduino.

Step 7: Plug Stuff in and Do Something Real - With an Arduino - USB Serial Part 3

Keep the breadboard as it was in the last step, except unplug the PIR sensor so it is out of the way.

Bring in your Arduino, we will serially connect it to the Wixel.

Connect the Wixel pins to Arduino pins as follows;
         Arduino P2 (RX) <-> Wixel P1_6 (TX) [long green wire]
         Arduino P3 (TX)<-> Wixel P1_7 (RX) [long blue wire]

Egads - STOP! Didn't I tell you it is NOT 5V tollerent!

Well I forgot too !  I plugged the 5V TX from the Arduino into the Wixel, and it worked !

It didn't blow smoke :) however I quickly remembered and disconnected the wires. It may be the Arduino current was low enough not to hurt the Wixel, but do so at you own risk - I don't recommend it.

The section on connecting microcontrollers is at http://www.pololu.com/docs/0J46/5.b , but in summary see picture three for a simple voltage divider circuit to connect a 5V pin to a 3.3V pin. The divider is only required on the Wixel RX pin, TX is an output pin so it's 3.3V.

Picture four shows that using a 3.3V controller is simpler, you could use a LilyPad http://www.sparkfun.com/products/9266 , or Arduino Pro/Pro Mini. Or the http://www.jkdevices.com/arduino-compatible/arduino-duemilanove-compatible which has built in level converters, I'm tempted...two Arduinos, imagine the trouble I could cause...

The dotted line is optional and allows the controller to reset the Wixel, you need to be careful with 5V, you could ensure that the pin is only floating or LOW, and never driven HIGH , or use another divider circuit. We won't use it here.

As I didn't have 2K Ohm resisters, a quick calculation showed that using 1K2 and 2K2 gives close enough to 3.3V, so that's what I used.  http://en.wikipedia.org/wiki/Voltage_divider details the voltage divider calculations.

See the first and second pictures for the breadboard setup with the voltage divider circuit, I used a free row under the Wixel antenna to squeeze in the resisters. Add the resisters to your board and connect to the Arduino pins as described above and in pictures five & six . There are tags on the picture explaining the connections.

EDIT: I forgot to follow my own advice and use a common ground. You should also connect the -ive (Gnd) bus to one of the Gnd pins on the Arduino. It works without it but it is best to prevent unbalanced voltages. /EDIT

From the Zip file, get the WixelSerialExample.pde sketch, also get the NewSoftSerial directory and paste it into your Arduino Libraries directory, it allows Serial I/O using pins other than the UART pins (P0 & P1), because we use the UART for the Serial Monitor on the PC.

Run the Arduino IDE and open the WixelSerialExample.pde sketch.

Upload the sketch and then open the Serial Monitor.

The sketch will receive bytes sent by the Wixel and send them to the Serial Monitor.

Now run SerialPortSerialIO.exe , select the COM port to the Wixel, then press one of the buttons.

The secret message is sent to the Wixel, the Wixel sends it to the Arduino and you should see the secret message in the Serial Monitor, see picture seven . If you get garbled characters check your Serial Monitor BAUD rate matches the sketch values.

So we can see how the Wixel allows you to send data from the PC to any serial device.

You can also add the use of  /RTS &  /DTR pins to control other pins on a serial device, such as /RESET or ENABLE.

There is a huge range of devices which talk serial;

     - Motor Controllers http://www.pololu.com/catalog/category/10 ,
     - RC Servo Controllers http://www.pololu.com/catalog/category/12 ,
     - Serial Sensors http://www.pololu.com/search?query=serial+sensor&x=0&y=0 ,
     - and many specialised devices, GPSs, Microcontollers, LCD displays, Barcode scanners,
       Card Readers, RFID, LED controllers, some Phones, LAB Equipment, Spy Satellites &
       Mars Rovers?

You may get a USB serial adapter for a few bucks less, but the flexibility of the Wixel is greater for tinkerers & hardware hackers. I'm still working on the 'able for the Wixel contolled Mars rover ;)

Next - Plug stuff in and do something real - Wirelessly!

p.s. Another option I have found, but not tried is a logic level converter, such as:
http://littlebirdelectronics.com/collections/breakout-boards/products/logic-level-converter

Step 8: Plug Stuff in and Do Something Real Wirelessly!

Now disconnect the Wixel from the Arduino.

With the WCU load the Wireless Serial App v1.2 , see picture one , note the parameters on the right. We are only interested in the first two parameters and the last one.

The second parameter is the BAUD rate, change it to 57600.

Scroll down to the last parameter, radio_channel. As the doco says "The channel number is from 0 to 255 and determines which frequency to broadcast on. Wixels must be on the same channel to communicate with each other. To avoid interference, Wixels that aren’t supposed to talk to each other should be at least 2 channels away from each other. "

We will leave the channel as default, but when you get multiple Wixels you may need to manage the channels to prevent conflict. Both Wixels will be on the same channel, bi-directional I/O between them.

This app allows the Wixel to do Wireless or wired Serial as described at http://www.pololu.com/docs/0J46/9.b . The serial mode parameter determines the mode. Zero is an automatic mode which uses the power source to determine the mode. This suits what we will be doing.

We will use one Wixel connected to the PC to wirelessly communicate to the other Wixel. The 'remote' Wixel will be on battery power. The Zero automatic mode means the USB powered Wixel will use the radio, and the VIN powered 'remote' will bridge the radio and the UART (ie TTL Serial).

In addition to the Wireless Serial signal the previouly mentioned /DTR /RTD /DSR /CD signals are also available wirelessly. Leave the default pins allocation, but the WCU parameters let you reassign these to other pins if you need to.

So after changing the BAUD rate, write the app to the Wixel on the breadboard.

Next unplug the Wixel, plug the USB cable into the second Wixel.

With the WCU load the Wireless Serial App v1.2 , set the BAUD rate parameter to 57600 and write to the Wixel. Windows will install drivers and allocate a COM port, note the COM port in WCU.

Leave this Wixel connected to the PC, mount this Wixel on the second breadboard for stability.

Now about battery power. Technically the Wixel can use power supply from 2.7-6.5 Volts, but there are other considerations. If it is below 3.5V the 3.3V output will be below 3.3V, due to the voltage drop of the regulator. VALT will be slightly less than VIN, due to a diode; on USB power (5V) I measured VALT as 4.33V.

If you are happy with 3.3V @ 150ma for circuits, simply supply 3.5 to 6.5V to VIN. However if you want to use 5V too, you want to provide 5V regulated power (so VALT is stable for your other circuits).

We will stick to 3.3V for simplisity, and use 4 AA batteries; rechargables provide ~4.8V and normal AAs provide ~6V. My four fully charged NiMh cells put out 5.54V.

Insert your batteries and connect the black lead to the -ive (Gnd) bus, connect the red lead to VIN (pin 2 on the left, ie abcde columns), I'm assuming you have the switched battery case. If the wire leads are a little loose in the board, I have a little trick, use a naked header pin and jam it in the same hole as the wire, see picture two .

Pictures three & four show my connection, with the mini switch, using the spare holes under the antenna. Black wire to the middle pin, and one outer pin is jumpered to the -ive (Gnd) bus.

Run SerialPortGeneralIO.exe , set the COM port , check the Blink boxes and the DSR Beep & Invert boxes.

Turn on the batteries on the 'remote' breadboard and you should see blinking LEDs, see picture five , and if you press the button the PC should beep.

Two way wireless communication! Pretty simple hey!

Now grab your remote board & battery and go wondering around the house & outside, if it stops blinking the signal is lost.

I got to the other end of a medium wooden house, ~12m/36ft, and had a signal much of the time, some places with lots of walls were dead spots. Outside I only get signal with line of sight to the room with the other Wixel.

This will be more than adequate for my robot use, as I will be in line if sight.

Next - I wrap up.











Step 9: Introducing the Wixel - Wrap It Up.

Well that is the basic wired and wireless Wixel introduction.

I have only had the Wixels for a few days, yet managed to get the basics working quite easily. If you are already familiar with microcontrollers etc. you should find it easy too.

I would have liked to demo the I/O Repeater app, but you really need two controllers to make that interesting, unfortunately I only have a lonely Arduino...  :[

If you want to, you can easily go one more step and wire the Arduino to the remote Wixel in the same way as step 7, to wirelessly send serial commands to the Arduino. I leave that as an exercise for the reader.

That is how I intend to control my robot, send commands from my netbook via the Wixels to have it FOLLOW MY ORDERS! Oops sorry..

My netbook is a convertable tablet, hence it will be straight forward to have a touch controled gui to control the robot and also to get status back such as battery levels, state of the sensors etc. But that will be another instructable.

Programming the PC side is straight forward. Get Microsoft Visual Studio Express (for either C#, C++, or Visual Basic) and write programs that use the SerialPort class to communicate with the Wixel. Use  'System.IO.Ports' see http://msdn.microsoft.com/en-us/library/system.io.ports.aspx   and http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx (particularly the example at the bottom) and search the net and you should find other examples. I was pointed to http://www.serial-port-monitor.com/free-serial-port-monitor-product-details.html to help with developing serial programs, I've yet to try it, but though it may be helpful.

EDIT I've managed to use Visual C# - see next step for sample code /EDIT

If you want to get into the Wixels advanced features you can start at http://www.pololu.com/docs/0J46/10 , I have had a look at the SDK and the source of some apps. It seems straight forward for an experienced programmer, it is not however, as simple as the Arduino IDE.

With the existing apps it has plenty of uses, and keep an eye out for more apps, I'm sure there will be plenty of creative uses to come.

In fact I heard a wisper that they are very close to releasing a serial-to-I2C app for the Wixel that will make it easy to wirelessly interface with the many I2C sensors available out there, so keep an eye out, can't wait for that one!

So, I introduce the Wixel, an advanced, flexible and affordable wireless solution, well documented and easily implemented.

I commend it to you. Have fun...


-----------------------------------------------
(How's that for my first 'able?)

Step 10: Update - Some Visual Studio C# Example Code


I've installed and frustratingly learned to use Visual C#.

Here is code which does what the ahk demo programs do.

I've just put the a code snippet below, see zip file at bottom for full projects.

>>>Starts on next line<<<----------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Media;

namespace SerialIO
{

public partial class SerialIOform : Form
{
string[] PortNames = {};
int BaudRateDefault = 9600;
Parity ParityDefault = Parity.None;
int DataBitsDefault = 8;
StopBits StopBitsDefault = StopBits.One;
Handshake HandshakeDefault = Handshake.None;
bool RTSdefault = false;
bool DTRdefault = false;

const string MessagePortError = "Error opening Port";

// Create a new SerialPort object with default settings.
SerialPort IOport = new SerialPort();

// Create timer to do stuff when started
Timer DoItNow = new Timer();

public SerialIOform()
{
InitializeComponent();
// Set port defaults and check if ports exist
SerialIOPortSetup();
// Setup timer
DoItNow.Interval = 500; // Two times a second
DoItNow.Tick += new EventHandler(DoItNow_Tick);
}

// Setup Defaults & Dropdown
private void SerialIOPortSetup()
{
PortNames = SerialPort.GetPortNames();
if (PortNames.Length != 0)
{
// Set Defaults
IOport.PortName = PortNames[0];
IOport.BaudRate = BaudRateDefault;
IOport.Parity = ParityDefault;
IOport.DataBits = DataBitsDefault;
IOport.StopBits = StopBitsDefault;
IOport.Handshake = HandshakeDefault;
IOport.RtsEnable = RTSdefault;
IOport.DtrEnable = DTRdefault;

// Populate Dropdown
SerialPortComboBox.Items.Clear();
SerialPortComboBox.Items.AddRange(PortNames);
SerialPortComboBox.SelectedIndex = 0;
MessagesLabel.Text = "";
StartButton.Text = "Start";
}
else
{
SerialPortComboBox.Items.Clear();
MessagesLabel.Text = "No ports found! Connect & press Refresh";
StartButton.Text = "Refresh";
}
}

private void SerialIOform_Load(object sender, EventArgs e)
{

}

private void SerialPortComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// close old port
if (IOport.IsOpen)
IOport.Close();

// set to selected port
IOport.PortName = SerialPortComboBox.SelectedItem.ToString();

// Check port valid
try
{
IOport.Open();
MessagesLabel.Text = ""; // clear old error msg
}
catch
{
MessagesLabel.Text = MessagePortError;
}
// Close it so it isn't open when doing nothing,
// Saves problem when port unplugged
if (IOport.IsOpen)
IOport.Close();
}

private void StartButton_Click(object sender, EventArgs e)
{
if (StartButton.Text == "Refresh")
{
SerialIOPortSetup();
}
else if (StartButton.Text == "Stop")
{
// Stop timer
DoItNow.Stop();
IOport.Close();
StartButton.Text = "Start";
}
else // must be "Start"
{
if (IOport.IsOpen ) // safety belt, port should be closed
IOport.Close();
// Check port valid, need to do it again as device could be unplugged
try {
IOport.Open();
}
catch {
MessagesLabel.Text = MessagePortError;
return;
}
StartButton.Text = "Stop";
// Port opened, Start timer to do stuff
DoItNow.Start();
}
}

void DoItNow_Tick(object sender, EventArgs e)
{
// Stop timer while processing event
DoItNow.Stop();
// Set input checkboxes to IOport settings
DCDcheckBox.Checked = IOport.CDHolding;
DSRcheckBox.Checked = IOport.DsrHolding;

// Beep if set
if (DCDbeepCheckBox.Checked && DCDcheckBox.Checked)
SystemSounds.Asterisk.Play();

if (DSRbeepCheckBox.Checked && DSRcheckBox.Checked)
SystemSounds.Beep.Play();

// Toggle if blink checked
if (DTRblinkCheckBox.Checked)
DTRcheckBox.Checked = !DTRcheckBox.Checked;
if (RTSblinkCheckBox.Checked)
RTScheckBox.Checked = !RTScheckBox.Checked;

// Set IOport to output settings
IOport.DtrEnable = DTRcheckBox.Checked;
IOport.RtsEnable = RTScheckBox.Checked;

// Restart timer
DoItNow.Start();
}

private void CancelButton_Click(object sender, EventArgs e)
{
if (IOport.IsOpen )
IOport.Close();
// get rid of IOport??

// exit
SerialIOform.ActiveForm.Close();
}

private void DCDbeepCheckBox_CheckedChanged(object sender, EventArgs e)
{
// don't need to do stuff, we test checked state elsewhere
}

private void DSRbeepCheckBox_CheckedChanged(object sender, EventArgs e)
{
// don't need to do stuff, we test checked state elsewhere
}

private void DTRblinkCheckBox_CheckedChanged(object sender, EventArgs e)
{
// don't need to do stuff, we test checked state elsewhere
}
private void DTRBlinkCheckBox_CheckedChanged(object sender, EventArgs e)
// Visual Studio got its nickers in a knot when I renamed DTRBlink checkbox to DTRblink
// and now asks for a DTRBlink version. ???
{
// don't need to do stuff, we test checked state elsewhere
}

private void RTSblinkCheckBox_CheckedChanged(object sender, EventArgs e)
{
// don't need to do stuff, we test checked state elsewhere
}

private void DTRcheckBox_CheckedChanged(object sender, EventArgs e)
{
// don't need to do stuff, we set state elsewhere
}

private void RTScheckBox_CheckedChanged(object sender, EventArgs e)
{
// don't need to do stuff, we set state elsewhere
}

//
//
} // SerialIOForm
} // SerialIO namespace
>>>Last Line above<<<---------------------------------------------------------------------------

MakerBot Challenge

Participated in the
MakerBot Challenge