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 comms 101


  • Please log in to reply
11 replies to this topic

#1 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 15 March 2012 - 04:18 PM

I've created a fairly simple program to write a string to a serial port:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace DisplayWriter1
{
    public class Program
    {
        public static void Main()
        {
             var serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);

             serial.Open();
             serial.Flush(); 

             string DataPacket = "<L1><PA><FE><MA><WC><FE>welcome";

             string hexValue = "0E"; // xorValue.ToString("X2");
                 
             byte[] byteData = Encoding.UTF8.GetBytes("<ID01>" + DataPacket + hexValue + "<E>");
             serial.Write(byteData, 0, byteData.Length);
             Thread.Sleep(100);
             serial.Close();
         }
    }
}
Ultimately, this will be used to display data on one of those scrolling dot-matrix LED signs.

When I connect the DB9 to HyperTerminal (which is set to 9600,8N1) nothing appears on the screen. I've connected Pins 2&3 on the serial plug to digital pins 1 and 0 on the NetDuino, and pin 5 on the serial plug to digital pin 14 (GND) on the ND.

If it's a fault in the ND, it's replicated on two of them (so I don't think it's that).

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 15 March 2012 - 05:53 PM

Hi Gorf, Are you using an RS232 serial cable? If so, it's probably sending dangerous RS232 voltages (-15V to 15V) to your Netduino's digital pins. The serial port on the Netduino uses TTL (0V to 5V) signals. You can pick up an RS232 shield and plug that into your Netduino...and that will convert the higher-voltage RS232 signals into a lower-voltage TTL signal for your Netduino. Chris

#3 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 15 March 2012 - 05:56 PM

Hello Gorf.
Your program is correct, although you should "halt" the program flow from exiting the Main method.
To do that, you can add:
Thread.Sleep(Timeout.Infinite);
as the latest operation.

However, that's not absolutely the reason because you don't seen anything in the PC.
The problem has been bumped many times, and it's related to the voltage levels of the Netduino (0...+3.3V), and the RS-232 (-10V...+10V). I don't think you have fried your Netduino, but I wouldn't make many attempts...it's easy to damage the Netduino by applying voltages above/under the max absolute limits.

The Neduino gives you the serial pins as they are: just as the micro-controller exposes them.
Instead, the RS-232 protocol implies a more complex interface, which is basically a level-shifter.
You have two choices to connect the machines safely, and reliably:
  • purchase a shield doing that (like this one);
  • build your own shifter by using a MAX232 chip (much cheaper, but requires a minimum of skill).
I would *NOT* buy this one: it's too trivial and unreliable.
Hope it helps.
Biggest fault of Netduino? It runs by electricity.

#4 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 16 March 2012 - 05:11 PM

Many thanks for the replies. I've bought this one.

The final version will have other functionality, so it will have timers and an infinite sleep. I tried to keep this one as simple as possible.

Anyone know why the commented out but comes up with mscorlib.dll "Exception was thrown: System.ArgumentException" string? xorValue is being calculated correctly in the pre-slimmed-sown version. Exception is thrown whether it's a byte or an integer...

That one might need to be a new thread...

#5 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 02 April 2012 - 11:18 AM

well, my RS232 shield finally arrived, and it has had no effect at all. Now there's not even a flicker from the LED sign. It's not that the ND+ was broken with previous attempts, either: I'm using a different one... The program is as above (except when I took the photo, the program was using COM2), but I'm now using a ND+ that's been flashed to 4.2, as well as the 4.2 framework. Any ideas?

Attached Files



#6 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 02 April 2012 - 11:35 AM

Hi Gorf, In the picture you have not connected any power supply to the breakout board. Try running a red wire from the breakout board VCC pin to the Netduino 3.3V socket. Paul

#7 GlenS

GlenS

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationTamworth, UK

Posted 02 April 2012 - 11:40 AM

Hi Gorf, Also, have you got the Rx and Tx pins connected correctly. It looks like you have the Rx from the breakout connected to the Rx (pin 0) of the N+, and the same therefore with tthe Tx pins. These should be in a cross-over configuration - ie. Tx from breakout -> Pin 0 N+, Rx from breakout to Pin 1 N+ Glen

#8 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 02 April 2012 - 06:02 PM

In the picture you have not connected any power supply to the breakout board.
Try running a red wire from the breakout board VCC pin to the Netduino 3.3V socket.

Thanks Paul - that was the issue. I'm still getting a bit of garbage after transmitting the data, but I think it's because I'm using patch cables which might just be a bit loose

(Incidentally, the hex conversion I mentioned in an earlier post is now working thanks to the update to NETMF 4.2)

Also, have you got the Rx and Tx pins connected correctly. It looks like you have the Rx from the breakout connected to the Rx (pin 0) of the N+, and the same therefore with tthe Tx pins. These should be in a cross-over configuration - ie. Tx from breakout -> Pin 0 N+, Rx from breakout to Pin 1 N+

ND+ Pin 0 <-> RxOUT<--
ND+ Pin 1 <-> TxIN-->
ND+ Pin 14 <-> GND
ND+ 3v3 <-> VCC

I can see how it might look different because of perspective in the photo

#9 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 03 April 2012 - 02:41 PM

OK I thought I'd add to this thread rather than starting a new one. Can anyone think why this might not be working?

Here's my code:
using System.IO.Ports;
using System.Text;
using System.Threading;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace SignWriterV2
{
    public class Program
    {
        public static void Main()
        {
            SerialPort serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
            serial.Open();
            byteData = Encoding.UTF8.GetBytes("<ID00><L1><PA><FE><MA><WB><FE><AC><CA>Total <CD>999999<CG>kWh40<E>");
            serial.Write(byteData, 0, byteData.Length);
            Thread.Sleep(100);
            serial.Flush();
            Thread.Sleep(100);
            serial.Close();
        }
    }
}
If I send the string direct to my LED device via serial port from the PC (using PuTTY on serial at 9600 8N1) the sign updates accordingly. To do this I have to have the string in the clipboard, and right-click on the PuTTY window.

If I run the ND+ code when it's connected via RS232 shield to the PC, PuTTY receives the string and displays it.

However, If I run the ND+ code when it's connected via RS232 shield to my LED device, I get no response from the device.

Any ideas? Best I can come up with is a broken gender changer... (needed for ND+ to LED, not needed for ND+ to PC or PC to LED)

#10 Ralim

Ralim

    Member

  • Members
  • PipPip
  • 13 posts

Posted 12 April 2012 - 07:06 AM

Hello, Just to throw in an idea, try throwing in a "\n" or "\r" for the newline and carrage return. The sign may wait for the new line signal before it processes the message... -Ralim

#11 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 16 April 2012 - 11:09 AM

Thanks for the reply, Ralim. I should have updated this when I got it working. The LED sign uses the angle brackets as its delimiters, so linefeeds etc aren't needed. I managed to get it working with a null modem cable. It turns out that simply swapping the TX and RX jumpers to the RS232 shield was not acceptable.

#12 Chip Moody

Chip Moody

    New Member

  • Members
  • Pip
  • 2 posts

Posted 27 April 2012 - 04:49 AM

It turns out that simply swapping the TX and RX jumpers to the RS232 shield was not acceptable.


That's an easy assumption to make, but as you discovered you're really dealing with TWO sets of com polarities there - a TX/RX set between the ND and your breakout board, and a different set between the BOB and the device you're trying to ultimately talk to.

Swapping TX/RX jumpers from the ND will not have the desired effect of changing the TX/RX on the RS232 connector - it simply kills your communication between the ND and the chip on the BOB.

- Chip




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.