Hi James,
Here is a simple serial example I put together for you. This example is not a comprehensive example and is missing some of the error correction and data validation you may want to add to make it more robust. But it should serve a good enough example to get you going. No external devices are needed for this example except for a jumper wire from Digital port 1 to D3. D1 is the Transmit (Tx) pin for COM1 and D2 is the Receiver (Rx) pin for COM2. In this example I'm using the Netduino as the receiver and sender, but in many cases one of these ports would be replaced by another device, and generally there would be some two-way communication, which can be easily added. For instance, in the code below you can send an acknowledgement command back to COM1 to let it know the transmission was successful. But of course, you would then have to add a wire from D0 to D2 to connect the Rx of COM1 to the TX of COM2. I will let you tackle that scenario.
Hopefully this helps. Let us know if you have any other questions.
Cheers,
Steve
Edit: I confused the WriteRead method of the SPI class with the Read method of the SerialPort class. There is only one Read method for SerialPort and not "many". I have changed the comments to reflect that change.
using System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using System.IO.Ports; //Required for the SerialPortsusing System.Text; // Required for the Encodingnamespace SimpleSerialExample{ public class Program { static SerialPort serialPort1; static SerialPort serialPort2; static OutputPort led; public static void Main() { // Define our LED. We will toolge the on/off state with serial commands. led = new OutputPort(Pins.ONBOARD_LED, false); // Define the port, "COM1" Netduino digital pins 0(RX), 1(TX) // We will use COM1 as the transmitting serial port for our example. serialPort1 = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One); // Define the port, "COM2" Netduino digital pins 2(RX), 3(TX) //We will use COM2 as the receiving serial port for our example serialPort2 = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One); // Since we are using COM2 as the receiving port we need to // define an event handler for incoming data. serialPort2.DataReceived += new SerialDataReceivedEventHandler(RecievedData); // Open both of the ports making them available to send and receive data. serialPort1.Open(); serialPort2.Open(); // For a simple test, we will create an infinite loop and manually send the commands to COM2 while (true) { // Here we convert the characters of the strings, "on" and "off" into a sequence of bytes and save that sequence into a byte array. byte[] onCommand = Encoding.UTF8.GetBytes("on"); byte[] offCommand = Encoding.UTF8.GetBytes("off"); // Send the on command to the onboard LED serialPort1.Write(onCommand, 0, onCommand.Length); // Wait for half a second before sending the next command. At slower baud rates you may need to increase the time between sending commands. Thread.Sleep(500); // Send the off command to the onboard LED serialPort1.Write(offCommand, 0, offCommand.Length); Thread.Sleep(500); } } //end of Main() static void RecievedData(object sender, SerialDataReceivedEventArgs e) { Debug.Print("Data incoming"); // Get the number of incoming bytes int numberOfBytes = serialPort2.BytesToRead; // Create a temporary Byte array to store the incoming data byte[] tempData = new byte[numberOfBytes]; // There is one method for Read(). // Here we are passing as arguments our temporary data array, to be populate with the incoming data, // an Offset of 0, meaning we grab the all the incoming data from the beginning. We could ignore the first 'x' number of // incoming bytes by increasing the offset. // The final argument is the count or number of bytes we are expecting to receiving from our incoming transmission. serialPort2.Read(tempData, 0, numberOfBytes); //Now we need to convert our incoming data into a readable string string command = new string(Encoding.UTF8.GetChars(tempData)); // Here we do some minor error checking. In a more robust scenario you would create a data packet // and do more intensive error correction. if (command != null) { //Remove any extra empty space from the end of the string command = command.Trim(); } else { command = ""; } // Pass our command string to the LED Controller LEDController(command); }//end of RecievedData() static void LEDController(string command) { // Display received data in the debugger Output window. Debug.Print("Incoming Command: '" + command + "'"); // Use a switch statement to define what action to take with the received command switch (command) { case "on": // turn LED on led.Write(true); //Display LED state Debug.Print("LED is ON"); break; case "off": // turn LED off led.Write(false); //Display LED state Debug.Print("LED is OFF"); break; default: Debug.Print("Unrecognized command: '" + command + "'"); break; } }// end of LEDController() } //end of Program} //end of namespace
Edited by Gutworks, 01 March 2013 - 03:20 PM.