/// <summary> Wait for any response from the device. </summary>
        /// <param name="timeout"> The timeout. </param>
        /// <returns> returns the received byte[] array. </returns>
        public byte[] WaitForUnknownReply(int timeout)
        {
            // wait for the 6 byte header to be received
            byte[] header = WaitForReply(6, timeout);
            if (header == null)
            {
                return(null);
            }

            // extrat the header into a Thorlabs message header structure
            MessageStruct headerStruct = Utilities.DeserializeMsg <MessageStruct>(header);

            // if the message is complete (destination < 0x80) then return header bytes
            if ((headerStruct._destination & 0x80) == 0)
            {
                return(header);
            }

            // wait for the remainder of the message (length defined by header packet length field)
            byte[] data = WaitForReply(headerStruct._packetLength, 200);
            if (data == null)
            {
                return(null);
            }

            // return both byte arrays as a single message byte array
            byte[] complete = new byte[headerStruct._packetLength + 6];
            header.CopyTo(complete, 0);
            data.CopyTo(complete, header.Length);
            return(complete);
        }
Beispiel #2
0
        /// <summary> Wait for homing to complete. </summary>
        /// <returns> true if it succeeds, false if it fails. </returns>
        private bool WaitForHomimgComplete()
        {
            // get a Thorlabs simple message to request a status as a byte[] array
            MessageStruct statusMessage = ThorlabsDevice.SimpleMessageStruct(DeviceMessages.MGMSG_MOT_REQ_DCSTATUSUPDATE);

            byte[] byteArray = Utilities.SerializeMessage(statusMessage);

            // loop until homed complete
            bool homed = false;

            while (!homed)
            {
                // send status request command
                _device.SendCommand(byteArray);

                // get any response (maybe a status response or a homed response)
                byte[] response = _device.WaitForUnknownReply(250);
                // get response as a message structure
                object returnObject = ObjectFromData(response);
                if (returnObject != null)
                {
                    // if a motor status then check the homing bit
                    if (returnObject is MotorStatus)
                    {
                        homed = ((((MotorStatus)returnObject)._status & 0x0400) != 0);
                    }
                    // if a simple message structure then check for homed message
                    if (returnObject is MessageStruct)
                    {
                        homed = ((MessageStruct)returnObject)._messageId == DeviceMessages.MGMSG_MOT_MOVE_HOMED;
                    }
                }
            }
            return(true);
        }
        /// <summary> Wait for moving complete. </summary>
        /// <returns> true if it succeeds, false if it fails. </returns>
        private bool WaitForMovingComplete()
        {
            // get a Thorlabs simple message to request a status as a byte[] array
            MessageStruct statusMessage = ThorlabsDevice.SimpleMessageStruct(DeviceMessages.MGMSG_MOT_REQ_DCSTATUSUPDATE);             // NOTE - some devices use 0x0480 as status request

            byte[] byteArray = Utilities.SerializeMessage(statusMessage);

            // loop until move complete
            bool moved = false;

            while (!moved)
            {
                // send status request command
                _device.SendCommand(byteArray);

                // get any response (maybe a status response or a move complete response)
                byte[] response = _device.WaitForUnknownReply(250);
                // get response as a message structure
                object returnObject = ObjectFromData(response);
                if (returnObject != null)
                {
                    // if a motor status then check for moving bit
                    if (returnObject is MotorStatus)
                    {
                        moved = ((((MotorStatus)returnObject)._status & 0x00f0) == 0);
                    }
                    // if a simple message structure then check for move complete message
                    if (returnObject is MessageStruct)
                    {
                        moved = ((MessageStruct)returnObject)._messageId == DeviceMessages.MGMSG_MOT_MOVE_COMPLETE;
                    }
                }
            }
            return(true);
        }
        /// <summary> Starts a move operation. </summary>
        /// <param name="step">	    Amount to increment by. </param>
        /// <param name="complete"> [out] The complete. </param>
        /// <returns> true if it succeeds, false if it fails. </returns>
        private bool StartMoving(int step, out bool complete)
        {
            complete = false;

            // gets the MotorMoveRelative command structure as a byte[] array
            MotorMoveRelative motorMessage = new MotorMoveRelative(step);

            byte[] byteArray = Utilities.SerializeMessage(motorMessage);

            // flush any existing messages and send move message
            _device.FlushMessages();
            if (!_device.SendCommand(byteArray))
            {
                return(false);
            }

            // get a Thorlabs simple message to request a status as a byte[] array
            MessageStruct statusMessage = ThorlabsDevice.SimpleMessageStruct(DeviceMessages.MGMSG_MOT_REQ_DCSTATUSUPDATE);             // NOTE - some devices use 0x0480 as status request

            byteArray = Utilities.SerializeMessage(statusMessage);

            // loop until device is moving
            bool started = false;

            while (!started && !complete)
            {
                // request status
                _device.SendCommand(byteArray);

                // get any response (maybe a status response or a move complete response)
                byte[] response = _device.WaitForUnknownReply(250);
                // get response as a message structure
                object returnObject = ObjectFromData(response);
                if (returnObject != null)
                {
                    // if a motor status then check for moving bit
                    if (returnObject is MotorStatus)
                    {
                        started = ((((MotorStatus)returnObject)._status & 0x00f0) != 0);
                    }
                    // if a simple message structure then check for move complete message
                    if (returnObject is MessageStruct)
                    {
                        complete = ((MessageStruct)returnObject)._messageId == DeviceMessages.MGMSG_MOT_MOVE_COMPLETE;
                    }
                }
            }

            return(true);
        }
