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

RS232 Advice


  • Please log in to reply
14 replies to this topic

#1 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 23 August 2011 - 08:09 PM

Hi all, I want to connect my Netduino board to a piece of equipment that has an RS-232 port. What do I need to get started? To be more specific, I'm only interested in connecting to the device, not to a PC and I'm not looking for a Serial to USB Convertor. The device I'm using has a 9 pin female connector and uses a minimal 3 wire serial port. I only need to connect the TxD, RxD and ground. I thought I might be able to do this directly from the board, but that doesn't seem to work (the device has a light that flashes if it doesn't understand the serial communication it's receiving, I had a very simple program running on the Netduino to send a known good command when the on-board button is pressed, but it doesn't seem that the device recognizes it). The device runs on 5V, so maybe that's part of the problem. Any pointers or advice here would be greatly appreciated. I know I might need a shield, but I don't know which is the right one to get. Thanks! Edit: another related question. If I was to set up the Netduino to send a string of bytes on COM1, then I tied pin 1 back to pin 0, shouldn't I be able to read the exact same data back again?

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 24 August 2011 - 04:43 AM

Hello.
You should not connect the RS232 pins to the Netduino input, because they uses different voltage levels.
The Netduino works with voltages in the 0..+3.3V range (+5V allowed for some inputs). The RS232 works also with negative voltages swinging from -10V to +10V (approx).

What you looking for is a RS232 to TTL converter shield, like this one. There are plenty of similar shields, so you may search for others. However, you should bear in mind that the shield should be equipped with the MAX232 chip (or equivalent).
Cheers
Biggest fault of Netduino? It runs by electricity.

#3 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 24 August 2011 - 12:55 PM

Hello.
You should not connect the RS232 pins to the Netduino input, because they uses different voltage levels.
The Netduino works with voltages in the 0..+3.3V range (+5V allowed for some inputs). The RS232 works also with negative voltages swinging from -10V to +10V (approx).

What you looking for is a RS232 to TTL converter shield, like this one. There are plenty of similar shields, so you may search for others. However, you should bear in mind that the shield should be equipped with the MAX232 chip (or equivalent).
Cheers


Thanks for the advice. I'll try and get hold of the shield you suggested (or similar). My device that I'm trying to connect to claims to be 5V for the serial communication, but I guess it probably still swings -ve, so maybe that's the problem. It's expecting the message from the Netduino to go negative and it doesn't.
Still, shouldn't I be able to connect the Netduino pin 0 to pin 1 and make that board talk to itself? Just as a way to convince myself that the board is working and the right messages are getting sent? Needless to say, it ain't working. The values I'm getting back on port.Read aren't the same as what I sent out with port.Write. (edit: nevermind, it's working now)

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 24 August 2011 - 02:22 PM

My device that I'm trying to connect to claims to be 5V for the serial communication, but I guess it probably still swings -ve, so maybe that's the problem. It's expecting the message from the Netduino to go negative and it doesn't.

What kind of device? Perhaps it already works on TTL levels.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 24 August 2011 - 03:50 PM

What kind of device? Perhaps it already works on TTL levels.


It's proprietary and so far it looks like it doesn't work, for whatever reason.

#6 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 25 August 2011 - 05:26 AM

Still, shouldn't I be able to connect the Netduino pin 0 to pin 1 and make that board talk to itself?

Of sure you can!
The Netduino (standard and Plus) has two UARTs: I never remember which is the COM1 and the COM2.
Anyway, you may short the I/O #0 and #1, as long as the I/O #2 and #3. Afterward, you may try the echo by using the COM1. That must be work (without any additional hardware).
Cheers
Biggest fault of Netduino? It runs by electricity.

#7 Inquisitor

Inquisitor

    Advanced Member

  • Members
  • PipPipPip
  • 91 posts
  • LocationAtlanta, Georgia, USA

Posted 25 August 2011 - 11:22 AM

Of sure you can!
The Netduino (standard and Plus) has two UARTs: I never remember which is the COM1 and the COM2.
Anyway, you may short the I/O #0 and #1, as long as the I/O #2 and #3. Afterward, you may try the echo by using the COM1. That must be work (without any additional hardware).
Cheers


Boom! I was about to order another one (which I will eventually) but at least I can do a test now.
Thanks Mario for stating the obvious in a mild way so I don't feel too dense. :blink:
Doing my best to keep the smoke in the little black boxes.
If my message helped you... how 'bout giving me a Posted Image
www.MessingWithReality.com

#8 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 29 August 2011 - 06:46 PM

Hello.
You should not connect the RS232 pins to the Netduino input, because they uses different voltage levels.
The Netduino works with voltages in the 0..+3.3V range (+5V allowed for some inputs). The RS232 works also with negative voltages swinging from -10V to +10V (approx).

What you looking for is a RS232 to TTL converter shield, like this one. There are plenty of similar shields, so you may search for others. However, you should bear in mind that the shield should be equipped with the MAX232 chip (or equivalent).
Cheers


Okay. Finally got it working. I bought this shield because I could buy it through Amazon and apparently emartee don't take credit cards, only paypal (no good with the corporate p-card).
So everything seems to work, after messing around finding the right cross over cable I was able to send a command to my device and have it react. I haven't yet tried reading and interpreting the messages coming back yet, one step at a time. My only problem with the board I got is that it sits directly on top of the netduino. It blocks access to the onboard button, but I can live with that for now, but the real problem is that it clashes with power connector on the netduino board meaning that I can push it all the way down. Does anybody know a good source for some kind of spacer?

#9 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 30 August 2011 - 06:07 AM

The shield uses only two wires, plus the power supply. I would connect it using some wire.
Biggest fault of Netduino? It runs by electricity.

#10 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 30 August 2011 - 02:15 PM

The shield uses only two wires, plus the power supply. I would connect it using some wire.


I could do that, but I do really like the compactness of having the boards stacked like that. It'll really make finding an enclosure to fit everything a lot easier when I'm eventually done with this project.

#11 Inquisitor

Inquisitor

    Advanced Member

  • Members
  • PipPipPip
  • 91 posts
  • LocationAtlanta, Georgia, USA

Posted 04 September 2011 - 07:27 PM

Of sure you can!
The Netduino (standard and Plus) has two UARTs: I never remember which is the COM1 and the COM2.
Anyway, you may short the I/O #0 and #1, as long as the I/O #2 and #3. Afterward, you may try the echo by using the COM1. That must be work (without any additional hardware).
Cheers


I knew Mario wouldn't lead me astray... but I was just wanting to try this out. Its been a while since I've done any serial port communications and I thought I'd try to make it a little easier on the next guy just getting started. It just lights the on-board LED via the most round-about method possible :rolleyes: ... Pressing the switch writes a flag to the serial port 1 which is read on serial port 2 which turns on the LED. Releasing the switch goes through the same Mickey Mouse and turns off the LED. Just run a wire from UART 1 TX (pin 1) to UART 2 RX (pin 2) and drop in 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 SecretLabs.NETMF.Hardware.NetduinoPlus;  // Use this if you have N+ project.
using System.IO.Ports;
using System.Text;

// Hook up a wire from port 1 to port 2.

namespace Serial_Port
{
    public class Program
    {
        private static OutputPort _Led;
        private static InterruptPort _Switch;
        private static SerialPort _Send;
        private static SerialPort _Recv;

        public static void Main()
        {
            // Set up our switch and led
            _Led = new OutputPort(Pins.ONBOARD_LED, false);
            _Switch = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled,
                Port.InterruptMode.InterruptEdgeBoth);
            _Switch.OnInterrupt += new NativeEventHandler(Switch_OnInterrupt);

            // Set up our sender and receiver
            _Send = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
            _Recv = new SerialPort(SerialPorts.COM2, 9600, Parity.None, 8, StopBits.One);
            _Recv.DataReceived += new SerialDataReceivedEventHandler(Recv_DataReceived);

            _Send.Open();
            _Recv.Open();

            Debug.Print("READY TO PRESS BUTTON");
            Thread.Sleep(int.MaxValue);
        }

        private static void Switch_OnInterrupt(uint data1, uint state, DateTime time)
        {
            // While we're at it want to see if we see any garbage collections between
            // time interrupt was received and when we get it passed to us.

            bool on = state == 0;
            DateTime now = DateTime.Now;
            Debug.Print(now.ToString() + ", " + (time - now).Ticks.ToString() + ", " + on.ToString());

            // Make up a flag.
            string flag;
            if (on)
                flag = "T";
            else
                flag = "F";
            byte[] buffer = Encoding.UTF8.GetBytes(flag);

            _Send.Write(buffer, 0, buffer.Length);
        }


        private static void Recv_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            DateTime now = DateTime.Now;
            if (e.EventType == SerialData.Eof)
            {
                Debug.Print("EOF");
                return;
            }

            // Should do some error checking for valid serial port, size of receive, etc.
            SerialPort sp = (sender as SerialPort);
            if (sp.BytesToRead == 0)
                return;

            byte[] buff = new byte[sp.BytesToRead];

            sp.Read(buff, 0, sp.BytesToRead);

            string flag = new string(Encoding.UTF8.GetChars(buff));

            switch (flag)
            {
                case "T":
                    Debug.Print("Turn on LED  " + now.ToString());
                    _Led.Write(true);
                    break;

                case "F":
                    Debug.Print("Turn off LED  " + now.ToString());
                    _Led.Write(false);
                    break;

                default:
                    Debug.Print("Unrecognized Flag(" + flag + ") " + now.ToString());
                    break;
            }
        }
    }
}

