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 ports and Bluetooth I/O and GPIO output

serial port Bluetooth I/O

  • Please log in to reply
12 replies to this topic

#1 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 24 February 2015 - 12:04 AM

Hello!

Okay here's the code

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.Text; 
using System.IO.Ports;   

namespace Bluetooth
{
    public class Program
    {
        static SerialPort serial;
        public static void Main()
        {
            // initialize the serial port for COM1 (pins D0 and D1)             
            serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);             
            // open the serial-port, so we can send and receive data             
            serial.Open();             
            // add an event-handler for handling incoming data             
            serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);       
            //wait until the end of the Universe :-)
            
            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);             
            }         
        }       
        
    }
}

It was lifted from this site:

http://blogs.msdn.co...oframework.aspx

 

It does what its supposed to. Picks up characters tossed at the attached Bluetooth device from one connected to the host. It then sends it back and the terminal doing the sending promptly displays it.

 

I'm trying to figure out how to safely insert the usual steps to enable a GPIO or two or three or four to react to the digital stream being sent between the Serial Port 0 connection. 

 

Ideally the big problem is one of being able to definitely send the stream from the serial-in connection, then to say GPIO_D2, and then GPIO_D3 and GPIO_D4 and of course GPIO_D5, and then to the serial-out connection. All without breaking the flow of logic he wrote.

 

Incidentally that's the first and best example I found of being able to use the on-board serial port on the board. 

 

For clarification I have over in the general portion of the board, one labeled "New device Monday" and one on powering a Netduino in there, but I suspect it will need to be moved here.



Doctor Who
"This signature does not exist!"

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 24 February 2015 - 04:50 AM

Hello Who.

Instead of using the event, fire a thread endlessly running which consumes the bytes incoming (basically the event handler content).

I think should be more reliable as timing, because the events are dispatched sequentially, but there's no guarantee of accuracy and you may experience a messy stream...

 

Anyway, I'm not sure to understand what you want to do.

Do you want the Netduino:

  1. receive a message from the serial, then
  2. process the message by de/activating one or more I/Os, then
  3. send an answer out on the same serial

Is that what you want?


Biggest fault of Netduino? It runs by electricity.

#3 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 24 February 2015 - 05:19 AM

Hello Who.

Instead of using the event, fire a thread endlessly running which consumes the bytes incoming (basically the event handler content).

I think should be more reliable as timing, because the events are dispatched sequentially, but there's no guarantee of accuracy and you may experience a messy stream...

 

Anyway, I'm not sure to understand what you want to do.

Do you want the Netduino:

  1. receive a message from the serial, then
  2. process the message by de/activating one or more I/Os, then
  3. send an answer out on the same serial

Is that what you want?

 

Hello!

Close enough.

What I'm looking for, is to have the Netduino process the message and since its single character ASCII then dump it in the same serial form out one or more I/O points and then pass it back unchanged to the serial out connection.

 

What that author created is what I was looking for as an excellent example of serial communications on the board and even the use of these serial out Bluetooth devices.



Doctor Who
"This signature does not exist!"

#4 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 24 February 2015 - 06:27 AM

If the characters arrive with a relative "distance" (in terms of time) each other, the original snippet should be good. I wondering how could happen whenever a long stream of bytes arrive at once.

 

At this point, where's the problem on inserting the I/O control inside the loop?

 

BTW, I believe the Netduino could be a bit touchy: remove the "until the end of the Universe" comment, then re-run the program...that might yield a way better result.

 

Cheers


Biggest fault of Netduino? It runs by electricity.

#5 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 24 February 2015 - 05:17 PM

Hello!

Okay the story so far. I pulled up the code and examined it. I then found a known working example and copied a reference for the D2 I/O connector for the board. I then inserted the seemingly correct definition into it. Here's the code:

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.Text; 
using System.IO.Ports;
  

namespace Bluetooth
{
    public class Program
    {
        static SerialPort serial;
        public static void Main()
        {
           
            // initialize the serial port for COM1 (pins D0 and D1)             
            serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);             
            // open the serial-port, so we can send and receive data             
            serial.Open();             
            // add an event-handler for handling incoming data             
            serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);       
            //wait until the end of the Universe :-)
         OutputPort d2 = new OutputPort(Pins.GPIO_PIN_D2, false);
         OutputPort d3 = new OutputPort(Pins.GPIO_PIN_D3, false);
            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);
                d2.Write(bytes, 0, bytes, bytes.Length);
                d3.Write(bytes, 0, bytes, bytes.Length);
            }         
        }       
        
    }
}

