0

I have to catch barcode from bluetooth SPP scanner. I can open the com port and catch all scans, however after 2 minutes the scanner goes sleep. When the scanner wakes up, it's not connected anymore but the com port still open. I need a way to find out, if the scanner still connected, or gone to sleep and close/re-open the com port when the scanner wake up again.

I pair the scanner with the pc in win10, which creates an input com port (com4). I can open that port and listen to messages. The problem is, the scanner use only 1 way communication and seems it does not send anything to say "i disconnect now", it just go sleep and disconnect. Is any way to see the scanner still connected, or gone to sleep, or anything to hook up to? The scanner is WASP WWS550i.

        _serialPort = New SerialPort(PortName, BaudRate, Parity.None, 8, StopBits.One)
        _serialPort.PortName = PortName
        _serialPort.Parity = Parity.None
        _serialPort.DataBits = 8
        _serialPort.BaudRate = BaudRate
        _serialPort.StopBits = StopBits.One

        AddHandler _serialPort.DataReceived, AddressOf sp_DataReceived

        _serialPort.Open()

Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    _LastDataReceived = _serialPort.ReadLine()
End Sub
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
fra
  • 186
  • 1
  • 12
  • [DeviceWatcher](https://learn.microsoft.com/en-us/uwp/api/Windows.Devices.Enumeration.DeviceWatcher) may be used for UWP applications. [Enumerate devices](https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/enumerate-devices) If it is a desktop application, try handling the WM_DEVICECHANGE message in [NativeWindow](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.nativewindow?view=netframework-4.8). [Detecting Bluetooth SPP serial port disconnect (C# 4.5 Win 8.1)](https://social.msdn.microsoft.com/Forums/vstudio/en-US/b9be2478-76ed-4aff-bd81-44c12f6709e6/) – kunif Sep 12 '19 at 13:22
  • I tried WM_DEVICECHANGE but it does not send any message about the port. I guess because it's a virtual port and the scanner doesn't send anything to say it's disconnected. – fra Sep 12 '19 at 14:45
  • Perhaps the same person was asking a question on StackOverflow. [Detecting Bluetooth SPP serial port disconnect (C# 4.5, Windows 8.1)](https://stackoverflow.com/q/32341397/9014308) It seems that bluetooth does not generate a message. Unfortunately it seems difficult to detect. – kunif Sep 12 '19 at 14:55
  • WM_DEVICECHANGE and look for GUID_BLUETOOTH_HCI_EVENT or for GUID_BLUETOOTH_RADIO_OUT_OF_RANGE: https://learn.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-and-wm-devicechange-messages – Mike Petrichenko Sep 12 '19 at 15:27
  • The other question wasn't from me. However seems there is no way to detect the status of this kind of connection, especial because it's not 2 way communication. Also I have to be able to connect any barcode readers from a lot anytime, so I think it is not gonna work. I am looking for different barcode scanners now and do it trough BLE. Thanks anyways – fra Sep 13 '19 at 07:42

1 Answers1

1

I had exactly same problem with Pos tablet scanner. ASP.CORE 3 WORKER SERVICE

I solved this issue by reopen the serial port after wake up. Also if I tried to reopen the port, the port says Port is unavailable because port was not properly closed (dispose).


My solution

I wrote 1 second checker which check the port if is still open. If is not then call close() on previous instance of serial port, after that reopen the port.

Now I can scanning after wake up.

public class Worker : BackgroundService
    {
        private static SerialPort SERIAL_PORT;

        // constructor DI

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {

        }

        public override Task StartAsync(CancellationToken cancellationToken)
        {
            // create serial port connection
            _logger.LogInformation($"-- open serial port {PORT_NAME}");

            SERIAL_PORT = SerialPortFactory.Create(PORT_NAME);
            SERIAL_PORT.Open();

            _logger.LogInformation($"-- serial port {PORT_NAME} opened");

            Task.Run(() => Checker(_logger)); // run port checker on another thread

            return base.StartAsync(cancellationToken);
        }

        public override Task StopAsync(CancellationToken cancellationToken)
        {
            SERIAL_PORT.Close();
            return base.StopAsync(cancellationToken);
        }

        public static void Checker(ILogger<Worker> _logger)
        {
            for (;;)
            {
                _logger.LogInformation("Sleep for 1 second!");
                Thread.Sleep(1000);
                try
                {
                    _logger.LogInformation(SERIAL_PORT.IsOpen.ToString());
                    if(!SERIAL_PORT.IsOpen)
                    {
                        SERIAL_PORT.Close();
                        Thread.Sleep(100); // is not necessary but ...

                        SERIAL_PORT = SerialPortFactory.Create(PORT_NAME);
                        SERIAL_PORT.Open();

                        _logger.LogInformation($"-- serial port {PORT_NAME} opened");
                    }
                } catch(Exception e)
                {
                    _logger.LogError(e.Message);
                }
            }
        }
    }

SerialPortFactory - is only my simple wrapper class for new SerialPort from MS doc

daremachine
  • 2,678
  • 2
  • 23
  • 34