Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Arduino serialport
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:
 
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 12-Nov-11 4:56am

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
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 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 12 Nov 2011
Copyright © CodeProject, 1999-2014
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