Exemplo n.º 1
0
            public void Put(byte[] src)
            {
                if (!Monitor.TryEnter(LockObject, 5000))
                {
                    throw new System.TimeoutException("LNGCommunication.PhysicalLayer.Channel.CreateChannel Lock_Exception");
                }
                try
                {
                    if (src == null || src.Length == 0)
                    {
                        return;
                    }

                    if (debugging)
                    {
                        UsbSerialDebugger.PrintLogPut(src, true);
                    }

                    buffer.Write(src);
                    Monitor.Pulse(LockObject);
                }
                finally
                {
                    Monitor.Exit(LockObject);
                }
            }
Exemplo n.º 2
0
            public byte[] Get()
            {
                if (!Monitor.TryEnter(LockObject, 5000))
                {
                    throw new System.TimeoutException("LNGCommunication.PhysicalLayer.Channel.CreateChannel Lock_Exception");
                }
                try
                {
                    //if (position == -1)
                    if (buffer.Size() == 0)
                    {
                        try
                        {
                            Monitor.Wait(LockObject);
                        }
                        catch (Java.Lang.InterruptedException e)
                        {
                            e.PrintStackTrace();
                            Thread.CurrentThread.Interrupt();
                        }
                    }

                    byte[] dst;
                    if (buffer.Size() <= MAX_BULK_BUFFER)
                    {
                        dst = buffer.ReadByteArray();
                    }
                    else
                    {
                        try
                        {
                            dst = buffer.ReadByteArray(MAX_BULK_BUFFER);
                        }
                        catch (Java.IO.EOFException e)
                        {
                            Log.Error("com.felhr.usbserial.SerialBuffer.SynchronizedBuffer.Get", e.Message);
                            return(new byte[0]);
                        }
                    }

                    if (debugging)
                    {
                        UsbSerialDebugger.PrintLogGet(dst, true);
                    }
                    return(dst);
                }
                finally
                {
                    Monitor.Exit(LockObject);
                }
            }
Exemplo n.º 3
0
 public override int SyncWrite(byte[] buffer, int timeout)
 {
     if (!asyncMode)
     {
         if (buffer == null)
         {
             return(0);
         }
         if (debugging)
         {
             UsbSerialDebugger.PrintLogPut(buffer, true);
         }
         return(connection.BulkTransfer(outEndpoint, buffer, buffer.Length, timeout));
     }
     else
     {
         return(-1);
     }
 }
Exemplo n.º 4
0
            public override void DoRun()
            {
                UsbRequest request = null;

                /**
                 * blocking call on usb device.
                 * Make sure for UsbRequest object RequestWait and Queue functions do not deadlock from different threads
                 **/

                //request = device.connection.RequestWait();
                if (!isSpecialCase)
                {
                    try
                    {
                        request = device.connection.RequestWait(2000);  //blocking call on usbdevice with optional timeout
                    }
                    catch (Java.Lang.NoSuchMethodError)
                    {
                        isSpecialCase = true;
                        Log.Error("com.felhr.usbserial.UsbSerialDevice.WorkerThread.DoRun", "Exception at RequestWait(): Special Case");
                    }
                    catch (Java.Util.Concurrent.TimeoutException)
                    {
                        /*do nothing and continue*/
                    }
                }
                else
                {
                    try
                    {
                        request = device.connection.RequestWait();  //blocking call on usbdevice
                    }
                    catch (Java.Lang.Exception)
                    {
                        /*do nothing and continue*/
                        Log.Error("com.felhr.usbserial.UsbSerialDevice.WorkerThread.DoRun", "Exception at RequestWait()");
                    }
                }

                if (request?.Endpoint.Type == UsbAddressing.XferBulk &&
                    request.Endpoint.Direction == UsbAddressing.In)
                {
                    byte[] data = device.serialBuffer.GetDataReceived();

                    // FTDI devices reserves two first bytes of an IN endpoint with info about
                    // modem and Line.
                    if (device.IsFTDIDevice())
                    {
                        ((FTDISerialDevice)device).ftdiUtilities.CheckModemStatus(data); //Check the Modem status
                        device.serialBuffer.ClearReadBuffer();

                        if (data.Length > 2)
                        {
                            data = FTDISerialDevice.AdaptArray(data);
                            OnReceivedData(data);
                        }
                    }
                    else
                    {
                        // Clear buffer, execute the callback
                        device.serialBuffer.ClearReadBuffer();
                        OnReceivedData(data);
                    }

                    if (debugging && !(data.Length <= 2 && device.IsFTDIDevice()))
                    {
                        UsbSerialDebugger.PrintReadLogGet(data, true);
                    }

                    // Queue a new request
                    //System.Threading.Thread.Sleep(10);

                    if (isAPI26Version) //required check for Java.Lang.NoSuchMethodError thrown for some manufacturers on Queue(buffer); deprecation of Queue(buffer, size) since API 26
                    {
                        requestIN?.Queue(device.serialBuffer.GetReadBuffer());
                    }
                    else
                    {
                        requestIN?.Queue(device.serialBuffer.GetReadBuffer(), SerialBuffer.DEFAULT_READ_BUFFER_SIZE);      //blocking call on usbdevice
                    }
                }
            }