예제 #1
0
        public int WriteData(byte[] wData)
        {
            if (outputReportSize == 0)
            {
                return(402);
            }
            if (false == connected)
            {
                return(406);
            }

            if (wData.Length < outputReportSize)
            {
                return(403);
            }
            if (writeRing == null)
            {
                return(405);
            }
            if (errCodeW != 0)
            {
                return(errCodeW);
            }
            if (writeRing.putIfCan(wData) == 3)
            {
                System.Threading.Thread.Sleep(1);
                return(404);
            }
            FileIOApiDeclarations.SetEvent(writeEvent);
            return(0);
        }
예제 #2
0
        public void CloseInterface()
        {
            if ((holdErrorThreadOpen) || (holdDataThreadOpen))
            {
                return;
            }

            // Shut down event thread
            if (dataThreadActive)
            {
                dataThreadActive = false;
                FileIOApiDeclarations.SetEvent(readEvent);
                int n = 0;
                if (dataThreadHandle != null)
                {
                    while (dataThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            dataThreadHandle.Abort(); break;
                        }
                    }
                    dataThreadHandle = null;
                }
            }

            // Shut down read thread
            if (readThreadActive)
            {
                readThreadActive = false;
                // Wait for thread to exit
                if (readThreadHandle != null)
                {
                    int n = 0;
                    while (readThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            readThreadHandle.Abort(); break;
                        }
                    }
                    readThreadHandle = null;
                }
            }
            if (writeThreadActive)
            {
                writeThreadActive = false;
                FileIOApiDeclarations.SetEvent(writeEvent);
                if (writeThreadHandle != null)
                {
                    int n = 0;
                    while (writeThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            writeThreadHandle.Abort(); break;
                        }
                    }
                    writeThreadHandle = null;
                }
            }
            if (errorThreadActive)
            {
                errorThreadActive = false;
                if (errorThreadHandle != null)
                {
                    int n = 0;
                    while (errorThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            errorThreadHandle.Abort(); break;
                        }
                    }
                    errorThreadHandle = null;
                }
            }

            if (writeRing != null)
            {
                writeRing = null;
            }
            if (readRing != null)
            {
                readRing = null;
            }

            //  if (readEvent != null) {readEvent = null;}
            //  if (writeEvent != null) { writeEvent = null; }

            if ((0x00FF != pid && 0x00FE != pid && 0x00FD != pid && 0x00FC != pid && 0x00FB != pid) || version > 272)
            {
                // it's not an old VEC foot pedal (those hang when closing the handle)
                if (readFileHandle != null)     // 9/1/09 - readFileHandle != null ||added by Onur to avoid null reference exception
                {
                    if (!readFileHandle.IsInvalid)
                    {
                        readFileHandle.Close();
                    }
                }
                if (writeFileHandle != null)
                {
                    if (!writeFileHandle.IsInvalid)
                    {
                        writeFileHandle.Close();
                    }
                }
            }
            connected = false;
        }
예제 #3
0
        protected void ReadThread()
        {
            //        FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");

            FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED();

            overlapped.Offset       = 0; // IntPtr.Zero;
            overlapped.OffsetHigh   = 0; // IntPtr.Zero;
            overlapped.hEvent       = (IntPtr)overlapEvent;
            overlapped.Internal     = IntPtr.Zero;
            overlapped.InternalHigh = IntPtr.Zero;
            if (inputReportSize == 0)
            {
                errCodeR  = 302;
                errCodeRE = 302;
                return;
            }
            errCodeR  = 0;
            errCodeRE = 0;

            byte[]   buffer = new byte[inputReportSize];
            GCHandle gch    = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired

            while (readThreadActive)
            {
                int dataRead = 0;    //FileIOApiDeclarations.
                if (readFileHandle.IsInvalid)
                {
                    errCodeRE = errCodeR = 320; goto EXit;
                }

                if (0 == FileIOApiDeclarations.ReadFile(readFileHandle, gch.AddrOfPinnedObject(), inputReportSize, ref dataRead, ref overlapped))     //ref readFileBuffer[0]
                {
                    int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                    if (result != FileIOApiDeclarations.ERROR_IO_PENDING)     //|| result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED)
                    {
                        if (readFileHandle.IsInvalid)
                        {
                            errCodeRE = errCodeR = 321; goto EXit;
                        }
                        errCodeR  = result;
                        errCodeRE = 308;
                        goto EXit;
                    }
                    else     //if (result != .ERROR_IO_PENDING)
                    {
                        // gch.Free(); //onur
                        while (readThreadActive)
                        {
                            result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 50);
                            if (FileIOApiDeclarations.WAIT_OBJECT_0 == result)
                            {
                                if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref dataRead, 0))
                                {
                                    result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                                    if (result == FileIOApiDeclarations.ERROR_INVALID_HANDLE || result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED)
                                    {
                                        errCodeR  = 309;
                                        errCodeRE = 309;
                                        goto EXit;
                                    }
                                }
                                // buffer[0] = 89;
                                goto ReadCompleted;
                            }
                        } //while
                    }     //if (result != .ERROR_IO_PENDING)...else
                    continue;
                }
                //buffer[0] = 90;
ReadCompleted:
                if (dataRead != inputReportSize)
                {
                    errCodeR = 310; errCodeRE = 310; goto EXit;
                }

                if (suppressDuplicateReports)
                {
                    int r = readRing.putIfDiff(buffer);
                    if (r == 0)
                    {
                        FileIOApiDeclarations.SetEvent(readEvent);
                    }
                }
                else
                {
                    readRing.put(buffer);
                    FileIOApiDeclarations.SetEvent(readEvent);
                }
            }    //while

EXit:
            FileIOApiDeclarations.CancelIo(readFileHandle);
            readFileHandle = null;
            gch.Free();     //onur
            return;
        }