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.