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 to PC just wont work, What am I Doing Wrong


  • Please log in to reply
9 replies to this topic

#1 Belatu

Belatu

    New Member

  • Members
  • Pip
  • 2 posts

Posted 06 April 2011 - 07:57 AM

I'm trying to get data from a Netduino to PC via Serial, But it just won't work I Must be doing something wrong.

my Code for sending the serial data is simple and straightforward:

public class Program
    {

        private static SerialPort m_SerialPort;
        static OutputPort ledPort = new OutputPort(Pins.ONBOARD_LED, false);
        static InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);
        static bool state = false;
        public static void Main()
        {
            // write your code here
            
            m_SerialPort = new SerialPort(Serial.COM2, 9600, Parity.None, 8, StopBits.One);
            m_SerialPort.Open();
            
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            while (true)
            {
                if (state == true)
                {
                    ledPort.Write(true);
                    SendSerialData("Hello World");
                    ledPort.Write(false);
                    Thread.Sleep(2000);
                }
                
            }

        }

        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            state = !state;
        }


        public static bool SendSerialData(string sendData)
        {
            // Declare the required variables
            bool returnValue = false;
            byte[] sendBuffer;

            try
            {
                // Make sure we have a open Serial Port
                if ((m_SerialPort != null) && (m_SerialPort.IsOpen))
                {   // Create the Send Buffer
                    sendBuffer = UTF8Encoding.UTF8.GetBytes(sendData + "\n");
                    m_SerialPort.Write(sendBuffer, 0, sendBuffer.Length);
                }
            }
            catch (Exception runtimeException)
            {

                Debug.Print("[WMServerComms.SendSerialData] " + runtimeException.ToString());

            }

            // Return to calling method
            return (returnValue);
        }

    }

So I'm Sending the following Value
Text: "Hello World"
HEX: "48 65 6C 6C 6F 20 57 6F 72 6C 64"
Bytes: "72 101 108 108 111 32 87 111 114 108 100"

On the PC my Code is as follows:

class Program
    {
        static SerialPort com1 = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One);
        static void Main(string[] args)
        {
            com1.DataReceived += new SerialDataReceivedEventHandler(com1_DataReceived);
            com1.Open();
            Console.ReadKey();
        }

        static void com1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            byte[] output = new byte[com1.BytesToRead];
            //string value = 
            com1.Read(output, 0, com1.BytesToRead);
            foreach (byte item in output)
            {
                Console.WriteLine(item.ToString());
            }
        }
    }

However the Data I Receive is
Text: "«::.ú‹….'7ë."
HEX: "AB 3A 3A 0A FA 8B 85 1B 27 37 EB 00"

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 06 April 2011 - 08:31 AM

Hi Belatu, What type of cable are you using between your Netduino and your PC? USB-TTL? An RS232 shield? Welcome to the Netduino community, Chris

#3 renatoa

renatoa

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationRomania

Posted 06 April 2011 - 08:41 AM

I guess he connected directly Netduino TTL serial to PC RS232 serial. I did the same mistake some time ago.

#4 Flores

Flores

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 06 April 2011 - 08:53 AM

If the cable is the problem, and you want a cheap solution for converting the signals you could use the TI MAX232. Works for me.

#5 Stefan

Stefan

    Moderator

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

Posted 06 April 2011 - 09:20 AM

I've successfully used these two breakouts for serial communication with the Netduino which convert TTL to RS232.

Breakout Board for FT232RL USB to Serial
RS232 Shifter SMD

Both are connected the same to the netduino, only difference is the PC-side.

Gnd -> Gnd
Vcc 3.3V -> 3.3V
TX-O -> Pin 0
RX-I -> Pin 1

On the PC plug it in the COM port or USB port, depending on the board you're using.
"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

#6 Belatu

Belatu

    New Member

  • Members
  • Pip
  • 2 posts

Posted 06 April 2011 - 03:05 PM

Yes I'm connecting directly to the Netduino Digital IO Pins. I take it that this means I'm receiving a TTL signal and not RS232.

#7 Stefan

Stefan

    Moderator

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

Posted 06 April 2011 - 03:13 PM

Yes I'm connecting directly to the Netduino Digital IO Pins. I take it that this means I'm receiving a TTL signal and not RS232.

That is correct. The signal should be transformed from TTL to RS232 as far as I know. There are several ways to do it, the break-out boards I mentioned before are the easiest way to do so.
"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

#8 Dan Morphis

Dan Morphis

    Advanced Member

  • Members
  • PipPipPip
  • 188 posts

Posted 06 April 2011 - 06:08 PM

That is correct. The signal should be transformed from TTL to RS232 as far as I know. There are several ways to do it, the break-out boards I mentioned before are the easiest way to do so.


Look at the output of a cheap USB->Serial adapter. I got lucky that the cheap PL2303 one I had didn't do true RS232 signal levels, it did TTL levels so I was able to directly connect it to my Netduino.

To test this, attach one probe of a multimeter to pin 5 (GND) of the adapter, and the other to pin 5 (DTR). If you see anything higher than 5V, then your adapter does true RS232 signal levels and you can't directly connect it to your Netduino. You might have to hack up a quick program to set the DTR line high in order to get accurate results, I don't recall the initial state of that pin.

#9 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 06 April 2011 - 07:27 PM

To test this, attach one probe of a multimeter to pin 5 (GND) of the adapter, and the other to pin 5 (DTR). If you see anything higher than 5V, then your adapter does true RS232 signal levels and you can't directly connect it to your Netduino. You might have to hack up a quick program to set the DTR line high in order to get accurate results, I don't recall the initial state of that pin.


To add to what Dan said...

TTL signals use 0V to represent "binary 0" and 3.3V/5V to represent "binary 1". The signals never go below ~0V.

RS232 signals on the other hand use 5V-12V to represent "binary 0" and -5V-12V (negative voltage) to represent "binary 1".

Some USB-RS232 adapters will only output -5V to 5V signals...which seems to work with most/all RS232 equipment.

Chris

#10 maximation

maximation

    New Member

  • Members
  • Pip
  • 1 posts

Posted 11 June 2011 - 11:11 AM

You can buy a MAX232 chip that with a few external caps will convert between TTL and True RS232 http://en.wikipedia.org/wiki/MAX232




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.