Instead those two inserts after the serial read instructions causes an error. Here:

 
Error 1 The name 'd2' does not exist in the current context C:\Users\GCL.CMDRSKYWALKER.000\documents\visual studio 2010\Projects\Bluetooth0\Bluetooth0\Program.cs 45 17 Bluetooth0
Error 2 The name 'd3' does not exist in the current context C:\Users\GCL.CMDRSKYWALKER.000\documents\visual studio 2010\Projects\Bluetooth0\Bluetooth0\Program.cs 46 17 Bluetooth0

As usual the programming IDE does not provide enough information. I have all of the using something definitions defined. I even have the write definition for the term included. Ideally the IDE needs to have more comprehensive help added.


Doctor Who
"This signature does not exist!"

#6 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 24 February 2015 - 05:19 PM

Hello!

More information. Here's the known working example that I chose that contained that term.

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

namespace B7
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            OutputPort a0 = new OutputPort(Pins.GPIO_PIN_D2, false);
            OutputPort a1 = new OutputPort(Pins.GPIO_PIN_D3, false);
         while (true)
            {
                     Thread.Sleep(250);
                  a0.Write(false); //
                  a1.Write(false); // O0 output engaged 
                  Thread.Sleep(2500);
                  a0.Write(true); //
                  a1.Write(false);  // O1 output engaged
                  Thread.Sleep(2500);
                  a0.Write(false); //
                  a1.Write(true);  // O2 output engaged
                  Thread.Sleep(2500);
                  a0.Write(true);  //
                  a1.Write(true);  // O3 output engaged
                  Thread.Sleep(2500);
                  a0.Write(false); //
                  a1.Write(false); // O4 output engaged 
         

            }

        }

    }
}

The premise there is that it toggles two lines and causes the attached device to step through a series of sequences. It worked, but, ah, the responses were peculiar. The project was eventually abandoned.



Doctor Who
"This signature does not exist!"

#7 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 25 February 2015 - 04:11 AM

Okay now it becomes peculiar. I relocated the definitions inside the loop and corrected a typo in the variables being presented to the output port. Now I'm getting this error:

Error	1	No overload for method 'Write' takes 3 arguments	C:\Users\GCL.CMDRSKYWALKER.000\documents\visual studio 2010\Projects\Bluetooth0\Bluetooth0\Program.cs	46	17	Bluetooth0
Error	2	No overload for method 'Write' takes 3 arguments	C:\Users\GCL.CMDRSKYWALKER.000\documents\visual studio 2010\Projects\Bluetooth0\Bluetooth0\Program.cs	47	17	Bluetooth0

The code fragment that triggered it is:

  d2.Write(bytes, 0, bytes.Length);
  d3.Write(bytes, 0, bytes.Length);

Its the same thing inside the parenthesis being used to send and receive data regarding the serial port into and out the attached Bluetooth device.

 

Help, ah, doesn't.

What I'm looking for obviously is a means to send into and out of the GPIO connections the same data circling through the serial port. The reluctance of the Visual Studio obviously confirms that it doesn't like the sequence of events.



Doctor Who
"This signature does not exist!"

#8 Juzzer

Juzzer

    Advanced Member

  • Members
  • PipPipPip
  • 135 posts
  • LocationHertfordshire, UK

Posted 25 February 2015 - 11:16 AM

using System.IO.Ports;
using Microsoft.SPOT.Hardware;
 
namespace MFConsoleApplication2
{
    public class Program
    {
        private static SerialPort _serial;
        private static OutputPort _d2, _d3;
 
        public static void Main()
        {
            _d2 = new OutputPort(Cpu.Pin.GPIO_Pin0, false);
            _d3 = new OutputPort(Cpu.Pin.GPIO_Pin1, false);
 
            _serial = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            _serial.DataReceived += _serial_DataReceived;
            _serial.Open();
        }
 
