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

First steps: SerialPort problem


  • Please log in to reply
16 replies to this topic

#1 frajen

frajen

    New Member

  • Members
  • Pip
  • 3 posts

Posted 12 January 2013 - 02:25 PM

Hi guys,

I want to start one of my first project and i just copy this code in my VS2010 :

 

public class Program    {        static SerialPort serial;        public static void Main()        {            // initialize the serial port for COM1 (using D0 & D1)            serial = new SerialPort(SerialPorts.COM1, 115200, 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)        {            // create a single byte array            byte[] bytes = new byte[32];            // 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);            }        }    }

 

 

But VisualStudio cant find the class SerialPort !

I have created a Netduino plus Project.

 

please help me :)

 

mfg

frajen



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 January 2013 - 06:18 PM

Hi frajen, Be sure to add the following at the top of your code:
using System.IO.Ports;
Also, be sure that the Microsoft.SPOT.Hardware.SerialPort.dll assembly is added to your project. Chris

#3 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 17 February 2014 - 12:07 PM

hi chris,

m new to netduino world, m using a netduino plus 1 board,

the problem is m also using the above code for serial communication b/w n+ and the pc with an rs232 , and to obtain the output the hyperterminal software., nw the problem is the transmitting part is getting executed but receiving part is not working.

pls help me how to over come this problem.

thank you.



#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 February 2014 - 06:47 PM

Hi sayantan, Is your DataReceived event being fired? What kind of cable are you using between the PC and Netduino? Chris

#5 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 18 February 2014 - 06:09 AM

Hello Chris;

m using an usb cable b/w pc and n+ , and a 9 pin d-type connector for  max232 and pc.

and yes the Datareceived event is getting fired., bt unable to retrieve  the data.



#6 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 18 February 2014 - 06:12 AM

using System; 
using System.Net;
using System.Text;
using System.IO.Ports;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
 
namespace rs232
{
    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);
        }// write your code here
     
    }
}// this is the code m using 
in hyperterminal, if i press a charecter, it gets  displays  one time and the onboard led blinks, bt it doesnt send back the data.


#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 18 February 2014 - 07:30 AM

Hi sayantan,

Hello Chris; m using an usb cable b/w pc and n+ , and a 9 pin d-type connector for  max232 and pc. and yes the Datareceived event is getting fired., bt unable to retrieve  the data.

Are you feeding RS232-voltage signals to the Netduino directly? Or is there some sort of shield involved? Photos would help us understand your setup better. Please note that a USB-RS232 adapter may understand Netduino's 0-3.3V SerialPort output and display it in a terminal program on your computer...but the -5V to 5V (or up to -15V to 15V) signals of RS-232 will not work with your Netduino--and subjecting it to high voltages like that (without proper signal voltage conversion) may damage your Netduino. Chris

#8 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 19 February 2014 - 07:04 AM

:D hi chris,

vry srry  unable to update sorts, showing updation error . but m sure no problem in the set up , coding m facing problem.

i hope i cn manage with it.

bt very urgent , i have to implement modbus protocol  master side of it, in my netduino and rs485 , as my project submission due date is vry near.

cn u pls [pls pls help me in it??? 

thank you



#9 lifanek

lifanek

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationKatowice, Poland

Posted 19 February 2014 - 07:37 AM

Hi Sayatan,

check out this thread - maybe something helps

http://forums.netdui...serial +threads

 

And please - could You spend a little bit more time on writing Your posts? It's hard to read.

 

Just like Chris I think You should use some MAX232 for PC communication (or USB to TTL). Also try Debug.Print(buffer.ToString());

 

For RS485 use MAX485 or 75176 and then connect one pin from Arduino to pin 2 and 3 in MAX. If it's LOW then it's receiving and when it goes HIGH You are able to transmit.

 

Example - Pin 3 connected to MAX485 pin 2 and 3

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]static OutputPort pin = new OutputPort[/color](Pins.GPIO_PIN_D3, false);

 

so pin is all the time low so event for reading can be fired.

and then later in code You should have something like this

 

pin.Write(true)

Serial.Write("test");

pin.Write(false)



#10 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 19 February 2014 - 09:19 AM

hello lifanek,

i'm really sorry first of all., i will definetly keep in mind what u said.

 

