void UpdateReceivedData(byte[] data)
        {
            var message = HexDump.DumpHexString(data);

            dumpTextView.Append(message);
            scrollView.SmoothScrollTo(0, dumpTextView.Bottom);
        }
Example #2
0
        void UpdateReceivedData(byte[] data)
        {
            var message = "Read " + data.Length + " bytes: \n"
                          + HexDump.DumpHexString(data) + "\n\n";

            dumpTextView.Append(message);
            scrollView.SmoothScrollTo(0, dumpTextView.Bottom);
        }
        void UpdateReceivedData(byte[] data)
        {
            string result = Encoding.UTF8.GetString(data);

            var message = "Read " + data.Length + " bytes: " + HexDump.DumpHexString(data) + "\n";

            dumpTextView.Append(message);
            scrollView.SmoothScrollTo(0, dumpTextView.Bottom);
        }
Example #4
0
        public override int Read(byte[] dest, int timeoutMilliseconds)
        {
            UsbEndpoint endpoint = _driver.Device.GetInterface(0).GetEndpoint(0);

            if (ENABLE_ASYNC_READS)
            {
                int readAmt;
                lock (_readBufferLock)
                {
                    readAmt = Math.Min(dest.Length, _readBuffer.Length);
                }

                var request = new UsbRequest();
                request.Initialize(_connection, endpoint);

                var buf = ByteBuffer.Wrap(dest);
                if (!request.Queue(buf, readAmt)) //TODO: Must fix this
                {
                    throw new UsbSerialException("Error queueing request.");
                }

                UsbRequest response = _connection.RequestWait();
                if (response == null)
                {
                    throw new UsbSerialException("Null response");
                }

                int payloadBytesRead = buf.Position() - MODEM_STATUS_HEADER_LENGTH;
                if (payloadBytesRead > 0)
                {
                    Log.Debug(nameof(FtdiSerialDriver), HexDump.DumpHexString(dest, 0, Math.Min(32, dest.Length)));
                    return(payloadBytesRead);
                }
                else
                {
                    return(0);
                }
            }
            else
            {
                lock (_readBufferLock)
                {
                    int readAmt        = Math.Min(dest.Length, _readBuffer.Length);
                    int totalBytesRead = _connection.BulkTransfer(endpoint, _readBuffer, readAmt, timeoutMilliseconds);

                    if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH)
                    {
                        throw new UsbSerialException($"Expected at least {MODEM_STATUS_HEADER_LENGTH} bytes");
                    }

                    return(FilterStatusBytes(_readBuffer, dest, totalBytesRead, endpoint.MaxPacketSize));
                }
            }
        }
        void UpdateReceivedData(byte[] data)
        {
            try
            {
                ConnectivityManager ConnectivityManager = (ConnectivityManager)Application.Context.GetSystemService(Context.ConnectivityService);
                NetworkInfo         connection          = ConnectivityManager.ActiveNetworkInfo;
                if (connection != null && connection.IsConnected && CrossConnectivity.Current.IsConnected)
                {
                    var message = HexDump.DumpHexString(data) + "\n\n";
                    dumpTextView.Append(message);
                    scrollView.SmoothScrollTo(0, dumpTextView.Bottom);
                }
                else
                {
                    if (wake.IsHeld && wifilock.IsHeld)
                    {
                        wake.Release();     //libera wakeLock
                        wifilock.Release(); //libera wifiLock
                    }
                    activateSleep = false;
                    PowerManager pwm = (PowerManager)GetSystemService(Context.PowerService);
                    WakeLock     wkl = pwm.NewWakeLock(WakeLockFlags.Full | WakeLockFlags.AcquireCausesWakeup | WakeLockFlags.OnAfterRelease, "wakeup device");
                    wkl.Acquire();
                    wkl.Release();

                    Finish();
                    intent = PackageManager.GetLaunchIntentForPackage("com.flexolumens.MonitorInteligente");
                    StartActivity(intent);
                    //OnDestroy();
                }

                if (activateSleep == true && (connection != null && connection.IsConnected) && CrossConnectivity.Current.IsConnected)
                {
                    wake.Acquire();
                    wifilock.Acquire();
                    activateSleep = false;
                    intento       = PackageManager.GetLaunchIntentForPackage("com.ssaurel.lockdevice");
                    StartActivity(intento);
                    //if (!wifi.IsWifiEnabled)
                    //{
                    //wifi.SetWifiEnabled(true);
                    //}
                }
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
            }
        }
