Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32 DCB structure feature #233

Closed
FengChendian opened this issue May 30, 2021 · 7 comments
Closed

win32 DCB structure feature #233

FengChendian opened this issue May 30, 2021 · 7 comments

Comments

@FengChendian
Copy link

Hello,
I can't find DCB struct in this package. Would it be possible to provide wrappers to DCB interfaces in winbase.h? It's important to implement serial port communication.
Thanks.

@timsneath
Copy link
Contributor

Of course! I've updated the main branch with this and added a number of relevant Win32 APIs.

Please let me know if you need anything else.

@timsneath
Copy link
Contributor

v2.1.2 is published, including these changes: https://pub.dev/packages/win32

@FengChendian
Copy link
Author

Thanks for quick updates! Now I can find DCB struct.

But I have a little problem about setting DCB parameters.
In C++, I can

DCB dcbSerialParameters = {0};
if (!GetCommState(this->handler, &dcbSerialParameters))
{
    std::cerr << "Failed to get current serial parameters\n";
} else
{
    dcbSerialParameters.BaudRate = CBR_115200;
}

How can I set dcbSerialParameters in dart? I try

final dcb = DCB();
dcb.BaudRate = 115200;

But I met "Exited (3221225477)"

@timsneath
Copy link
Contributor

You need to do something like:

final dcb = calloc<DCB>();
dcb.ref.BaudRate = 115200;

DCB has to be natively allocated (hence the calloc). The first line returns a Pointer<DCB>, and then you can access the underlying members using the .ref property.

@FengChendian
Copy link
Author

FengChendian commented Jun 3, 2021

Thanks! Now I know how to use native Struct.

BTW, I found when I open "COM5" using dart, the handle always euqal -1. But I can open this port sucessfully with similar C++ code.
Because of GetLastError() #189 problem, I don't know if this is my code bug or package bug. When dart-lang/sdk#38832 fixed, I will track this. Here is my dart code:

final portName = TEXT("COM5");

final handler = CreateFile(portName, GENERIC_ALL, 0, nullptr, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, NULL);

if (handler == INVALID_HANDLE_VALUE) {
  final lastError = GetLastError();
  if (lastError == ERROR_FILE_NOT_FOUND) {
    print(portName.toDartString() + "is not available");
  } else {
    print('get ${lastError}');
  }
  print("handler error");
  return;
}

@timsneath
Copy link
Contributor

I added an example here:
https://github.com/timsneath/win32/blob/ac9a8bc7febc11f04e57da105b122c3941b23b0c/example/serial.dart

This seems to work successfully on my machine at least:

[C:\src\win32] dart example\serial.dart
BaudRate = 1200, ByteSize = 7, Parity = 2, StopBits = 0
BaudRate = 57600, ByteSize = 8, Parity = 0, StopBits = 0

@FengChendian
Copy link
Author

LOL... After compare with the example, I found my code bug. I use GENERIC_ALL instead ofGENERIC_READ | GENERIC_WRITE in dart.
Now it worked! Thank you very much!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants