Beispiel #1
0
 private PortEvent SendPortRequest(PortRequest request)
 {
     request.RequestId = _ports.NextRequestId();
     _pendingRequests.AnnounceRequest(request.RequestId);
     WriteMessage(request);
     return(_pendingRequests.WaitForResponse(request.RequestId) as PortEvent);
 }
Beispiel #2
0
        /// <summary>
        /// Reads the value of the digital input.
        /// </summary>
        /// <param name="port">the digital input's port ID</param>
        /// <returns>the input value</returns>
        public bool ReadDigitalPin(int port)
        {
            Port p = _ports.GetPort(port);

            if (p == null)
            {
                throw new WirekiteException(String.Format("Invalid port ID {0}", port));
            }

            PortType type = p.Type;

            if (type == PortType.DigitalInputPrecached || type == PortType.DigitalInputTriggering)
            {
                return(p.LastSample != 0);
            }

            PortRequest request = new PortRequest {
                PortId = (UInt16)port,
                Action = Message.PortActionGetValue
            };

            SubmitPortRequest(request);

            PortEvent evt = p.WaitForEvent();

            return(evt.Value1 != 0);
        }
Beispiel #3
0
        /// <summary>
        /// Send data to and request data from an I2C slave in a single operation
        /// </summary>
        /// <remarks>
        /// <para>
        /// The operation performs a complete I2C transaction, starting with a START condition,
        /// a RESTART condition when switching from transmission to receipt, and ending with
        /// a STOP condition.
        /// </para>
        /// <para>
        /// The request is executed sychnronously, i.e. the call blocks until the data
        /// has been transmitted and received, or the transmission has failed.
        /// </para>
        /// <para>
        /// If less than the specified number of bytes are transmitted, `nil` is returned and
        /// <see cref="GetLastI2CResult(int)"/> returns the associated reason.
        /// </para>
        /// </remarks>
        /// <param name="port">the I2C port ID</param>
        /// <param name="data">the data to transmit</param>
        /// <param name="slave">the slave address</param>
        /// <param name="receiveLength">the number of bytes of data request from the slave</param>
        /// <returns>the received data or <c>null</c> if the transaction fails</returns>
        public byte[] SendAndRequestOnI2CPort(int port, byte[] data, int slave, int receiveLength)
        {
            Port p = _ports.GetPort(port);

            if (p == null)
            {
                throw new WirekiteException(String.Format("Invalid port ID {0}", port));
            }

            PortRequest request = new PortRequest
            {
                PortId           = (UInt16)port,
                Action           = Message.PortActionTxNRxData,
                Data             = data,
                ActionAttribute2 = (UInt16)slave,
                Value1           = (UInt16)receiveLength
            };

            WaitUntilAvailable(request);
            WriteMessage(request);

            PortEvent response = _pendingRequests.WaitForResponse(request.RequestId) as PortEvent;

            p.LastSample = response.EventAttribute1; // status code
            return(response.Data);
        }
