Esempio n. 1
0
        public bool Available(int portIndex)
        {
            string portName = "COM" + portIndex.ToString() + ":";
            IntPtr tmphPort = Com_WinApi.CreateFileA(portName,
                (UInt32)dwDesiredAccess.GENERIC_READ, 0, IntPtr.Zero,
                (UInt32)dwCreationDisposion.OPEN_EXISTING, 0x0, IntPtr.Zero);
            if (tmphPort == (IntPtr)Com_WinApi.INVALID_HANDLE_VALUE)
                return false;
            else
                Com_WinApi.CloseHandle(tmphPort);

            return true;
        }
Esempio n. 2
0
        /// <summary>
        /// Перевідкриття СОМ-порту
        /// </summary>
        /// <returns>Якщо true то СОМ-порт перевідкриття СОИ-порту відбулося успішно</returns>
        public bool ReOpen()
        {
            bool fRez = false;

            if (isOpen)
            {
                PortClear();
                fRez = Com_WinApi.CloseHandle(handle);
            }

            isOpen = false;
            if (fRez)
                isOpen = Open(portIndex);

            return isOpen;
        }
Esempio n. 3
0
        /// <summary>
        /// Check for valid file path.
        /// </summary>
        /// <param name="fileName">File path for check</param>
        /// <param name="timeout">Timeout for executing function</param>
        /// <exception cref="Timeout">Throw when file is not exists</exception>
        /// <returns>Return true when path is valid else false.</returns>
        public static bool FileExists(string path, int timeout, string exceptionSuffix)
        {
            lastResult = null;
            Com_WinApi.OutputDebugString("func start");
            th = new Thread(new ParameterizedThreadStart(FileExsist));
            th.IsBackground = true;
            th.Start(path);
            th.Join(timeout);
            if (th.IsAlive)
            {
                th.Abort();
                Com_WinApi.OutputDebugString("PathFileExists: time is out");
                throw new TimeoutException("Timeout" + exceptionSuffix);
                //return false;
            }

            if (lastResult != null)
            {
                return((bool)lastResult);
            }

            return(false);

            //fdPathFileExists ff = winapi.Funcs.PathFileExists;
            //IAsyncResult result = ff.BeginInvoke(fileName, new AsyncCallback(WhenPathFileExists), "");
            //Thread.Sleep(timeout);
            //if (lastResult == null)
            //{
            //    winapi.Funcs.OutputDebugString("PathFileExists: time is out");
            //    fl_timeout = true;
            //    //throw new TimeoutException("Timeout");
            //}
            //else
            //    return (bool)lastResult;

            //return false;
        }
Esempio n. 4
0
        /// <summary>
        /// Check if file or directory exist
        /// </summary>
        /// <param name="parameter">File name or directory path</param>
        private static void FileExsist(object path)
        {
            Com_WinApi.OutputDebugString("FileExsist start");

            if (path == null)
            {
                lastResult = false;
                return;
            }

            string fileName = path.ToString();

            if (string.IsNullOrEmpty(fileName))
            {
                lastResult = false;
                return;
            }

            uint attributes = Com_WinApi.GetFileAttributes(fileName);

            if (Com_WinApi.INVALID_FILE_ATTRIBUTE == attributes)
            {
                Com_WinApi.GetLastError();

                //if (winapi.Consts.ERROR_FILE_NOT_FOUND == error ||
                //    winapi.Consts.ERROR_PATH_NOT_FOUND == error)
                Com_WinApi.OutputDebugString("FileExsist end _ not valid atribute");
                lastResult = false;
                return;
            }

            //isDirectory = 0 != (winapi.Consts.FILE_ATTRIBUTE_DIRECTORY & attributes);

            Com_WinApi.OutputDebugString("FileExsist end");
            lastResult = true;
        }