Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Serial port issues


  • Please log in to reply
2 replies to this topic

#1 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 06 August 2014 - 12:48 AM

Hello!

I am attempting to wrap my mind around this code:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace serial1
{
    public class Program
{
    static SerialPort serial;
 
    public static void Main()
    {
        // initialize the serial port for COM1 (using D0 & D1)
        serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
        // open the serial-port, so we can send & receive data
        serial.Open();
        // add an event-handler for handling incoming data
        serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
 
        // wait forever...
        Thread.Sleep(Timeout.Infinite);
    }
 
    static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // create a single byte array
        byte[] bytes = new byte[1];
 
        // as long as there is data waiting to be read
        while (serial.BytesToRead > 0)
        {
            // read a single byte
            serial.Read(bytes, 0, bytes.Length);
            // send the same byte back
            serial.Write(bytes, 0, bytes.Length);
        }
    }
}

And the code was lifted from this blog http://blog.codeblac...with-RS232.aspx

All he posts is the code after the definitions. He does not explain what should installed behind the scenes. Especially since I am seeing definition errors on the named functions above. However oddly enough VS does offer some automated help.

 

Has anyone tried out his suggestions?



Doctor Who
"This signature does not exist!"

#2 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 06 August 2014 - 02:29 AM

You need to add,

using System.IO.Ports;



#3 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 06 August 2014 - 05:34 PM

And you need to use a separate serial usb cable/converter. Other than that, all the program does echo back any characters it receives.

 

There are a few spots in the code that bug me.  Why does he allocate a byte array of 1 byte?  just declare a byte, then use Serial readByte() and writeByte methods instead of read() and write().

 

I especially would not do a new byte every time I entered that delegate - very inefficient.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.