Beispiel #5
0
        /// <summary> Starts a homing operation. </summary>
        /// <param name="complete"> [out] The complete. </param>
        /// <returns> true if it succeeds, false if it fails. </returns>
        private bool StartHoming(out bool complete)
        {
            complete = false;
            // gets the MotorHome command structure as a byte[] array
            MessageStruct message = ThorlabsDevice.SimpleMessageStruct(DeviceMessages.MGMSG_MOT_MOVE_HOME);

            byte[] byteArray = Utilities.SerializeMessage(message);

            // flush any existing messages and send move message
            _device.FlushMessages();
            if (!_device.SendCommand(byteArray))
            {
                return(false);
            }

            // get a Thorlabs simple message to request a status as a byte[] array
            MessageStruct statusMessage = ThorlabsDevice.SimpleMessageStruct(DeviceMessages.MGMSG_MOT_REQ_DCSTATUSUPDATE);

            byteArray = Utilities.SerializeMessage(statusMessage);

            // loop until device is homing
            bool started = false;

            while (!started && !complete)
            {
                // request status
                _device.SendCommand(byteArray);

                // get any response (maybe a status response or a homed response)
                byte[] response = _device.WaitForUnknownReply(250);
                // get response as a message structure
                object returnObject = ObjectFromData(response);
                if (returnObject != null)
                {
                    // if a motor status then check the homing bit
                    if (returnObject is MotorStatus)
                    {
                        started = ((((MotorStatus)returnObject)._status & 0x0200) != 0);
                    }
                    // if a simple message structure then check for homed message
                    if (returnObject is MessageStruct)
                    {
                        complete = ((MessageStruct)returnObject)._messageId == DeviceMessages.MGMSG_MOT_MOVE_HOMED;
                    }
                }
            }

            return(true);
        }
        /// <summary> Gets a device information. </summary>
        /// <returns> The device information. </returns>
        public string GetDeviceInfo()
        {
            try
            {
                // check if device is connected
                if (!_device.IsConnected)
                {
                    return("Not Connected");
                }

                // get a simple Thorlabs Message Structure and convert to a byte[] array
                MessageStruct message   = SimpleMessageStruct(DeviceMessages.MGMSG_HW_REQ_INFO);
                byte[]        byteArray = Utilities.SerializeMessage(message);

                // send the message
                if (!SendCommand(byteArray))
                {
                    return("Error Writing Command");
                }

                // get expected size of response
                int structSize = Marshal.SizeOf(typeof(GetHardwareInfo));
                // wait for response from device (timeout 2 secs)
                byteArray = WaitForReply(structSize, 2000);
                if (byteArray == null)
                {
                    return("Error waiting for response");
                }

                // convert response to a GetHardwareInfo structure
                GetHardwareInfo hardwareInfo = Utilities.DeserializeMsg <GetHardwareInfo>(byteArray);
                return(hardwareInfo.ToString());
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }