-
Notifications
You must be signed in to change notification settings - Fork 143
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
Only 63 COM Ports can be opened concurrently on Windows #235
Comments
A quick look into the code reveals this line: nrjavaserial/src/main/c/src/SerialImp.c Line 4779 in e897e39
You could try to increase it. |
Thanks, @fwolter, beat me to it. You can also try putting JNA 4 on your classpath. If it's available at runtime, port detection uses it to enumerate serial ports from the registry:
That should bypass the upper bound set in the native code. When investigating #196, the only documented limit to the number of serial ports on Windows that I could find was 256. On the driver side, the maximum number of serial ports is given by a constant that isn't specifically published but does seem to be 4,096. Out of curiosity, are you aware of any official documentation to support the 4,096-port limit in Windows 10? |
Hi, thanks for the quick answers! I also think that the jna should be present. At least it is in my maven dependencies. But I will try to debug the native code and see where it might have a problem. Regarding the official info about the 4096 ports. I did not find where I got that from, but I think it was somerwhere in the Windows Registry. If I find it I will post it here. |
I think I may have found the Problem. In the I tried just adding 1 to it there and I was successful opening another port. Now when I add the following lines to the
I have to say, that I am not a C and Windows expert so I do not know if this has any side effects in the application. For my usecase it seems to work. Regarding the maximum COM ports on Windows if found this site where it says: https://learn.microsoft.com/en-us/windows-hardware/drivers/serports/external-naming-of-com-ports |
Good find!
Interesting. This would definitely blow up on POSIX systems where the value of I think the safest approach here is to probably define the platform-dependent maximum port number; on POSIX, this can be based on diff --git a/src/main/c/include/SerialImp.h b/src/main/c/include/SerialImp.h
index 329140e..c9dc443 100644
--- a/src/main/c/include/SerialImp.h
+++ b/src/main/c/include/SerialImp.h
@@ -281,12 +281,16 @@ Trent
# define DEVICEDIR "//./"
# define LOCKDIR ""
# define LOCKFILEPREFIX ""
+// Windows has no strict limit to the number of open serial ports, but we need
+// to define something.
+# define PORT_MAX 4096
# define OPEN serial_open
# define CLOSE serial_close
# define WRITE serial_write
# define READ serial_read
# define SELECT serial_select
#else /* use the system calls for Unix */
+# define PORT_MAX FD_SETSIZE
# define OPEN open
# define CLOSE close
# define WRITE write
diff --git a/src/main/c/src/SerialImp.c b/src/main/c/src/SerialImp.c
index 5ea7404..fcd6af4 100644
--- a/src/main/c/src/SerialImp.c
+++ b/src/main/c/src/SerialImp.c
@@ -4195,15 +4195,15 @@ int initialise_event_info_struct( struct event_info_struct *eis )
}
end:
FD_ZERO( &eis->rfds );
- if (eis->fd < FD_SETSIZE && eis->fd > 0) {
+ if (eis->fd < PORT_MAX && eis->fd > 0) {
FD_SET( eis->fd, &eis->rfds );
eis->tv_sleep.tv_sec = 0;
eis->tv_sleep.tv_usec = 100 * 1000;
eis->initialised = 1;
return( 1 );
- } else if(eis->fd >= FD_SETSIZE ){
+ } else if(eis->fd >= PORT_MAX ){
// you can reduce this limitation only with migration to epool or something like that.
- report_error("initialise_event_info_struct: eis->fd >= FD_SETSIZE!\n");
+ report_error("initialise_event_info_struct: eis->fd >= PORT_MAX!\n");
} else if(eis->fd <= 0 ){
// you can reduce this limitation only with migration to epool or something like that.
report_error("initialise_event_info_struct: eis->fd <= 0!\n"); I'm glad you have an application-specific solution that works for you for now. I'll package my diff up as a PR and get that up in a bit. |
Hm, actually, that still might cause problems on Windows, because
Mind you, |
I am testing it with a seperate PORT_MAX constant and it seems to work as well. But I get that you don't want to run into undefined behaviour. |
Thanks for the feedback! Btw, out of curiosity, where are you getting all of these serial ports from? Are these virtual/network serial ports, or do you have that many actual hardware serial ports? (Just wondering if there's a convenient way for me also to get 64+ ports for local reproduction of this issue.) |
Our actual usecase is using network provided COM Ports via COM servers from Digi, but I am simulating the COM ports with a tool called com0com. With that tool you can simulate COM Pairs that communicate with each other so you can communicate with your own System. https://com0com.sourceforge.net/ I hope this helps you and thanks for your help :) |
Do you want me to open a pull request with these changes or will you do this? |
Thanks for offering – I want to play with the |
I have more than 64 COM Ports configured in my Windows System. Windows 10 supports 4096 COM Ports.
After opening 63 Serialports with
SerialPort serialPort = (SerialPort) commPortIdentifier.open("NRSerialTest", 0);
my application just "waits" at the opening of the 64th. I suspect that only 63 SerialPorts can be opened at tha same time, but I do not get why.I tried looking in the code, but did not find any restrictions.
Here is a small test application I wrote:
` public static void main(String[] args) {
When I add the
serialPort.close();
line, the program finishes with all ports being opened once.Some Information abput my system:
Windows 10 22H2 Build 19045
Java 11.0.2
The text was updated successfully, but these errors were encountered: