예제 #1
0
        ///////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Polls the Cubloc inputs
        /// </summary>
        ///////////////////////////////////////////////////////////////////////////////////////////
        private void UpdateThread()
        {
            Message result = null;
            Message inputRead = new Message(MessageType.InputBufferRead);
            inputRead.Data = "000";  //Channel 0 is the digital input buffer
            while (mRunUpdateThread) {
                if (Connected) {

                    result = Send(inputRead, 500);

                    if (result != null) {
                        if (result.Command == MessageType.InputBufferUpload) {
                            if (result.Data.Length == 6) {
                                for (int i = 0; i < mInputBuffer.Length; i++) {
                                    mInputBuffer[i] = Convert.ToByte(result.Data.Substring(i * 3, 3));
                                }
                                for (int i = 0; i < 11; i++) {
                                    mInputTags[i].Value = Bitset.GetBit(mInputBuffer, i);
                                }
                            } else {
                                //Parse Error...
                                System.Diagnostics.Debug.Write("Update Thread. Parse Error.");
                            }
                        } else {
                            //Command Error....
                            System.Diagnostics.Debug.Write("Update Thread. Command Error.");
                        }
                    } else {
                        //No response....
                        System.Diagnostics.Debug.Write("Update Thread. No Response.");

                    }

                    WriteOutputBuffer();
                    Thread.Sleep(50);
                } else {
                    Thread.Sleep(1000);
                    Connect();
                }

            }
        }
예제 #2
0
        ///////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Syncronous Send.  Sends a Esys protocol message over the serial port and waits for
        /// the response.  Returns the reponse if successful, null otherwise.
        /// </summary>
        ///////////////////////////////////////////////////////////////////////////////////////////
        private Message Send(Message message, int timeout)
        {
            if (Connected) {
                lock (mSyncLock) {
                    Esys.Protocol.Message result = null;
                    int count = timeout / 20;
                    mResult = null;
                    mMsgNumber = (mMsgNumber >= 99999) ? 1 : mMsgNumber + 1;
                    message.Number = mMsgNumber;
                    mPort.Write(message.ToString());

                    for (int i = 0; i < count; i++) {
                        result = mResult;
                        if (result != null && result.Number == message.Number && result is Message) {
                            break;
                        } else {
                            result = null;
                            Thread.Sleep(20);
                        }
                    }

                    return (Message)result;
                }
            } else {
                return null;
            }
        }
예제 #3
0
 ///////////////////////////////////////////////////////////////////////////////////////////
 /// <summary>
 /// Asyncronous Send.  Sends a Esys protocol message over the serial port and immediately
 /// returns.
 /// </summary>
 ///////////////////////////////////////////////////////////////////////////////////////////
 private void Send(Message message)
 {
     if (Connected) {
         lock (mSyncLock) {
             mMsgNumber = (mMsgNumber >= 99999) ? 1 : mMsgNumber + 1;
             message.Number = mMsgNumber;
             mPort.Write(message.ToString());
         }
     }
 }
예제 #4
0
        void OnInputTagRead(object sender, EventArgs e)
        {
            Tag tag = sender as Tag;
            if (!mInitialized || !Connected || tag == null) return;
            int index = GetTagIndex(tag);

            Message result = null;
            Message inputRead = new Message(MessageType.InputBufferRead);
            inputRead.Data = (index + 1).ToString("000");  //Channel 1 - 9 are analog inputs

            for (int i = 0; i < 3; i++) {
                result = Send(inputRead, 200);
                if (result != null) break;
            }
            if (result == null) {
                Disconnect();
            } else {
                try {
                    tag.Value = (Int16)(Convert.ToByte(result.Data.Substring(0,3)) + (Convert.ToByte(result.Data.Substring(3,3)) * 256));
                } catch{
                    tag.Value = 0;
                }
            }
        }
예제 #5
0
 ///////////////////////////////////////////////////////////////////////////////////////////
 /// <summary>
 /// Event handler called when data is received on the serial port (mPort)
 /// </summary>
 ///////////////////////////////////////////////////////////////////////////////////////////
 private void mPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     Message result = new Message();
     string endchar = ASCIIEncoding.ASCII.GetString(new byte[] { 0x03 });
     try {
         string data = mPort.ReadTo(endchar);
         data = data.Remove(0, 1); //Remove the start char
         if (result.TryParse(data)) {
             mResult = result;
         }
     } catch {
         //ArgumentException             -The length of the value parameter is 0.
         //ArgumentNullException         -The value parameter is nullNothingnullptra null reference (Nothing in Visual Basic).
         //InvalidOperationException     -The specified port is not open.
         //TimeoutException              -The operation did not complete before the time-out period ended.
     }
 }
예제 #6
0
        protected void WriteOutputBuffer()
        {
            Message result = null;
            Message writeOutput = new Message(MessageType.OutputBufferWrite);
            writeOutput.Data = mOutputBuffer[0].ToString("000");
            writeOutput.Data += mOutputBuffer[1].ToString("000");

            for (int i = 0; i < 3; i++) {
                result = Send(writeOutput, 400);
                if (result != null) break;
            }
            if (result == null) Disconnect();
        }