Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

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.

Thanks

Regards
Mitchell

CODE:

C#
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()
        {
            initializeSerial();
            initializeServo();
            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')
                    {
                        setServo(0);
                    }
                    if(c[0] == '0')
                    {
                        setServo(90);
                    }
                }
            }
            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)
        {
            try
            {
                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;
            servo.SetDutyCycle(0);
            setServo(90);
        }
        private static void initializeSerial()
        {
            port.ReadTimeout = 0;
            port.Open();
        }
Posted

1 solution

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.
 
Share this answer
 
Comments
M.R. Inc 12-Nov-11 12:18pm    
Thank you!!!!!!

Code is working flawlessly :D
OriginalGriff 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900