none
Wait until Serial Data was send RRS feed

  • Question

  • Hello community,

    i write a serial data to an open port, now i must wait until the my outputbuffer is empty and then continue my program.

    Now there are several questions:

    Does the program run while writing serial data in the background or does the program automatically stops till data was send?

    Is there a function or an event that I can catch when the Serial outputbuffer is empty and data was send.

    If there is no function i must stop the thread until I am sure that know I know the data was send.

    There is no ACK that i can wait for.

     




    Thursday, January 19, 2017 4:58 PM

All replies

  • Hi Der Bastler,

    >>Does the program run while writing serial data in the background or does the program automatically stops till data was send?

    Yes, I think you mean the DataReceived event , Am I right? If yes, it is fired on a background thread.

    >>Is there a function or an event that I can catch when the Serial outputbuffer is empty and data was send.

    You can check SerialPort.ReadExisting Method (), it reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object. If your outputbuffer is empty. This method will return empty.  If the data send successfully, you will get these data as it shows in string "indata" in the following demo.

    using System;
    using System.IO.Ports;
    
    class PortDataReceived
    {
        public static void Main()
        {
            SerialPort mySerialPort = new SerialPort("COM1");
    
            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;
    
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
    
            mySerialPort.Open();
    
            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            Console.ReadKey();
            mySerialPort.Close();
        }
    
        private static void DataReceivedHandler(
                            object sender,
                            SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);
        }
    }
    

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 20, 2017 9:00 AM
  • A am not receiving data, i am sending data. Every 10ms i send a burst of 30bytes and i want to know when the last byte was send.

    I can calculate this with the baudrate like.

    SendTime = 1/BAUD * BITS per frame * BufferSize

    Now SendTime is the minimum time in seconds i need so send the package.  

    Friday, January 20, 2017 3:44 PM
  • Hi Der,

    >>A am not receiving data, i am sending data. Every 10ms i send a burst of 30bytes and i want to know when the last byte was send. I can calculate this with the baudrate like

    Steps you need to follow to work with & send data via Serial Port:
    1. First you need to initializing the SerialPort instance with proper argument values
    2. Call Open() method on it to start interacting with the port
    3. Then you may call any of the overloaded methods named Write(), or WriteLine() to send data via the port.
    4. After finishing your job call the Close() method on it.

    After call Open() method, Once connected, At this point the PC will try to open the default serial port which is default is 9600 Baud, No Parity, One Stop bit and No flow control.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 23, 2017 8:10 AM
  • Hello,

    maybe you can tell us, why you must wait for execution of send. What device you're expect on other side of line? In most cases communication is 2-way, so I expect that if you're Master, Slave device can resaponse with some message to you after your send, or if your send message isn't with proper consistency device will not response to some defined timeout. With this system it should work in many cases. Or is your communication 1-way? In this case very hard to check that, maybe if you know that time of send complete message, define some WriteTimeout little bit higher as your expected and if some problem occur, event will rise. Then you will know that there's some problem.

    Regards...

    Duris_I

    Monday, January 23, 2017 11:23 AM