Main Site Documentation

Serial Port flow control


#1

Hi!
I am using FEZ Panda II.
I am working at project which uses serial port to transmitting data. Because of out of memory I must to proccess data in time of transmition. Therefore it is necessery to use flow control. I have few question to .Net Micro Framework Community:

  1. Can somebody to give me a simple example of hardware and software handshaking?
  2. I have application which writes data to FEZ. What happens in PC App when FEZ reports full buffer ? Does PC application stops in line of writing data and will be waiting for empty buffer?

#2

below are my attempts,
receiver:


 private static SerialPort RS;
        private static System.Collections.ArrayList List;
        public static void Main()
        {
            List = new System.Collections.ArrayList();
            RS = new SerialPort("COM1", 9600);
            RS.Handshake = Handshake.XOnXOff;
            RS.Open();
            
            RS.Write(new byte[] { (byte)HandshakeCommands.Xon}, 0, 1);
            while (true)
            {
                if (RS.BytesToRead > 0)
                {
                    byte[] buffer = new byte[RS.BytesToRead];
                    RS.Read(buffer, 0, buffer.Length);
                    List.Add(buffer);
                    RS.Write(new byte[] { (byte)HandshakeCommands.Xoff }, 0, 1);
                    Thread.Sleep(1000); // simulates data processing 
                    RS.Write(new byte[] { (byte)HandshakeCommands.Xon }, 0, 1);
                }
            }
        }

transmiter:


private static SerialPort RS;       
        static void Main(string[] args)
        {
            RS = new SerialPort("COM3", 9600);
            RS.Handshake = Handshake.XOnXOff;
            RS.WriteTimeout = Timeout.Infinite;

            byte[] bytes = new byte[1024];

            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)i;
            }
            RS.Open();
            RS.Write(bytes, 0, bytes.Length);  // In this point, I am starting receiver program
            while (RS.BytesToWrite != 0) ;       //hear I want to wait for all data send but it dosnt work
            Thread.Sleep(Timeout.Infinite);       // in this point I havent all in receiver side
        }


#3

I am also curious if anyone had actually tried the software handshaking on NETMF.

You should use hardware handshaking if that if that is an option. You will need 2 more wires, RTS and CTS. Search the web for further details please.


#4

I still have problem with flow control in serialPort. I am using FTDI cabel and I have connected it in this way : Tx<->Rx, CTS <-> RTS.I set handshake as request to send. In uC side I dont recive data because of I want to full up buffer. On PC side I send a lot of data in loop. I expected some error or stop of program on Write method, but PC all the time can to send data though full receiver buffor.
uC:


        public static SerialPort RS;
        public static void Main()
        {
            RS = new SerialPort("COM2", 9600);
            RS.Handshake = Handshake.RequestToSend;
            //RS.DataReceived += new SerialDataReceivedEventHandler(RS_DataReceived);
            RS.Open();
            Thread.Sleep(Timeout.Infinite);
        }

        static void RS_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] inpuBuffer = new byte[RS.BytesToRead];
            RS.Read(inpuBuffer, 0, inpuBuffer.Length);
        }

PC:


    private static SerialPort RS;       
        static void Main(string[] args)
        {
            RS = new SerialPort("COM3", 9600);
            RS.Handshake = Handshake.RequestToSend;
            RS.WriteTimeout = Timeout.Infinite;
            
            RS.DataReceived += new SerialDataReceivedEventHandler(RS_DataReceived);
            RS.ErrorReceived += new SerialErrorReceivedEventHandler(RS_ErrorReceived);
            RS.DtrEnable = true;
            RS.RtsEnable = true;

            byte[] bytes = new byte[1024];

            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)i;
            }
            RS.Open();

            while (true)
            {
                RS.Write(bytes, 0, bytes.Length);
            }
        }

        static void RS_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            throw new NotImplementedException();
        }

        static void RS_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            throw new NotImplementedException();
        }


#5

The pc has a lot of ram. It will always take your data but will only send it when the device accepts it


#6

But I want to know when data are accepted and when aren’t accepted. If data are not accepted I want to wait…


#7

I used somthing like this :


            while (true)
            {
                while (!RS.CtsHolding) Thread.Sleep(0);
                RS.Write(bytes, 0, bytes.Length);
            }

and it works,but I thought this will be released automatic by SerialPort class. Thank you for your interesting.


#8

They are handeled automatically!