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.

Cyclone_ZA

Member Since 21 May 2012
Offline Last Active Jul 31 2015 08:13 AM
-----

Topics I've Started

Issues with Serial Communication

22 July 2015 - 05:34 PM

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;


namespace RF
{
    public class Program
    {
        public static SerialPort oCom1 = new SerialPort(SerialPorts.COM1, 11520, Parity.None ,8, StopBits.One);
        public static SerialPort oCom2 = new SerialPort(SerialPorts.COM2, 11520, Parity.None, 8, StopBits.One);

        public static Timer timer_SendSignal = new Timer(new TimerCallback(Transmit), null, 3000, 10000);

        public static void Main()
        {
            
            oCom2.Open();
            oCom2.DataReceived += oCom2_DataReceived;


            while (true)
            {
                Thread.Sleep(250);
            }
        }

        static void oCom2_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int iBytes = oCom2.BytesToRead;

                Debug.Print("Data Received : " + iBytes + " bytes");

                int iRead_count = 0;
                byte[] rx_byte = new byte[7];
                string counter_string = string.Empty;
                iRead_count = oCom2.Read(rx_byte, 0, 7);
                //Debug.Print("Read Count : " + iRead_count);
                if (iRead_count > 0)// Any Data?
                {
                    for (int i = 0; i < rx_byte.Length; i++)
                    {
                        if (rx_byte[i].ToString().Substring(0,1) != "0")
                            counter_string += Encoding.UTF8.GetChars(rx_byte).GetValue(i).ToString();
                    }

                    Debug.Print(" > " + counter_string);
                }

                oCom2.Flush();

                Thread.Sleep(250);

            }
            catch (Exception ex)
            {
                //   Debug.Print("Error : " + ex.Message);
            }

        }

        private static void Transmit(object state)
        {
            oCom1.Open();
            oCom1.Write(UTF8Encoding.UTF8.GetBytes("abcdefg"), 0, 7);
            oCom1.Close();
            oCom1.Flush();
            Debug.Print("Transmitted!");
        }

    }
}

Hi All,

 

Looking for some help here.

 

I'm having a frustrating time trying to get basic Serial communication to work.

My ultimate intention is to use a 315mhz RF Kit to send instructions between two Netduino 2 Plus'

 

I was having trouble getting that to work, so decided to just test basic serial communication to work by simply linking the first Netduino TX (Pin D1 ... Com1) to the RX on the same Netduino (Pin D2).

 

I keep getting garbage data coming through when the DataReceived event is fired - very seldom am I actually getting the data I'm sending "abcdefg".

 

It's also sending parts of the message - for example it will send some of the characters like "acdef", etc.

 

I've tried different baud rates, to no avail.

 

Above is the basic code I wrote ... Spot anything wrong?


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.