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

UART communication Netduino Plus


  • Please log in to reply
28 replies to this topic

#1 efrain7805

efrain7805

    New Member

  • Members
  • Pip
  • 3 posts

Posted 12 July 2012 - 03:35 AM

I'm new to netduino need to establish a communication with COM1 netduino and PC serial Port area using "Tera Term" but communication does not work I press the letter "a" on the keyboard and returns me another character. I'm working with a Baude Rate of 9600. Alguine I can say I'm doing wrong? Thank you. I leave the source code.

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

namespace NetduinoPlusApplication1
{

    public class Program
    {
        static SerialPort serial;
        static OutputPort Led = new OutputPort(Pins.ONBOARD_LED, false);

        public static void Main()
        {
            // initialize the serial port for COM1 (using D0 & D1)        
            serial = new SerialPort("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)
        {
            int bytesToRead = serial.BytesToRead;
            //start reading the stream 
            if (bytesToRead > 0)
            {
                // get the waiting data 
                byte[] buffer = new byte[bytesToRead];

               serial.Read(buffer, 0, buffer.Length);

               serial.Write(buffer, 0, buffer.Length);

                Led.Write(true);
            }

            Led.Write(false);
        }
    }
}

Edited by Chris Walker, 12 July 2012 - 04:40 AM.
added [code][/code] tags


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 July 2012 - 04:43 AM

Hi efrain7805, Can you confirm that you're using all the same settings on the PC (8 data bits, no parity, one stop bit)? Also, what kind of cable are you using to attach your Netduino to the PC? RS232? USB-TTL? Are you doing any voltage level conversion? Welcome to the Netduino community, Chris

#3 efrain7805

efrain7805

    New Member

