Click here to Skip to main content
11,644,286 members (72,007 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Arduino serialport

I need some help with my code because i keep re-arranging it and adding stuff and using try, catch but no matter what i try i cannot get the serial to work with a servo / PWM on Netduino.

This code writes to the Netduino and the Netduino responds on serial but as soon as the while begins it freezes.

Please any help would be appreciated.




using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO;
using System.Text;
using System.IO.Ports;
namespace mrinc
    public class Program
        static SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
        static PWM servo = new PWM(Pins.GPIO_PIN_D5);
        static int[] range = new int[2];
        static byte[] buffer = new byte[32];
        public static void Main()
            s = "M.R. Inc*******";
            port.Write(Encoding.UTF8.GetBytes(s), 0, s.Length);
            while (true)
                int count = 0;
                try { count = port.Read(buffer, 0, buffer.Length); }
                catch (Exception) { }
                if (count > 0)
                    char[] c = Encoding.UTF8.GetChars(buffer);
                    if (c[0] == '1')
                    if(c[0] == '0')
            private static long map(long x, long in_min, long in_max, long out_min, long out_max)
            return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
        private static void setServo(int value)
                servo.SetPulse(20000, (uint)map((long)value, 0, 180, range[0], range[1]));
            catch (Exception)
            { }
        private static void initializeServo()
            range[0] = 1000;
            range[1] = 2000;
        private static void initializeSerial()
            port.ReadTimeout = 0;
Posted 12-Nov-11 3:56am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It freezes because you are doing a blocking Read call , having set the timeout to zero. That means that the Read will not return until Buffer.Length characters have been read - the error timeout has been disabled by the zero you wrote to it. If te Arduinoi does not supply at least Buffer.Length characters,. Read will never return.

Look at changing to an event driven read: use the SerialPort.DataReceived Event[^] to collect each character as it comes, and build up whatever message it provides in a buffer. At least that way you can look at what is being received and stand a better chance of debugging the dataflow.
mrinc at 12-Nov-11 12:18pm
Thank you!!!!!!

Code is working flawlessly :D
OriginalGriff at 13-Nov-11 3:19am
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 570
1 484
2 Afzaal Ahmad Zeeshan 413
3 DamithSL 308
4 Sergey Alexandrovich Kryukov 276
0 OriginalGriff 795
1 DamithSL 611
2 Mika Wendelius 494
3 484
4 Afzaal Ahmad Zeeshan 404

Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 12 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100