FengChendian / serial_port_win32

A flutter SerialPort library using win32 API.
BSD 3-Clause "New" or "Revised" License
30 stars 8 forks source link

suddenly,getAvailablePorts() failed,can't open register. #23

Closed NickYoung-max closed 1 year ago

NickYoung-max commented 1 year ago

serial_port_win32 version is 0.5.2

run flutter doctor: [√] Flutter (Channel stable, 3.7.0, on Microsoft Windows [Version 10.0.19044.2728], locale zh-CN) [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.2.6) [√] Android Studio (version 2021.2) [√] Connected device (3 available) [√] HTTP Host Availability

when run the code SerialPort.getAvailablePorts() thorw execption like below:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: can't open Register

0 SerialPort._getRegistryKeyValue (package:serial_port_win32/src/serial_port.dart:483:9)

1 SerialPort.getAvailablePorts (package:serial_port_win32/src/serial_port.dart:545:18)

your example code is the same.

FengChendian commented 1 year ago

zh-CN? 那我直接用中文好了。看起来是你的注册表打开失败了,你看看你的注册表里有没有HKEY_LOCAL_MACHINE?这个报错我还是第一次见。

NickYoung-max commented 1 year ago

zh-CN? 那我直接用中文好了。看起来是你的注册表打开失败了,你看看你的注册表里有没有HKEY_LOCAL_MACHINE?这个报错我还是第一次见。

image 有的。不止我一台电脑是这样,我同事的也是一样的错误,以前是可以的,就是这周再测试的时候就报这个错了。

FengChendian commented 1 year ago

zh-CN? 那我直接用中文好了。看起来是你的注册表打开失败了,你看看你的注册表里有没有HKEY_LOCAL_MACHINE?这个报错我还是第一次见。

image 有的。不止我一台电脑是这样,我同事的也是一样的错误,以前是可以的,就是这周再测试的时候就报这个错了。

image 你继续展开看看,有没有HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM这条路径?

这段报错代码是为了查询注册表用的,打开失败可能是没有条目,但电脑上不应该没有,

  /// [_getRegistryKeyValue] will open RegistryKey in Serial Path.
  static int _getRegistryKeyValue() {
    final hKeyPtr = calloc<IntPtr>();
    try {
      if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _keyPath, 0, KEY_READ, hKeyPtr) !=
          ERROR_SUCCESS) {
        RegCloseKey(hKeyPtr.value);
        throw Exception("can't open Register");
      }
      return hKeyPtr.value;
    } finally {
      free(hKeyPtr);
    }
  }

我在 Flutter (Channel stable, 3.7.7, on Microsoft Windows [版本 10.0.22621.1413], locale zh-CN)上测试是没有问题的,如果还是不行,你看一下RegOpenKeyEx返回的到底是什么值。

另外,我在发布的新版本里更新了win32包的版本,或许有效果。

NickYoung-max commented 1 year ago

很奇怪,突然就好了,还是0.5.2版本,刚才试了一下,不报错了,我也没做其它操作

FengChendian commented 1 year ago

那可能是有别的软件也在扫注册表。下次你看看错误码。我下个版本把Error Code输出加进去吧,主要win32的GetLastError函数是废的,还得加额外的变量存储返回值当时就没加。

NickYoung-max commented 1 year ago

那可能是有别的软件也在扫注册表。下次你看看错误码。我下个版本把Error Code输出加进去吧,主要win32的GetLastError函数是废的,还得加额外的变量存储返回值当时就没加。

还是有问题,好像返回是固定的,都是com6、com7,其它设备都没有返回。 开机的时候(没有链接usb设备)打开注册表就发现有com6、com7,插上其它设备,还是只有com6、com7 image, 调用SerialPort.getAvailablePorts();方法的时候只返回com6、com7,其它链接的设备没有返回。

在我同事的win11上还是报Exception: can't open Register的错误,它电脑上的注册表是这样的 image

FengChendian commented 1 year ago

你同事的问题好解决,看起来是上面没有任何串口设备,所以没有SERIALCOMM文件夹,这个我加了个错误处理应该就能解决了。

你的话,应该是串口设备枚举失败,不是我库的问题。所有连接到Windows的虚拟串口(VCP)都会显示在这个注册表下。如果这里没有,说明是电脑没有识别到串口,或者你使用的不是由标准USB CDC协议实现的Virtual Com port。

NickYoung-max commented 1 year ago

跑刚刚更新的0.5.4包后是不报错了,但是只能识别rfid读卡器的设备连接,其它的像打印机、平板的usb连接就没有识别到,难道是因为它们的串口协议不是你说的USB CDC?

FengChendian commented 1 year ago
  1. 手机、平板的话看你选什么, 你如果选传输文件,应该是能读到一个串口的,选MIDI、仅充电之类的不会有。
  2. 打印机在我这边的电脑上是归类为HID设备的,不会在SERIALCOMM文件夹下。

PS: 一般读卡器肯定能读到,RFID一般都是输出SPI协议或者UART,后面会接个TTL转USB模块,比如CH340,这实现的就是USB CDC VCP的串口功能。

另外我这边测试的话一般是和Vofa+这个串口软件的结果直接比对,你可以看看Vofa+之类的能不能读到其他的串口,如果不能的话就不是库的问题。 还有就是你可以你在Windows的硬件管理里找到串口这一栏,上面所有的串口。

NickYoung-max commented 1 year ago

好的,感谢!