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.

technochris1

Member Since 29 Aug 2013
Offline Last Active Apr 29 2014 05:54 AM
-----

Topics I've Started

SerialPort Application Help.

15 October 2013 - 09:21 PM

Im Porting over an application that i made for the Arduino. The application should works as follows:

 

Steps:

  • Receive Serial Data. (Always ends in rn)
  • Parse out parts of the data
  • Check files on Sdcard for match. if no match stop here. 
  • Apply the Parsed Data to the initialization of an instance of a Struct.
  • Add the Struct to an ArrayList.
  • if the Structs existed for a certain amount of time, Signal the Serial port.
  • After a long period of time, remove the structs.

 

I Made a flowchart to explain the Application:

 

Posted Image

 

I have the Application working, but im experiencing Data Loss issues somewhere along the Line.

 

Here is my Actual Request Struct:

class Request{    public int Location_index { get; set; }    public char SubLocation { get; set; }    public int Details_index { get; set; }    public long Start_time { get; set; }    public int Level { get; set; }    public Request(int locIndex, char SubLoc, int Detail)    {        Location_index = locIndex;        SubLocation = SubLoc;        Details_index = Detail;        Start_time = DateTime.Now.Ticks;        Level = 0;    }    public override bool Equals(object obj)    {        Request ncs = obj as Request;        if (ncs == null) return false;        else        {            if (this.Location_index != ncs.Location_index) return false;            if (this.SubLocation != ncs.SubLocation) return false;            if (this.Details_index != ncs.Details_index) return false;            return true;        }    }}

Serial Port Data Lost

03 October 2013 - 04:24 PM

I came from Arduino with a project that needs more power and memory. This project receives data over the Uart/Rs232 line by line (each line ends in rn), then parses the data out. I have successfully ported over most of my program by the Serial port is where i am having problems. I have looked for hours for a reliable example of serial that works alongside of my program.   Ive tried "hari" Serial Port Helper class, and get missing data on occasion. Ive tried to make my own, without success.  

public class Program    {        static SerialPort _port;        static string Message = "";        static long RxTime;        static bool DataRx;        static void Main(string[] args)        {            _port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);            _port.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);            _port.Open();            while (true)            {                while (Message.Length > 0)                {                    if ((Message.IndexOf('r') > 0) && (Message.IndexOf('n') > 0))                    {                        ParseData(Message.Substring(0, Message.IndexOf('r')));                        Message = Message.Substring(Message.IndexOf('n') + 1);                        DataRx = false;                    }                    if ((DateTime.Now.Ticks - RxTime >= (TimeSpan.TicksPerMillisecond * 500)) && DataRx)                    {                        Debug.Print("Incomplete/Currupt Data: " + Message);                        Message = "";                        DataRx = false;                    }                }                            }        }        private static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)        {            RxTime = DateTime.Now.Ticks;            DataRx = true;            //System.Threading.Thread.Sleep(10);            string msg = null;            byte[] bytes = new byte[_port.BytesToRead];            _port.Read(bytes, 0, bytes.Length);            msg = new string(System.Text.Encoding.UTF8.GetChars(bytes));            if (msg != null)            {                Message += msg;            }        }        static public void ParseData(string s)        {            Debug.Print("Parse: " + s);            byte[] msg = System.Text.Encoding.UTF8.GetBytes(s);            _port.Write(msg, 0, msg.Length);            _port.WriteByte(13);            _port.WriteByte(10);        }    }

This Code work independently without a problem. But Once i add the rest of the program i start to lose data. I'm assuming that my program is what is causing the data loss.      The rest of my program is relatively simple. Data received is parsed (Looking for a room number, text explaining command). Then sent to add an instance of a class to an ArrayList. Every object in the ArrayList is looked at and removed after a certain time.


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.