and yes i'm already using a [color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]MAX232 for PC communication (or USB to TTL)., [/color]

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]i have an ADM3485 IC for RS485, will it work.????[/color]



#11 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 19 February 2014 - 09:27 AM

Hey Lifanek,

 

I gone through the [color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]thread u suggested and i tried out  the coding, its displaying only "[/color]transmit thread works" after every 5 sec

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]I have a doubt that for receiving the output,  are u using any " hyperterminal software" or  any such software???[/color]

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]Thank You..[/color]



#12 lifanek

lifanek

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationKatowice, Poland

Posted 19 February 2014 - 11:29 PM

This code was made by me from other examples just for testing purposes (this is why it is printing "transmit thread works" - i just wanted to know that the thread is running).

 

For communicating with PC I have used Arduino Mega and Arduino IDE serial monitor - the only job for arduino was read from one UART and write it to second (read from Netduino and write to PC) and same thing in other direction (read from PC and write to netduino).



#13 lifanek

lifanek

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationKatowice, Poland

Posted 19 February 2014 - 11:32 PM

I am also testing 485 through 75176 but I'm getting some noise. Using this code:

static OutputPort RW485 = new OutputPort(Pins.GPIO_PIN_D2, true); //False - write / True - read

static void transmit()
{
while (true)
{
lock (monitor)
{
RW485.Write(true);
const string input = "Test$n";
byte[] sender = Encoding.UTF8.GetBytes(input);
serial.Write(sender, 0, sender.Length);
RW485.Write(false);
}
Thread.Sleep(5000);
}
}

This should give You Test$ message over 485 every 5 sec

 

Check out this:

 

http://img80.imagesh...63/43013093.png

 

it is from polish electronic forum and guys was trying to use 75176 without noise (You can see there some pull up resistors etc.) - I will try this... maybe next week

 

 

 

hello lifanek,

i'm really sorry first of all., i will definetly keep in mind what u said.

 

and yes i'm already using a [color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]MAX232 for PC communication (or USB to TTL)., [/color]

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]i have an ADM3485 IC for RS485, will it work.????[/color]

 

Check out data sheets for this device - for example MAX485 and 75176 for me look like same device just made by other companies - maybe ADM3485 is just like that.



#14 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 23 February 2014 - 04:30 AM

HI Chris, 

this is the shot of my set-up, but still i'm unable to communicate with pc. The netduino on-board led links when ever i press a botton from the keyboard.

Attached Files



#15 sayantan

sayantan

    Member

  • Members
  • PipPip
  • 10 posts
  • Locationindia

Posted 23 February 2014 - 04:31 AM

 

using System; 
using System.Net;
using System.Text;
using System.IO.Ports;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
 
namespace rs232
{
    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);
        }// write your code here
     
    }
}// this is the code m using 
in hyperterminal, if i press a charecter, it gets  displays  one time and the onboard led blinks, bt it doesnt send back the data.

 

this is the coding i'm using.



#16 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 23 February 2014 - 07:05 AM

Hi sayantan, Your best bet here is that there's some sort of voltage level conversion issue--or a loose wire, etc. What voltage is your TTL-RS232 logic level converter expecting? If it's 3.3V, then that's fine. If it's 5V then it may not be picking up on the Netduino's output data signals properly. Also...have you double-checked your Ground connection from Netduino to PC (i.e. they must both be sharing a common ground)? Chris

#17 Fahrice

Fahrice

    New Member

  • Members
  • Pip
  • 2 posts

Posted 23 March 2014 - 06:54 PM

This post seems to be about a month old so maybe you've already figured out the solution, but...

 

It seems to me that what is likely happening is HyperTerminal is not configured to display the keys you're typing - aka "local echo".  If this is true, then what is happening is:

 

1. you type a character (it is NOT displayed in HyperTerminal)

2. your Netduino receives the character

3. your Netduino blinks the LED

4. your Netduino sends the character back to HyperTerminal

5. HyperTerminal displays what it received

 

The question is - what are you expecting to see?  If you press "A", are you expecting to see "AA"?

 

Try looking for a setting in HyperTerminal that allows you to turn ON local echo.

 

I ran into this same exact issue with Putty this weekend.

 

Hope this helps someone!






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.