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

How to communicate to your PC using the serial port?


  • Please log in to reply
2 replies to this topic

#1 Nerdiator

Nerdiator

    New Member

  • Members
  • Pip
  • 3 posts

Posted 22 June 2016 - 02:34 PM

I can't seem to get Serial Communication with USB working. To clarify my problem, here is the code on my netduino 3

 

public class Program
    {
        static SerialPort _serial;
        private static Lcd _lcd;
        public static void Main()
        {
            _serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
            _serial.Open();
            _serial.DataReceived += _serial_DataReceived;


            var lcdProvider = new GpioLcdTransferProvider(Pins.GPIO_PIN_D12,    // RS
                                                        Pins.GPIO_PIN_D11,    // ENABLE
                                                        Pins.GPIO_PIN_D5,     // DB4
                                                        Pins.GPIO_PIN_D4,     // DB5
                                                        Pins.GPIO_PIN_D3,     // DB6
                                                        Pins.GPIO_PIN_D2);    // DB7


            _lcd = new Lcd(lcdProvider);


            _lcd.Begin(20, 2);
            
            Thread.Sleep(100);
            _lcd.Clear();
            _lcd.SetCursorPosition(0, 1);


            Thread.Sleep(100);
            _lcd.Write("Initing...");
            Thread.Sleep(100);
            _lcd.Clear();
            _lcd.SetCursorPosition(0, 1);
            _lcd.Write("Waiting...");


            Thread.Sleep(500);
            while (true)
            {
                _lcd.SetCursorPosition(0, 0);
                _lcd.Clear();
                _lcd.Write(_serial.IsOpen.ToString());
                Thread.Sleep(1000);
            }
            //Thread.Sleep(Timeout.Infinite);




        }


        private static void _serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {


            _lcd.Clear();
            _lcd.Write("Connected");
            // create a single byte array
            byte[] bytes = new byte[1];


            // 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);


                string data = Convert.ToBase64String(bytes);
                _lcd.Write(data);


            }


        }
    }

and this is the code on the PC application that's running:

 

public partial class MainWindow : Window
    {
        private SerialPort serial;


        public MainWindow()
        {
            InitializeComponent();
            AssettoCorsa assettoCorsa = new AssettoCorsa();
            assettoCorsa.PhysicsUpdated += AssettoCorsa_PhysicsUpdated;




            serial = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            serial.DataReceived += Serial_DataReceived;
            serial.Open();


            assettoCorsa.Start();
        }




        private void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // wait a little for the buffer to fill
            System.Threading.Thread.Sleep(100);


            // create an array for the incoming bytes
            byte[] bytes = new byte[serial.BytesToRead];
            // read the bytes
            serial.Read(bytes, 0, bytes.Length);
            // convert the bytes into a string
            string line = System.Text.Encoding.UTF8.GetString(bytes);


            // write the received bytes, as a string, to the console
            System.Console.WriteLine("echo: " + line);
            System.Console.WriteLine();
        }


        private void AssettoCorsa_PhysicsUpdated(object sender, PhysicsEventArgs e)
        {
            Console.WriteLine(e.Physics.Gear + @"-------" + e.Physics.SpeedKmh);
            if (serial.IsOpen)
            {
                serial.WriteLine(e.Physics.SpeedKmh.ToString(CultureInfo.InvariantCulture));
            }
           
        }
    }


But when I send data, nothing happens on the Netduino. The ports are open though

 



#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 22 June 2016 - 03:28 PM

Are you expecting to use the USB on the Netduino itself or are you using a USB to serial cable?

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 24 June 2016 - 08:35 AM

Hi, if you're expecting to be able to do serial communication between your Netduino & your PC using the USB cable that you use to power your Netduino / program it, it won't work.  While you can do this using an Arduino or a Particle Photon, it won't work for a Netduino.

 

Please see this forum thread from a few years ago for some background:

http://forums.netdui...d-the-netduino/

 

Nevyn asks if you're using a USB to serial cable.  He's talking about a USB to TTL cable - a special type of USB cable that contains an embedded chip which converts the voltages / signalling between what the PC speaks (i.e. differential USB signalling @ 5 V) to what the Netduino requires (i.e. TTL at 3.3 V).

 

You can find these cables many places.  Adafruit carries two such cables - one which uses a Prolific PL2303 chip and another one which uses an FTDI FT232RL chip.  I have used both with my Netduino and have been successful in transferring data between it and my PC.  SparkFun also has a cable like this.  They're about $10 US (the high end one that Adafruit sells is $17).  You'll find items like this on eBay; beware that there have been issues with counterfeit chips in the ones you can find on eBay.

 

USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi
 
FTDI Serial TTL-232 USB Cable
 
USB to TTL Serial Cable

 

With regard to your code...

 

On the Netduino:

  • On a superficial level the code you've written for your Netduino looks okay.
  • You're attempting to use the COM1 UART on the Netduino.
  • Be aware that this designates digital pins 0 & 1 (RX & TX respectively) on the Netduino.
  • It is NOT the Netduino's USB port.

On the PC:

  • The code you've posted is written to utilize the COM1 port - that's a real physical hardware RS232 port on many PCs (older ones).  Newer PCs frequently don't have such a port.
  • If you get one of the above-mentioned cables and plug it in, it will expose itself as something called a Virtual COM Port (you may need to install some drivers the first time round).
  • After this has been done, you will need to go into Device Manager on the PC to look up which COM port number has been assigned.  One way to do this is (Windows 7 assumed) to go into Control Panel, choose Device Manager, and then expand the Ports node of the device tree.  It's not unusual to see a Virtual COM port with a higher number (i.e. COM6, COM8).
  • Once you've figured out which port has been exposed - update your PC code to use that port #.

You can find a nice diagram of the Netduino here.  You can see which pins are used to implement the COM ports (4 of them).

 

http://forums.netdui...&attach_id=2376

 

Hope this is useful to you.


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi




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.