  • Members
  • Pip
  • 3 posts

Posted 12 July 2012 - 01:37 PM

thanks for responding. The configuaracion of PC is equal to the program but I'm not using using a voltage level converter. I directly connect the P0, P1 and ground directly to the Port netduino plus 232 of the PC to the Pines 2,3,5. if I use a voltage level converter which recommend? once again thank you very much.

#4 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 12 July 2012 - 02:52 PM

Can you post the character you recvive when you type "a" into the console?
Do you get a character like this? ž or ×?
If so then you need to logically invert the RX or TX lines as what you are receiving back to the console is the logical inverse of the byte you expected.

i.e.
//The letter 'a' in ascii byte values is 0x61
//In binary this is 0110 0001
//If the RX or TX lines are not inverting the response you will get a value of 0x9E (158 decimal)
//i.e. the non inverted value would be 1001 1110

Can you set a breakpoint on the line
serial.Write(buffer, 0, buffer.Length);
What do you see in the buffer, i.e. if you press the 'a' key you should see a byte array with a single entry with the value of 0x61 (or 97 if you haven't enabled hex view). If you see that then it seems likely that the culprit is the TX line which needs inverting, otherwise its the RX line.

I suspect as you are not using a voltage converter you are not getting the inversion done for free, so either use a voltage converter like a MAX232, or invert the RX and TX lines using a device like a 74HCT14N although i would recomend a purpose built RS232 level converter if you are likely to experience 12V serial signals which you would typically get from a PC serial port (although not always if you are using a USB serial port, dc-dc converters are expensive and 5v is enough to trigger most modern (later than 1980's) logic devices...)


Nak.

#5 efrain7805

efrain7805

    New Member

  • Members
  • Pip
  • 3 posts

Posted 17 July 2012 - 02:06 AM

friends, I connected the converter voltage levels and it worked excellent. I use max 3232. thank you again.

#6 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 18 July 2012 - 02:23 AM

I have been trying different examples all day. This one works the best but it still doesn't accept "SerialPort" saying "The type or namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?) What do I need?

#7 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 18 July 2012 - 01:30 PM

Also, I'm using COM2 since I need RTS and CTS. I have seen nothing on how to use them though. Where should I be looking? (A good reference?)

#8 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 20 July 2012 - 04:13 PM

Okay, I had to make "SerialPort" plural to accept it but it still doesn't work below in the program. On line with " serial = new SerialPorts("COM2", 9600, Parity.None, 8, StopBits.One); I get "Cannot create an instance of static class 'SecretLabs.NETMF.Hardware.NetduinoPlus.SerialPorts'"

#9 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 20 July 2012 - 09:17 PM

Hi Kentj,

Okay, I had to make "SerialPort" plural to accept it but it still doesn't work below in the program. On line with " serial = new SerialPorts("COM2", 9600, Parity.None, 8, StopBits.One); I get "Cannot create an instance of static class 'SecretLabs.NETMF.Hardware.NetduinoPlus.SerialPorts'"

If you take the plural "s" out of "= new SerialPorts", does that work? I believe that the class you're looking for is System.IO.SerialPort rather than 'SerialPorts'.

Chris

#10 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 21 July 2012 - 12:49 AM

With "SerialPort" I get the error "Type or namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?)"

#11 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 21 July 2012 - 01:34 AM

Hi Kentj,

With "SerialPort" I get the error "Type or namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?)"

Two quick questions...

1. Do you have Microsoft.SPOT.Hardware.SerialPort.dll added as a reference to your project?
2. Do you have the text "using System.IO.Ports;" at the top of your source code file?

Chris

#12 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 21 July 2012 - 04:23 PM

I have "using System.IO.Ports;" but how do you add "Microsoft.SPOT.Hardware.SerialPort.dll" to the references. I have "Microsoft.SPOT.Hardware" under which heading is "{}Microsoft.SPOT\ {}Microsoft.SPOT.Hardware\ {}System.IO.Ports" in references.

#13 Stefan

Stefan

    Moderator

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

Posted 21 July 2012 - 07:37 PM

I have "using System.IO.Ports;" but how do you add "Microsoft.SPOT.Hardware.SerialPort.dll" to the references. I have "Microsoft.SPOT.Hardware" under which heading is "{}Microsoft.SPOT\ {}Microsoft.SPOT.Hardware\ {}System.IO.Ports" in references.

Click on "Project -> Add reference" in the menu bar at the top. Now select Microsoft.SPOT.Hardware.SerialPort.
"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

#14 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 22 July 2012 - 03:55 AM

Microsoft.SPOT.Hardware.SerialPort was not found anywhere on my computer with or without the .dll

#15 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 July 2012 - 05:15 AM

Hi Kentj,

Microsoft.SPOT.Hardware.SerialPort was not found anywhere on my computer with or without the .dll


In "Add References", go to the ".NET" tab. The component name should be "Microsoft.SPOT.Hardware.SerialPort".

They're not necessarily in alphabetical order.

If it's not there, then your current project may not be a .NET Micro Framework project or the SDK might not be installed properly on your computer.

Chris

#16 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 23 July 2012 - 02:38 PM

There is nothing under the ".NET" tab. I re-downloaded .NET Micro Framework making sure it was for the Netduino Plus and not the Netduino Go. Got out of Visual Studio and back in again. Re-loaded my program which is a .NET Micro Framework project. and there still is nothing under the .NET tab.

#17 Stefan

Stefan

    Moderator

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

Posted 23 July 2012 - 02:55 PM

Hi Kentj, Could you please go to "Project -> Project Properties" and make a screenshot? It should look like this: Attached File  project_properties.png   30.86KB   37 downloads Also, go to "Project -> Add Reference...". I have this list, you should add the selected reference: Attached File  add_reference.png   34.13KB   38 downloads It won't be there if it's the wrong project type though. The actual DLL is found here: C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\
"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

#18 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 23 July 2012 - 03:24 PM

Sorry, I don't remember how to do a screen shot since I haven't done it since the '80s (DOS). The "Project Properties" screen looks the same though. The other one does not.

#19 Stefan

Stefan

    Moderator

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

Posted 23 July 2012 - 03:30 PM

Sorry, I don't remember how to do a screen shot since I haven't done it since the '80s (DOS). The "Project Properties" screen looks the same though. The other one does not.

To make a screenshot, press the PrtScn button on your keyboard. That will add an image of the full screen in your clipboard. If you press Alt+PrtScn it will just do the active window.

Then go to an image editor, for example Microsoft Paint, paste (Ctrl+V) and store it to the disk.

I'm curious how it looks at your place. You got both SDKs installed? The general .NETMF SDK (http://netmf.codeple...oads/get/343358) and the Netduino SDK (http://www.netduino.com/downloads/)
"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

#20 Kentj

Kentj

    Member

  • Members
  • PipPip
  • 16 posts

Posted 23 July 2012 - 03:42 PM

We'll try this.




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.