        static void _serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] bytes = new byte[1];
            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);
 
                _d2.Write(!_d2.Read());
                _d3.Write(!_d3.Read());
            }         
        }
    }
}


#9 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 25 February 2015 - 05:04 PM

 

using System.IO.Ports;
using Microsoft.SPOT.Hardware;
 
namespace MFConsoleApplication2
{
    public class Program
    {
        private static SerialPort _serial;
        private static OutputPort _d2, _d3;
 
        public static void Main()
        {
            _d2 = new OutputPort(Cpu.Pin.GPIO_Pin0, false);
            _d3 = new OutputPort(Cpu.Pin.GPIO_Pin1, false);
 
            _serial = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            _serial.DataReceived += _serial_DataReceived;
            _serial.Open();
        }
 
        static void _serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] bytes = new byte[1];
            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);
 
                _d2.Write(!_d2.Read());
                _d3.Write(!_d3.Read());
            }         
        }
    }
}

 

 

 

Hello!

Thanks man. That'll work. I'll know in about fifteen or twenty minutes if it works. It'll certainly replace the original application, which was exported from that blog. 

 

However trying to put that code into the IDE promptly causes an error message to pop up about a missing { symbol, and of course the system can not explain what it means.  

 

More about:

Okay Juzzer, I removed what I copied before, and copied it again, and then stuck it into my framework. It then cleared the errors that I put there before.

 

To Chris:

I'm not sure how much input you have into the folks at Microsoft for the Framework development, but it would be nice if the error messages also produced meaningful output.

 

Consider this, during the days of running ordinary BASIC on the PC, if the user made a mistake in a line, the interpretation sequencer would complain with a syntax error message. And it would be obvious.

 

But all of what I've entered into this thread regarding errors, aren't. They are obscuring the responses with FUD on the screens which surface when such clarification is requested.


Edited by Dr Who, 25 February 2015 - 10:05 PM.


Doctor Who
"This signature does not exist!"

#10 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 25 February 2015 - 11:04 PM

Okay group thank you for all of your help. This was amazing.

 

However it didn't work as expected. And I'm more annoyed at Microsoft then anything (or anyone) else.

 

If anything to make this work more, we need a heck of a lot of other examples to support this whole truly amazing idea.



Doctor Who
"This signature does not exist!"

#11 Juzzer

Juzzer

    Advanced Member

  • Members
  • PipPipPip
  • 135 posts
  • LocationHertfordshire, UK

Posted 26 February 2015 - 09:58 AM

I'm a bit confused what you are actually trying to achieve and what examples are missing to get you going...

 

Are you just trying to achieve BT comms?



#12 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 26 February 2015 - 01:01 PM

  d2.Write(bytes, 0, bytes.Length);
  d3.Write(bytes, 0, bytes.Length);

Its the same thing inside the parenthesis being used to send and receive data regarding the serial port into and out the attached Bluetooth device.

 

Help, ah, doesn't.

What I'm looking for obviously is a means to send into and out of the GPIO connections the same data circling through the serial port. The reluctance of the Visual Studio obviously confirms that it doesn't like the sequence of events.

 

It sounds like you need to bit-bang the data through the GPIO ports yourself.  Writing a series of bytes to a GPIO pin is not supported natively you can only set the pin high or low.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#13 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 26 February 2015 - 04:00 PM

It sounds like you need to bit-bang the data through the GPIO ports yourself.  Writing a series of bytes to a GPIO pin is not supported natively you can only set the pin high or low.

 

Regards,

Mark

 

 

I'm a bit confused what you are actually trying to achieve and what examples are missing to get you going...

 

Are you just trying to achieve BT comms?

 

 

To answer both of you. What I was looking for was to have the board respond to the Bluetooth device. Besides passing the text back and forth between the serial I/O pins, I originally wanted the two chosen GPIO pins to do just that Nevyn, to blink back and forth between those character deliveries.

 

But it got too complicated, and I realized that yes indeed the GPIO pins do what you're thinking of, and that what's I'm good at. Then it would get more interesting from there. 

 

Besides, I'm off on something new regarding the older programs I already had debugged. But never fear I'll revisit this thread and even the problems soonest.



Doctor Who
"This signature does not exist!"





Also tagged with one or more of these keywords: serial port, Bluetooth I/O

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.