V1/Cet.Develop.NETMF.IO/Ports/SerialPortClient.cs
/// <summary>
/// Entry-point for submitting a query to the remote device
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
Entry-point for submitting a query to the remote device
Entry - point for submitting a query to the remote device
public CommResponse Query(ClientCommData data)
        {
            lock (this.Port)
            {
                byte[] outgoing = data
                    .OutgoingData
                    .ToArray();
                var incoming = new ByteArrayWriter();
                const int tempSize = 64;
                var temp = new byte[tempSize];
                for (int attempt = 0, retries = data.Retries; attempt < retries; attempt++)
                {
                    this.Port
                        .DiscardInBuffer();
                    this.Port
                        .DiscardOutBuffer();
                    this.Port
                        .Write(outgoing, 0, outgoing.Length);
                    incoming.Reset();
                    bool timeoutExpired;
                    int totalTimeout = this.Latency + data.Timeout;
                    using (Timer timer = new Timer(
                        _ => timeoutExpired = true,
                        state: null,
                        dueTime: totalTimeout,
                        period: Timeout.Infinite))
                    {
                        timeoutExpired = false;
                        while (timeoutExpired == false)
                        {
                            int length = this.Port.BytesToRead;
                            if (length > 0)
                            {
                                if (length > tempSize)
                                    length = tempSize;
                                this.Port
                                    .Read(temp, 0, length);
                                incoming.WriteBytes(
                                    temp,
                                    0,
                                    length);
                                data.IncomingData = incoming.ToReader();
                                CommResponse result = data
                                    .OwnerProtocol
                                    .Codec
                                    .ClientDecode(data);
                                if (result.Status == CommResponse.Ack)
                                {
                                    return result;
                                }
                                else if (result.Status == CommResponse.Critical)
                                {
                                    return result;
                                }
                                else if (result.Status != CommResponse.Unknown)
                                {
                                    break;
                                }
                            }
                            Thread.Sleep(0);
                        }
                    }   
                }       
                return new CommResponse(
                    data,
                    CommResponse.Critical);
            }   
        }
public CommResponse Query ( ClientCommData data ) { lock ( this . Port ) { byte [ ] outgoing = data . OutgoingData . ToArray ( ) ; var incoming = new ByteArrayWriter ( ) ; const int tempSize = 64 ; var temp = new byte [ tempSize ] ; for ( int attempt = 0 , retries = data . Retries ; attempt < retries ; attempt ++ ) { this . Port . DiscardInBuffer ( ) ; this . Port . DiscardOutBuffer ( ) ; this . Port . Write ( outgoing , 0 , outgoing . Length ) ; incoming . Reset ( ) ; bool timeoutExpired ; int totalTimeout = this . Latency + data . Timeout ; using ( Timer timer = new Timer ( _ => timeoutExpired = true , state : null , dueTime : totalTimeout , period : Timeout . Infinite ) ) { timeoutExpired = false ; while ( timeoutExpired == false ) { int length = this . Port . BytesToRead ; if ( length > 0 ) { if ( length > tempSize ) length = tempSize ; this . Port . Read ( temp , 0 , length ) ; incoming . WriteBytes ( temp , 0 , length ) ; data . IncomingData = incoming . ToReader ( ) ; CommResponse result = data . OwnerProtocol . Codec . ClientDecode ( data ) ; if ( result . Status == CommResponse . Ack ) { return result ; } else if ( result . Status == CommResponse . Critical ) { return result ; } else if ( result . Status != CommResponse . Unknown ) { break ; } } Thread . Sleep ( 0 ) ; } } } return new CommResponse ( data , CommResponse . Critical ) ; } }
Entry-point for submitting a query to the remote device
Entry - point for submitting a query to the remote device
//set the proper parameters to the port //this.Port.BaudRate = this.Setting.BaudRate; //this.Port.Parity = this.Setting.Parity; //this.Port.DataBits = this.Setting.DataBits; //this.Port.StopBits = this.Setting.StopBits; //convert the request data as an ordinary byte array //create a writer for accumulate the incoming data //retries loop //flush any residual content //phyiscal writing //start the local timer //reception loop, until a valid response or timeout //read the incoming data from the physical port //append data to the writer //try to decode the stream //exit whether any concrete result: either good or bad //stop immediately if the host asked to abort //TODO //using (timer) //for //no attempt was successful //lock
None
data
ClientCommData
None