Haris

Haris

  • NA
  • 13
  • 5.9k

Help: Receiving a frame throug SerialPort

Nov 22 2011 7:14 PM
I have a problem receiving a complet through the serial port. I have a device (Xbee) that is sending a frame which consists of 22 bytes. This frame is sent to the PC every 100ms at a baudrate of 4800bps.

I have made a class that uses asynchrone IO. When i use the class i create a instance and when i want to read the
newest value (frame) received i use "object.Temperatur" to get the string where the frame is stored. Temperatur is a string.

The big problem is to get a complete frame of 22 bytes stored in Temperatur (converted to HEX --> Temperatur = ConvertToHex(Encoding.ASCII.GetString(buffer, 0, bytesRead)); (ConvertToHex is a metod which i have defined in the class)). When i use this class i get only pieces and the pieces start from different bytes of the frame. Does anyone know what i could do to get a complete frame?

I have also shared my program. Feel free to take it if you can use something form it :)

I am sorry for my bad english.


////////////////////////////////////////////// MY CLASS //////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;


namespace ThermoCouple
{
    class AsynchIOThermo
    {
        SerialPort COMport = new SerialPort();
       
        private Stream inputStream;
        private AsyncCallback myCallBack;
        private byte[] buffer;
        private int BufferSize;
       
        public string Temperatur { set; get; }
         
        public AsynchIOThermo(string COMportName, int BaudRate, int DataBits, string Parity, string StopBits, int StreamBufferSize)
        {
            BufferSize = StreamBufferSize;

          
            COMport.ReadBufferSize = 2200;

            // Initial COMport
            COMport.PortName = COMportName;
            COMport.BaudRate = BaudRate;
            COMport.DataBits = DataBits;

            switch (Parity)
            {
                case "None":
                    COMport.Parity = System.IO.Ports.Parity.None;
                    break;
                case "Mark":
                    COMport.Parity = System.IO.Ports.Parity.Mark;
                    break;
                case "Even":
                    COMport.Parity = System.IO.Ports.Parity.Even;
                    break;
                case "Odd":
                    COMport.Parity = System.IO.Ports.Parity.Odd;
                    break;
                case "Space":
                    COMport.Parity = System.IO.Ports.Parity.Space;
                    break;
            }

            //StopBits Termo
            switch (StopBits)
            {
                case "1":
                    COMport.StopBits = System.IO.Ports.StopBits.One;
                    break;
                case "1.5":
                    COMport.StopBits = System.IO.Ports.StopBits.OnePointFive;
                    break;
                case "2":
                    COMport.StopBits = System.IO.Ports.StopBits.Two;
                    break;
            }

            COMport.Open();
            inputStream = COMport.BaseStream;
            buffer = new byte[StreamBufferSize];
           
            myCallBack = new AsyncCallback(OnCompletedRead);  
        }

        /*abstract*/
        public void Run()
        {
            inputStream.BeginRead(
                buffer,
                0,
                buffer.Length,
                myCallBack,
                null);
        }
       
       
        public void OnCompletedRead(IAsyncResult asyncResult)
        {
            int bytesRead = inputStream.EndRead(asyncResult);

            if (bytesRead > 0)
            {
                Temperatur = ConvertToHex(Encoding.ASCII.GetString(buffer, 0, bytesRead));
                inputStream.BeginRead(buffer, 0, buffer.Length, myCallBack, null);
            }
        }

        private string ConvertToHex(string asciiString)
        {
            string hex = "";
            foreach (char c in asciiString)
            {
                int tmp = c;
                hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
            }
            return hex;
        }

        public void Stop()
        {
            try
            {
                COMport.Close();
            }
            catch
            {
                MessageBox.Show("Unable to close the connection!");
            }
        }
    }
}

////////////////////////////////////////////// END OF CLASS /////////////////////////////////////////////////

Attachment: theprogram.zip

Answers (15)