Exemplo n.º 1
0
        /// <summary>
        /// Writes raw data to PLC memory in the given <paramref name="offset"/> asynchronously.
        /// <p>
        /// When the write operation is complete <paramref name="cb"/> will be invoked (from another thread).
        /// </p>
        /// </summary>
        ///
        /// <example>
        /// <code>
        ///
        ///void WriteRawAsyncHandler(IAsyncResult ar)
        ///{
        ///    AsyncResult a = (AsyncResult)ar;
        ///    PLC p = (PLC)a.AsyncState;
        ///    p.EndWriteRaw(ar);
        ///}
        ///
        ///private void massWriteB_Click(object sender, EventArgs e)
        ///{
        ///    Object o = plcLB.SelectedItem;
        ///    if (o == null) return;
        ///    PLC plc = (PLC)o;
        ///    MemoryStream ms = new MemoryStream();
        ///    BinaryWriter bw = new BinaryWriter(ms);
        ///    for (int i = 0; i &lt; 100; i++)
        ///        bw.Write(i*2);
        ///    // Offset 0 is MI registers. So this code writes 100 integers to MI registers
        ///    // starting from offset 0. Which means MI[0] will be 0, MI[1] will be 2, MI[2] will be 4
        ///    // and so on. Same pattern can be used to write many MW, MF or MB at once.
        ///    plc.BeginWriteRaw(0, ms.GetBuffer(), new AsyncCallback(WriteRawAsyncHandler), plc);
        ///}
        /// </code>
        /// </example>
        /// <param name="offset">Offset of the memory location in the whole PLC memory in bytes.</param>
        /// <param name="data">Byte array of the data you want to write. This can contain sequential 4-byte integers, floats or 2-byte unsigned hosrts or 1-byte data.</param>
        /// <param name="cb">Callback to be called when the operation is complete.</param>
        /// <param name="state">Any state to be passed to <paramref name="cb"/> when invoked, it's a good idea to use the PLC instance as state.</param>
        /// <returns></returns>
        public IAsyncResult BeginWriteRaw(int offset, byte[] data, AsyncCallback cb, object state)
        {
            log.Debug("BeginWriteRaw");
            BeginWriteRawDelegate d = Write;

            return(d.BeginInvoke(offset, data, cb, state));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Ends a asynchronous BeginWriteRaw call.
        /// </summary>
        /// <param name="ar"></param>
        /// <seealso cref="BeginWrite"/>
        public void EndWrite(IAsyncResult ar)
        {
            log.Debug("EndWrite");
            AsyncResult           a = (AsyncResult)ar;
            BeginWriteRawDelegate d = (BeginWriteRawDelegate)a.AsyncDelegate;

            d.EndInvoke(ar);
        }