FengChendian / serial_port_win32

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

Exception: SetCommState error #31

Open ChanakaWeerasinghe opened 6 months ago

ChanakaWeerasinghe commented 6 months ago

When I'm trying to connect the serial port I'm getting the below issue. could you please help me to fix this error.

port = SerialPort("COM3",
                              ByteSize: 8,
                              Parity :2,
                              StopBits : 1,
                              BaudRate: 9600,
                              openNow: false, ReadIntervalTimeout: 1, ReadTotalTimeoutConstant: 2
                              );
            port.open();

Exception: SetCommState error

When the exception was thrown, this was the stack:

0 SerialPort._setCommState (package:serial_port_win32/src/serial_port.dart:269:7)

1 SerialPort.open (package:serial_port_win32/src/serial_port.dart:174:7)

2 SerialPort.openWithSettings (package:serial_port_win32/src/serial_port.dart:263:5)

3 _MyHomePageState._getPortsAndOpen (package:tster/main.dart:57:12)

4 _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1183:21)

5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:275:24)

6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:652:11)

7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:309:5)

FengChendian commented 6 months ago

StopBits == 1 means ONE5STOPBITS (1.5) in Windows API. You can not set stop bits to ONE5STOPBITS when you set ByteSize to 8.

If you want to set one stop bit, you should use const variable ONESTOPBIT or 0

See

https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-dcb

When a DCB structure is used to configure the 8250, the following restrictions apply to the values specified for the ByteSize and StopBits members:

ChanakaWeerasinghe commented 6 months ago

@FengChendian Thanks for clarification. When I remove stop bytes 1 it's working. You can close the issue. Also One more small question but not related to the above issue.

I need to send a hex to the device.


String buffer = "02 01 32 03 36";
port.writeBytesFromString(buffer);

is this correct?

FengChendian commented 6 months ago

@FengChendian Thanks for clarification. When I remove stop bytes 1 it's working. You can close the issue. Also One more small question but not related to the above issue.

I need to send a hex to the device.


String buffer = "02 01 32 03 36";
port.writeBytesFromString(buffer);

is this correct?

It's wrong. String will be transferred to char(c language) according to asiic code. In dart, you should use uint8list. And you need be careful to convert hex to decimal.

ChanakaWeerasinghe commented 5 months ago

@FengChendian Thank you for clarification sir.

Also i got error in here .could you please help me to resolve this issue.

if (GetLastError() == ERROR_IO_PENDING) {
    /// wait io complete, timeout in 500ms
    for (int i = 0; i < 1000; i++) {
          if (WaitForSingleObject(_over.ref.hEvent, 0) == 0) {
          ClearCommError(handler!, _errors, _status);
                if (_status.ref.cbInQue != 0) {
                **readData.add((await _read(1))[0]);<----------------here
                } else {}
          ResetEvent(_over.ref.hEvent);
          // break for next read operation.
          break;
          } else {
          ResetEvent(_over.ref.hEvent);
          // continue waiting
          await Future.delayed(dataPollingInterval);
          }
    }
}

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: RangeError (index): Index out of range: no indices are valid: 0

0 Uint8List.[] (dart:typed_data-patch/typed_data_patch.dart:2259:7)

1 SerialPort.readBytesUntil (package:serial_port_win32/src/serial_port.dart:458:46)

FengChendian commented 5 months ago

@FengChendian Thank you for clarification sir.

Also i got error in here .could you please help me to resolve this issue.

if (GetLastError() == ERROR_IO_PENDING) {
    /// wait io complete, timeout in 500ms
    for (int i = 0; i < 1000; i++) {
          if (WaitForSingleObject(_over.ref.hEvent, 0) == 0) {
          ClearCommError(handler!, _errors, _status);
                if (_status.ref.cbInQue != 0) {
                **readData.add((await _read(1))[0]);<----------------here
                } else {}
          ResetEvent(_over.ref.hEvent);
          // break for next read operation.
          break;
          } else {
          ResetEvent(_over.ref.hEvent);
          // continue waiting
          await Future.delayed(dataPollingInterval);
          }
    }
}

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: RangeError (index): Index out of range: no indices are valid: 0 #0 Uint8List.[] (dart:typed_data-patch/typed_data_patch.dart:2259:7) #1 SerialPort.readBytesUntil (package:serial_port_win32/src/serial_port.dart:458:46)

Are you sure that the COM port configuration is true?

if (_status.ref.cbInQue != 0), the data will be larger than zero. And read(1) should return a list within one data. The function uses [0] to get Uint8List Value at index 0.

Can you get data using the following function in doc?

port.readOnListenFunction = (value) {
print(value);
};