Example #6
0
            public override int Read(byte[] dest, int timeoutMillis)
            {
                if (mEnableAsyncReads)
                {
                    UsbRequest request = new UsbRequest();
                    try
                    {
                        request.Initialize(mConnection, mReadEndpoint);

                        // CJM: Xamarin bug:  ByteBuffer.Wrap is supposed to be a two way update
                        // Changes made to one buffer should reflect in the other.  It's not working
                        // As a work around, I added a new method as an extension that uses JNI to turn
                        // a new byte[] array.  I then used BlockCopy to copy the bytes back the original array
                        // see https://forums.xamarin.com/discussion/comment/238396/#Comment_238396
                        //
                        // Old work around:
                        // as a work around, we populate dest with a call to buf.Get()
                        // see https://bugzilla.xamarin.com/show_bug.cgi?id=20772
                        // and https://bugzilla.xamarin.com/show_bug.cgi?id=31260

                        ByteBuffer buf = ByteBuffer.Wrap(dest);
                        if (!request.Queue(buf, dest.Length))
                        {
                            throw new IOException("Error queueing request.");
                        }

                        UsbRequest response = mConnection.RequestWait();
                        if (response == null)
                        {
                            throw new IOException("Null response");
                        }

                        int nread = buf.Position();
                        if (nread > 0)
                        {
                            // CJM: This differs from the Java implementation.  The dest buffer was
                            // not getting the data back.

                            // 1st work around, no longer used
                            //buf.Rewind();
                            //buf.Get(dest, 0, dest.Length);

                            System.Buffer.BlockCopy(buf.ToByteArray(), 0, dest, 0, dest.Length);

                            Log.Debug(TAG, HexDump.DumpHexString(dest, 0, Math.Min(32, dest.Length)));
                            return(nread);
                        }
                        else
                        {
                            return(0);
                        }
                    }
                    finally
                    {
                        request.Close();
                    }
                }

                int numBytesRead;

                lock (mReadBufferLock)
                {
                    int readAmt = Math.Min(dest.Length, mReadBuffer.Length);
                    numBytesRead = mConnection.BulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                                                            timeoutMillis);
                    if (numBytesRead < 0)
                    {
                        // This sucks: we get -1 on timeout, not 0 as preferred.
                        // We *should* use UsbRequest, except it has a bug/api oversight
                        // where there is no way to determine the number of bytes read
                        // in response :\ -- http://b.android.com/28023
                        if (timeoutMillis == Integer.MaxValue)
                        {
                            // Hack: Special case "~infinite timeout" as an error.
                            return(-1);
                        }
                        return(0);
                    }
                    System.Buffer.BlockCopy(mReadBuffer, 0, dest, 0, numBytesRead);
                }
                return(numBytesRead);
            }
            public override int Read(byte[] dest, int timeoutMillis)
            {
                var endpoint = mDevice.GetInterface(0).GetEndpoint(0);

                if (ENABLE_ASYNC_READS)
                {
                    int readAmt;
                    lock (mReadBufferLock)
                    {
                        // mReadBuffer is only used for maximum read size.
                        readAmt = Math.Min(dest.Length, mReadBuffer.Length);
                    }

                    var request = new UsbRequest();
                    request.Initialize(mConnection, endpoint);

                    var buf = ByteBuffer.Wrap(dest);
                    if (!request.Queue(buf, readAmt))
                    {
                        throw new IOException("Error queueing request.");
                    }

                    var response = mConnection.RequestWait();
                    if (response == null)
                    {
                        throw new IOException("Null response");
                    }

                    var payloadBytesRead =
                        buf.Position() - MODEM_STATUS_HEADER_LENGTH;
                    if (payloadBytesRead > 0)
                    {
                        // CJM: This differs from the Java implementation.  The dest buffer was
                        // not getting the data back.
                        Buffer.BlockCopy(buf.ToByteArray(),
                                         0,
                                         dest,
                                         0,
                                         dest.Length);

                        Log.Debug(TAG,
                                  HexDump.DumpHexString(dest,
                                                        0,
                                                        Math.Min(32, dest.Length)));
                        return(payloadBytesRead);
                    }

                    return(0);
                }

                int totalBytesRead;

                lock (mReadBufferLock)
                {
                    var readAmt = Math.Min(dest.Length, mReadBuffer.Length);

                    // todo: replace with async call
                    totalBytesRead = mConnection.BulkTransfer(endpoint,
                                                              mReadBuffer,
                                                              readAmt,
                                                              timeoutMillis);

                    if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH)
                    {
                        throw new IOException(
                                  "Expected at least "
                                  + MODEM_STATUS_HEADER_LENGTH
                                  + " bytes");
                    }

                    return(FilterStatusBytes(mReadBuffer,
                                             dest,
                                             totalBytesRead,
                                             endpoint.MaxPacketSize));
                }
            }