Doing my best to keep the smoke in the little black boxes.
If my message helped you... how 'bout giving me a Posted Image
www.MessingWithReality.com

#12 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 13 September 2011 - 07:50 PM

Does anybody know a good source for some kind of spacer?


To answer my own question, and in case anybody else needs them, I found this.

#13 home-ent

home-ent

    New Member

  • Members
  • Pip
  • 2 posts

Posted 21 August 2014 - 03:30 PM

I've had a couple of compile errors, first I've gotten around was the system.net

i found relevant reference and added .system so thats now ok. but the second error is

 

The type or namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?)

 

why is the namespace not recognised?

i have an original netduino (netduino 1)

 



#14 home-ent

home-ent

    New Member

  • Members
  • Pip
  • 2 posts

Posted 21 August 2014 - 07:08 PM

i'm basically using the uart's to interact with a temperature and humidity sensor, once ive got the ports working i just need to write to them

eg, $sure humidity 

and it will return eg. 056%RH

 

or $sure temp -c

for a return eg. 023Centigrade

 

the next step is to read the numerical data (first three digits) into a variable so i can affect an output based on each value. 

i'm no programmer, this is the learning bit for me, i can handle the if then while  and set the outputs as required.

its setting the uart ports and reading the relative bits into the variable that i struggle with. 

as i said previously i'm still trying to get the serial port past the name space bit.

hard ware that's the easy bit for me i could probably build this in hard ware but it would be fixed levels. plus i wouldn't learn anything new, which was the point of getting the netduino. 

my netduino is the original original one using SDK4.1



#15 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 09 November 2014 - 01:02 AM

Hi home-ent,

...but the second error is
 
The type or namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?)
 
why is the namespace not recognised?

Do you have Microsoft.SPOT.Hardware.SerialPort.dll added as an assembly reference in your project? We include this in the default Netduino templates--but if you created a project from scratch you'll want to pull this in if you're using SerialPorts.

Welcome to the Netduino community,

Chris




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.