Beispiel #4
0
        /// <summary>
        /// Reset the I2C bus
        /// </summary>
        /// <remarks>
        /// If an I2C transaction is interrupted, a slave can still hold on to SDA
        /// because it believes to be in the middle of a byte.
        /// By toggling SCL up to 9 times, most slaves let go of SDA.
        /// This method can be used if an I2C operation returns a "bus busy" error.
        /// </remarks>
        /// <param name="port">hte I2C port ID</param>
        public void ResetBusOnI2CPort(int port)
        {
            if (_deviceState == DeviceState.Closed)
            {
                return; // silently ignore
            }
            Port p = _ports.GetPort(port);

            if (p == null)
            {
                return;
            }

            PortRequest request = new PortRequest
            {
                Action = Message.PortActionReset,
                PortId = (UInt16)port
            };

            WaitUntilAvailable(request);

            PortEvent response = _pendingRequests.WaitForResponse(request.RequestId) as PortEvent;

            p.LastSample = response.EventAttribute1; // status code
        }
        /// <summary>
        /// Request data from an SPI slave
        /// </summary>
        /// <para>
        /// The operation performs a complete SPI transaction, i.e. enables the clock for the duration of
        /// transation and receives the data.
        /// </para>
        /// <para>
        /// The operation is executed sychnronously, i.e.the call blocks until the
        /// transaction has been completed or has failed. If the transaction fails,
        /// use <see cref="GetLastSPIResult(int)"/> to retrieve the reason.
        /// </para>
        /// <para>
        /// SPI is a full-duplex protocol at all times. Unless they use additional connections, slaves
        /// cannot distinguish between read and write transactions.
        /// This member functions send a configurable value on the MOSI line during the read.
        /// Default value is 0xff.
        /// </para>
        /// <param name="port">the SPI port ID</param>
        /// <param name="length">the number of bytes of data requested from the slave</param>
        /// <param name="chipSelect">the digital output port ID to use as chip select (or <see cref="InvalidPortId"/> if not used)</param>
        /// <param name="mosiValue">byte value sent on MOSI signal during reading</param>
        /// <returns>the received data or <c>null</c> if it fails</returns>
        public byte[] RequestOnSPIPort(int port, int length, int chipSelect, int mosiValue = 0xff)
        {
            Port p = _ports.GetPort(port);

            if (p == null)
            {
                throw new WirekiteException(String.Format("Invalid port ID {0}", port));
            }

            PortRequest request = new PortRequest
            {
                PortId           = (UInt16)port,
                Action           = Message.PortActionRxData,
                ActionAttribute1 = (byte)mosiValue,
                ActionAttribute2 = (UInt16)chipSelect,
                Value1           = (uint)length,
                RequestId        = _ports.NextRequestId()
            };

            WaitUntilAvailable(request);
            WriteMessage(request);

            PortEvent response = _pendingRequests.WaitForResponse(request.RequestId) as PortEvent;

            p.LastSample = response.EventAttribute1; // status code
            return(response.Data);
        }
Beispiel #6
0
 private void SubmitPortRequest(PortRequest request, bool throttle = false)
 {
     request.RequestId = _ports.NextRequestId();
     if (throttle)
     {
         WaitUntilAvailable(request);
     }
     WriteMessage(request);
 }
Beispiel #7
0
        /// <summary> Adds pObjRecord. </summary>
        /// <remarks> Ranaya, 08/05/2017. </remarks>
        /// <param name="pObjRecord"> The Object record to add. </param>
        /// <returns> An int. </returns>

        public int Add(PortRequest pObjRecord)
        {
            int lIntResult = 0;

            //CREAR

            if (!mObjPermissionsDAO.ExistRequest(pObjRecord.RequestId))
            {
                lIntResult = mObjPortRequestDAO.Add(pObjRecord);
                if (lIntResult == 0)
                {
                    LogService.WriteSuccess("[PortRequest CREATED]");


                    if (mObjPermissionsDAO.IsRequestPreparedToCreateSaleOrder(pObjRecord.RequestId))
                    {
                        lIntResult = mObjPermissionsDAO.CreateSaleOrder(pObjRecord.RequestId);
                    }
                }
                else
                {
                    LogService.WriteError("[PortRequest NO CREATED]");
                }
            }
            //EDITAR
            else
            {
                // New portRequest line
                if (mObjPermissionsDAO.IsPortExist(pObjRecord.RequestId, pObjRecord.PortId, pObjRecord.PortType) == "0")
                {
                    lIntResult = mObjPortRequestDAO.Add(pObjRecord);
                    if (lIntResult == 0)
                    {
                        LogService.WriteSuccess("[PortRequest UPDATE ADDPORT]");
                    }
                }
                // Update portRequest line
                else
                {
                    pObjRecord.RowCode = mObjPermissionsDAO.GetRowCodeByPort(pObjRecord.RequestId, pObjRecord.PortId, pObjRecord.PortType);
                    lIntResult         = mObjPortRequestDAO.Update(pObjRecord);

                    if (lIntResult == 0)
                    {
                        LogService.WriteSuccess("[PortRequest UPDATE]");
                        lIntResult = mObjPermissionsDAO.UpdateSaleOrder(pObjRecord.RequestId);
                        if (lIntResult == 0)
                        {
                            LogService.WriteSuccess("[PortRequest SaleOrder UPDATE]");
                        }
                    }
                }
            }

            return(lIntResult);
        }
