Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Receive data from Serial Usb

Hello!
I am trying to establish data exchange with my custom device connected via Serial Usb. I am using UsbSerialForAndroid https://github.com/anotherlab/UsbSerialForAndroid.
I managed to connect to my device and successfully send data.
My device should answer me right away, but I can’t track the response. The DataReceived event never fires.

public class SerialUsbConnector
    {
    const string ACTION_USB_PERMISSION = "USB_PERMISSION";
        private IUsbSerialPort _port;
        private UsbManager _usbManager;
        private SerialInputOutputManager_serialIoManager;
        public void Init()
        {
            _usbManager = Android.App.Application.Context.GetSystemService(Context.UsbService) as UsbManager;
                var drivers = await FindAllDriversAsync(_usbManager);
                if (drivers.Count == 0) return;
                var driver = drivers.ToArray()[0];
                if (driver == null)
                    throw new Exception("Driver specified in extra tag not found.");
                _port = driver.Ports[0];
                if (_port == null)
                {
                    return;
                }
                GetPremission(driver.Device);
                var portInfo = new UsbSerialPortInfo(_port);
                int vendorId = portInfo.VendorId;
                int deviceId = portInfo.DeviceId;
                int portNumber = portInfo.PortNumber;
                _serialIoManager = new SerialInputOutputManager(_port)
                {
                    BaudRate = 115200,
                    DataBits = 8,
                    StopBits = StopBits.One,
                    Parity = Parity.None,
                };
                _serialIoManager.DataReceived += (sender, e) => {
                   ///…///
                    System.Diagnostics.Debug.WriteLine(e.Data);
                };
                _serialIoManager.ErrorReceived += (sender, e) => {
                    ///…///
                };
                try
                {
                    _serialIoManager.Open(_usbManager);
                }
                catch (Java.IO.IOException e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                    return;
                }
                Device.BeginInvokeOnMainThread(() =>
                {
                    _serialIoManager.Write("some message", 50);   // Added
                });
        }
        private Task<IList<IUsbSerialDriver>> FindAllDriversAsync(UsbManager usbManager)
        {
        return UsbSerialProber.DefaultProber.FindAllDriversAsync (usbManager);
        }        
        private async void GetPremission(UsbDevice device)
        {
           ///…///
        }     
}

I added a method to write to a library class SerialInputOutputManager

public int Write(String str, int timeout)
    {
        int bytesWritten = 0;
        try
        {
            List<byte> buff = new List<byte>();
            buff.AddRange(Encoding.ASCII.GetBytes(str));
            bytesWritten = port.Write(buff.ToArray(), timeout);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message + "\n");
        }
            return bytesWritten;
    }

Can anybody help me?

Sign In or Register to comment.