Example #1
0
        private byte[] ConstructPacket(int deviceAddress, RWMode readOrWrite, CommandCategory category, string command, params byte[] data)
        {
            List <byte> packet = new List <byte>(14 + data.Length);

            // 0
            packet.Add((byte)Terminator.Start);
            // 1-4
            packet.AddRange(Address(deviceAddress));
            // 5
            packet.Add((byte)readOrWrite);
            // 6
            packet.Add((byte)category);
            // 7-9
            packet.AddRange(ToAsciiBytes(command, 3, 0x00));
            // 10
            packet.Add((byte)Terminator.Separator);

            if (data.Length > 0)
            {
                packet.AddRange(data);
            }

            packet.Add((byte)Terminator.Separator);

            packet.Add(CheckSum(packet));

            packet.Add((byte)Terminator.End);

            return(packet.ToArray());
        }
Example #2
0
        private void ExceptionHandler(VisaException ve, RWMode rw)
        {
            if (rw == RWMode.Read)
            {
                Console.Write("LOST READ ");
            }
            else
            {
                Console.Write("LOST WRITE ");
            }

            Console.WriteLine("Last command: " + lastCmd);
            Console.WriteLine("Visa Exception type " + ve.ErrorCode.ToString());

            if ((ve.ErrorCode == VisaStatus.ErrorSystemError ||
                 ve.ErrorCode == VisaStatus.ErrorTimeout ||
                 ve.ErrorCode == VisaStatus.ErrorConnectionLost ||
                 ve.ErrorCode == VisaStatus.ErrorIO ||
                 ve.ErrorCode == VisaStatus.ErrorNotControllerInChange) && rw == RWMode.Write)
            {
                // something major has happened to cause a Write to timeout

                // If writes are timing out, we don't want to waste time trying to close the connection down cleanly,
                // because that will probably timeout too.  Instead, just set the instrument object to null.
                // Close ( );

                if (visaHandle != 0)
                {
                    visaHandle     = 0;
                    liveConnection = false;
                    // since we've lost the connection to the instrument, rethrow the exception with an explanation message
                    // and tell them we've lost the connection.
                    VisaException writeTimeoutException = new VisaException(VisaStatus.ErrorConnectionLost, "The connection to " + this.Address + " has been lost.", ve);
                    throw writeTimeoutException;
                }
            }
            else if (ve.ErrorCode == VisaStatus.ErrorConnectionLost || ve.ErrorCode == VisaStatus.ErrorIO || ve.ErrorCode == VisaStatus.ErrorSystemError)
            {
                if (visaHandle != 0)
                {
                    // this should prevent every thread from getting hung with Visa Timeouts
                    Close();
                    visaHandle     = 0;
                    liveConnection = false;
                    throw ve;
                }
            }

            // else if ( ve.ErrorCode = VisaStatus.ErrorTimeout && rw == RWMode.Read )
            // timed out on a read - may be bacause the query data wasn't available
            // or that something is up with the visa connection.
            // Assume its not a catastrophic event and try to continue on

            if (!HandleVisaExceptions)
            {
                throw ve;
            }
        }
Example #3
0
        /// <summary>
        /// Set the mode based on the string. If it's one of the RWMode values, returns true.
        /// </summary>
        public static bool SetMode(string value)
        {
            modeStr = value ?? string.Empty;

            if (value != null)
            {
                bool success = Enum.TryParse(value, out RWMode res);
                mode = success ? res : RWMode.None;
                return(success);
            }
            mode = RWMode.None;
            return(false);
        }
Example #4
0
        public static byte ModeToByte(RWMode mode)
        {
            switch (mode)
            {
            case RWMode.READ:
                return(0x0);

            case RWMode.WRITE:
                return(0x1);

            default:
                return(0x2);
            }
        }
Example #5
0
        public static string ModeToString(RWMode mode)
        {
            switch (mode)
            {
            case RWMode.READ:
                return("READ");

            case RWMode.WRITE:
                return("READ");

            default:
                return("UNDEF");
            }
        }
Example #6
0
        internal override string RawCommand(string userinput)
        {
            string reply = string.Empty;

            try
            {
                string[]        parts = userinput.Split(new char[] { ';' }, 2);
                RWMode          mode  = (RWMode)parts[0][0];
                CommandCategory cat   = (CommandCategory)parts[0][1];
                string          cmd   = parts[0].Substring(2, 3);
                reply = mode + " " + cat + "-" + cmd + Environment.NewLine;

                byte[] data = new byte[0];
                if (parts.Length == 2)
                {
                    data = System.Text.Encoding.ASCII.GetBytes(parts[1]);
                }
                byte[] packet = ConstructPacket(_deviceAddress, mode, cat, cmd, data);
                bool   success;
                byte[] response = BlockingSend(packet, out success, Vislink_HDR1000.ETX);
                if (response != null)
                {
                    int            addr;
                    ResponseStatus status;
                    byte[]         respData;
                    ParseResponse(response, out addr, out status, out respData);
                    reply += "Received Bytes: " + ByteArrayToString(response) + Environment.NewLine;
                    reply += " Addr = " + addr + " Status = " + status + " (" + (char)status + ") Data = \"" + ToAsciiChars(respData) + "\"" + Environment.NewLine;
                }
                else
                {
                    reply += "<no response received>" + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                reply += Environment.NewLine + ex.ToString();
            }
            return(reply);
        }
Example #7
0
 public static bool SetMode(RWMode value)
 {
     modeStr = value.ToString();
     mode    = value;
     return(true);
 }
Example #8
0
        private void ExceptionHandler(System.Runtime.InteropServices.COMException ce, RWMode rw)
        {
            // this is only called from the Read() call if a COM exception is raised - assume it is not catastrophic and move on
            if (rw == RWMode.Read)
            {
                Console.Write("LOST COM READ ");
            }
            else
            {
                Console.Write("LOST COM WRITE ");
            }

            Console.WriteLine("Last command: " + lastCmd);
            Console.WriteLine("COM Exception type " + ce.ErrorCode.ToString(CultureInfo.InvariantCulture));

            {
                // else if ( ve.ErrorCode = VisaStatus.ErrorTimeout && rw == RWMode.Read )
                // timed out on a read - may be bacause the query data wasn't available
                // or that something is up with the visa connection.
                // Assume its not a catastrophic event and try to continue on

                if (!HandleVisaExceptions)
                {
                    throw ce;
                }
            }
        }
 public static extern MMIOError Advance(
     IntPtr hmmio,
     [In, Out, MarshalAs(UnmanagedType.LPStruct)] MMIOINFO pmmioinfo,
     RWMode fuAdvance);