Beispiel #8
0
        /// <summary>
        /// Sets the duty cycle of a PWM output
        /// </summary>
        /// <param name="port">the PWM output's port ID</param>
        /// <param name="dutyCycle">the duty cycle between 0.0 (for 0%) and 1.0 (for 100%)</param>
        public void WritePWMPin(int port, double dutyCycle)
        {
            PortRequest request = new PortRequest
            {
                PortId = (UInt16)port,
                Action = Message.PortActionSetValue,
                Value1 = (UInt32)(dutyCycle * 2147483647 + 0.5)
            };

            SubmitPortRequest(request);
        }
Beispiel #9
0
        /// <summary>
        /// Writes a value to the digial output pins and synchronizes it with an SPI port
        /// </summary>
        /// <remarks>
        /// Writing a value is an asynchronous operation. The function returns immediately without awaiting
        /// a confirmation that it has succeeded. However, the action is not executed until all already
        /// all SPI actions submitted to the same SPI port have been executed and before any later submitted action.
        /// This is useful to change a signal relevant for the SPI communication such as a data/command signal.
        /// </remarks>
        /// <param name="port">digital output port ID</param>
        /// <param name="value">the output value (true for high, false for low)</param>
        /// <param name="spiPort">SPI port ID</param>
        public void WriteDigitalPinSynchronizedWithSPI(int port, bool value, int spiPort)
        {
            PortRequest request = new PortRequest
            {
                PortId           = (UInt16)port,
                Action           = Message.PortActionSetValue,
                Value1           = value ? (uint)1 : 0,
                ActionAttribute2 = (UInt16)spiPort
            };

            SubmitPortRequest(request, spiPort != 0);
        }
Beispiel #10
0
        /// <summary> Updates the given pObjRecord. </summary>
        /// <remarks> Ranaya, 08/05/2017. </remarks>
        /// <param name="pObjRecord"> The Object record to add. </param>
        /// <returns> An int. </returns>

        public int Update(PortRequest pObjRecord)
        {
            int lIntResult = 0;

            lIntResult = mObjPortRequestDAO.Update(pObjRecord);

            if (lIntResult == 0 &&
                mObjPermissionsDAO.ExistsSaleOrder(pObjRecord.RequestId))
            {
                lIntResult = mObjPermissionsDAO.UpdateSaleOrder(pObjRecord.RequestId);
            }

            return(lIntResult);
        }
Beispiel #11
0
        private UInt16 SubmitI2CTx(int port, byte[] data, int slave)
        {
            PortRequest request = new PortRequest
            {
                PortId           = (UInt16)port,
                Action           = Message.PortActionTxData,
                Data             = data,
                ActionAttribute2 = (UInt16)slave
            };

            WaitUntilAvailable(request);
            WriteMessage(request);

            return(request.RequestId);
        }
        private UInt16 SubmitSPITx(int port, byte[] data, int chipSelect, byte action)
        {
            PortRequest request = new PortRequest
            {
                PortId           = (UInt16)port,
                Action           = action,
                Data             = data,
                ActionAttribute2 = (UInt16)chipSelect,
                RequestId        = _ports.NextRequestId()
            };

            WaitUntilAvailable(request);
            WriteMessage(request);
            return(request.RequestId);
        }
Beispiel #13
0
        /// <summary>
        /// Reads the value of the analog input.
        /// </summary>
        /// <param name="port">the analog input's port ID</param>
        /// <returns>the input value in the range between -1.0 and 1.0</returns>
        public double ReadAnalogPin(int port)
        {
            Port p = _ports.GetPort(port);

            if (p == null)
            {
                throw new WirekiteException(String.Format("Invalid port ID {0}", port));
            }

            PortRequest request = new PortRequest
            {
                PortId = (UInt16)port,
                Action = Message.PortActionGetValue
            };

            SubmitPortRequest(request);

            PortEvent evt = p.WaitForEvent();
            Int32     v   = (Int32)evt.Value1;

            return(v < 0 ? v / 2147483648.0 : v / 2147483647.0);
        }