コード例 #1
0
 protected override void OnResume()
 {
     base.OnResume();
     Log.Debug(TAG, "Resumed, port=" + mUsbSerialPort);
     if (mUsbSerialPort == null)
     {
         mTitleTextView.Text = "No serial device.";
     }
     else
     {
         try {
             mUsbSerialPort.Baudrate = 19200;
             mUsbSerialPort.Open();
         } catch (Exception e) {
             Log.Error(TAG, "Error setting up device: " + e.Message, e);
             mTitleTextView.Text = "Error opening device: " + e.Message;
             try {
                 mUsbSerialPort.Close();
             } catch (Exception) {
                 // Ignore.
             }
             mUsbSerialPort = null;
             return;
         }
         mTitleTextView.Text = "Serial device: " + mUsbSerialPort.GetType().Name;
     }
 }
        protected async override void OnResume()
        {
            Log.Info(TAG, "OnResume");

            base.OnResume();

            var portInfo   = Intent.GetParcelableExtra(EXTRA_TAG) as UsbSerialPortInfo;
            int vendorId   = portInfo.VendorId;
            int deviceId   = portInfo.DeviceId;
            int portNumber = portInfo.PortNumber;

            Log.Info(TAG, string.Format("VendorId: {0} DeviceId: {1} PortNumber: {2}", vendorId, deviceId, portNumber));

            var drivers = await MainActivity.FindAllDriversAsync(usbManager);

            var driver = drivers.Where((d) => d.Device.VendorId == vendorId && d.Device.DeviceId == deviceId).FirstOrDefault();

            if (driver == null)
            {
                throw new Exception("Driver specified in extra tag not found.");
            }

            port = driver.Ports[portNumber];
            if (port == null)
            {
                titleTextView.Text = "No serial device.";
                return;
            }
            Log.Info(TAG, "port=" + port);

            titleTextView.Text = "Serial device: " + port.GetType().Name;

            serialIoManager = new SerialInputOutputManager(port)
            {
                BaudRate = 9600,
                DataBits = 8,
                StopBits = StopBits.One,
                Parity   = Parity.None,
            };
            serialIoManager.DataReceived += (sender, e) =>
            {
                RunOnUiThread(() =>
                {
                    UpdateReceivedData(e.Data);
                });
            };
            serialIoManager.ErrorReceived += (sender, e) => {
                RunOnUiThread(() => {
                    var intent = new Intent(this, typeof(MainActivity));
                    StartActivity(intent);
                });
            };

            Log.Info(TAG, "Starting IO manager ..");
            try
            {
                serialIoManager.Open(usbManager);
            }
            catch (Java.IO.IOException e)
            {
                titleTextView.Text = "Error opening device: " + e.Message;
                return;
            }
        }
コード例 #3
0
        private async void StartReceiving()
        {
            if (App.UsbManager == null || App.PortInfo == null)
            {
                ToastService.ToastShortMessage(AppResources.No_Device);
                return;
            }

            var driver =
                await FindDriversService.GetSpecificDriverAsync(App.UsbManager,
                                                                App.PortInfo);

            if (driver != null)
            {
                _port = driver.Ports[App.PortInfo.PortNumber];
            }
            else
            {
                ToastService.ToastShortMessage(AppResources.No_Driver);
                return;
            }

            CurrentDeviceName = _port.GetType().Name;
            NotifyPropertyChanged(nameof(CurrentDeviceName));

            _serialIoManager = new SerialInputOutputManager(_port)
            {
                BaudRate = CurrentSettings.BaudRate,
                DataBits = CurrentSettings.DataBits,
                StopBits = CurrentSettings.StopBits,
                Parity   = CurrentSettings.Parity
            };
            _serialIoManager.DataReceived += (sender, e) =>
            {
                Device.BeginInvokeOnMainThread(() => { UpdateReceivedData(e.Data); });
            };
            _serialIoManager.ErrorReceived += (sender, e) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    ToastService.ToastShortMessage(AppResources
                                                   .Received_Error);
                });
            };
            ToastService.ToastShortMessage(AppResources.Port_Listening);

            try
            {
                _serialIoManager.Open(App.UsbManager);
            }
            catch (Exception)
            {
                ToastService.ToastShortMessage(
                    $"{AppResources.Open_Failed}: {CurrentDeviceName}");
            }
            finally
            {
                NotifyPropertyChanged(nameof(PortCommand));
                NotifyPropertyChanged(nameof(PortStatus));
            }
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.serial_console);

            ActionBar.SetTitle(Resource.String.test_console_title);

            ActivityStatus = TEST_STATUS.STANDBY;

            TransfarRate = DEFAULT_TRANSFAR_RATE;

            DeviceName = UseUsbSerialPort.GetType().Name;

            if (0 == String.Compare(DeviceName, 0, "Cdc", 0, 3))
            {
                IsCdcDevice = true;
            }
            else
            {
                IsCdcDevice = false;
            }

            TestModeTextView = (TextView)FindViewById(Resource.Id.test_mode);
            TitleTextView    = (TextView)FindViewById(Resource.Id.serial_device_name);

            TransfarRateTitleTextView = (TextView)FindViewById(Resource.Id.title_transfar_rate);
            TransfarRateValueTextView = (TextView)FindViewById(Resource.Id.transfar_rate_value);
            if (IsCdcDevice)
            {
                TransfarRateTitleTextView.Enabled = false;
                TransfarRateValueTextView.Enabled = false;
            }
            else
            {
                TransfarRateValueTextView.SetText(TransfarRate.ToString(), TextView.BufferType.Normal);
            }

            DumpTextView = (TextView)FindViewById(Resource.Id.consoleText);
            ScrollView   = (ScrollView)FindViewById(Resource.Id.demoScroller);

            ActivityStatusTextView = (TextView)FindViewById(Resource.Id.activity_status);
            ActivityStatusTextView.SetText(Resource.String.activity_status_standby);

            TestTimeTextView = (TextView)FindViewById(Resource.Id.test_time);
            TestTimeTextView.SetText(string.Format("{0:0#}:{1:0#}", TestTimePeriod / 60, TestTimePeriod % 60), TextView.BufferType.Normal);

            RemainTimeTextView = (TextView)FindViewById(Resource.Id.remain_time);
            RemainTimeTextView.SetText(Resource.String.remain_time_initial);

            TitleGoodTextView  = (TextView)FindViewById(Resource.Id.title_good);
            GoodCountTextView  = (TextView)FindViewById(Resource.Id.good_count);
            TitleErrorTextView = (TextView)FindViewById(Resource.Id.title_error);
            ErrorCountTextView = (TextView)FindViewById(Resource.Id.error_count);
            TitleTotalTextView = (TextView)FindViewById(Resource.Id.title_total);
            TotalCountTextView = (TextView)FindViewById(Resource.Id.total_count);

            ModeChangeButton        = (Button)FindViewById(Resource.Id.modeChange);
            ModeChangeButton.Click += ModeChangeButtonHandler;

            UseUsbSerialPort.DataReceivedEventLinser += DataReceivedHandler;

            CheckNmeaCheckSumInstance = new CheckNmeaCheckSum(this);
            CheckCyclic00ToFFInstance = new CheckCyclic00ToFF(this);
            CheckCyclic41To5AInstance = new CheckCyclic41To5A(this);
            CheckSendDataInstance     = new CheckSendData(this);
            CheckSendDataCdcInstance  = new CheckSendDataCdc(this);

            CheckInstance = CheckCyclic00ToFFInstance;
            TestModeTextView.SetText(CheckInstance.TestMode, TextView.BufferType.Normal);
        }