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

Issues with Serial Communication

Serial

  • Please log in to reply
3 replies to this topic

#1 Cyclone_ZA

Cyclone_ZA

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationJohannesburg, South Africa

Posted 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?


Edited by Cyclone_ZA, 23 July 2015 - 06:29 AM.


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 23 July 2015 - 06:22 AM

Hi Cyclone,

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 D3).
 
Above is the basic code I wrote ... Spot anything wrong?

First things first... Pin D1 and pin D3 are both TX pins. You'll want to connect pin D1 (COM1 TX) to D2 (COM2 RX).

Does that get you started down the right path?

Chris

#3 Cyclone_ZA

Cyclone_ZA

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationJohannesburg, South Africa

Posted 23 July 2015 - 06:29 AM

Hi Chris,

 

Apologies - typo - I meant to type D1 and D2.

 

I've also tried using COM3 (D7) - and get the same results.

 

(I've correct my original post)



#4 NameOfTheDragon

NameOfTheDragon

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationCanterbury, Kent, UK

Posted 31 July 2015 - 05:10 AM

This probably has no bearing, but you might want to flush before you close.

 

Also, if you close the port immediately after sending data, then you may be shutting it down before it has transmitted all the characters in the Tx buffer. I think SerialPort.Write() is non-blocking, I'm not sure how it is implemented in Netduino.

 

--Tim Long







Also tagged with one or more of these keywords: Serial

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.