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

Error 87 on Windows #122

Open
innot opened this issue Feb 5, 2019 · 2 comments
Open

Error 87 on Windows #122

innot opened this issue Feb 5, 2019 · 2 comments

Comments

@innot
Copy link

innot commented Feb 5, 2019

This issue is probably related to issue #104 PureJavaSerialPort initializing fails for STM32 Virtual USB COM Port Driver

I have been experimenting with PureJavaCom on Windows 10 using virtual Com Ports, but it would not work for me. I have been using the SimpleWrite.java example from Sun

Debugging showed, that the problem was in the WinAPI.java class in the
static public boolean SetCommState(HANDLE hFile, DCB dcb)
method at line 779:
res = m_K32lib.SetCommState(hFile, dcb);

This always threw a LastErrorException with Code 87 Invalid Parameter.

This was caused by the DCB fields XonChar and XoffChar both being 0x00. But the Windows API documentation for the SetCommState Function states:

The SetCommState function fails if the XonChar member of the DCB structure is equal to the XoffChar member.

A quick fix for this issue is to set the XonChar and XoffChar fields in the DCB class:

diff --git a/src/jtermios/windows/WinAPI.java b/src/jtermios/windows/WinAPI.java
index 101aabc..1bd9007 100644
--- a/src/jtermios/windows/WinAPI.java
+++ b/src/jtermios/windows/WinAPI.java
@@ -505,6 +505,6 @@
 		public byte Parity;
 		public byte StopBits;
-		public byte XonChar;
-		public byte XoffChar;
+		public byte XonChar = 0x11;
+		public byte XoffChar = 0x13;
 		public byte ErrorChar;
 		public byte EofChar;

The bug probably depends on how purejavacomm is used, i.e. in which order the purejavacomm functions are called. There seem to be multiple execution paths to the SetCommState() method, and at least the one thru CommPortIdentifier.getPortIdentifier(String) does not instantiate the PureJavaSerialPort() class where XonChar and XoffChar would be initialized.
But I do not have the expertise (and time) to really check this. My small hack should work okay and should not have any side effects.

Testing my quick fix I found another small issue in the jtermios.windows.testsuite.TestSuite.java class which causes the same Error 87 (for the same reason) - a typo where XonChar is set twice. The following patch fixes this and the test now works for me

diff --git a/src/jtermios/windows/testsuite/TestSuite.java b/src/jtermios/windows/testsuite/TestSuite.java
old mode 100755
new mode 100644
index 76e1c1c..14f2787
--- a/src/jtermios/windows/testsuite/TestSuite.java
+++ b/src/jtermios/windows/testsuite/TestSuite.java
@@ -87,5 +87,5 @@
 		dcb.XonChar = 0x11;
 		dcb.StopBits = ONESTOPBIT;
-		dcb.XonChar = 0x13;
+		dcb.XoffChar = 0x13;
 
 		check(SetCommState(hComm, dcb), "SetCommState ");


Best regards,

Thomas

@innot
Copy link
Author

innot commented Feb 5, 2019

My little hack works for my simple test app and also for the jtermios.windows.testsuite.TestSuite.java tests, but still fails with the same problem running the full purejavacomm.testsuite. The DCB.XonChar and XoffChar get overwritten with 0x00 on a different path to SetCommState().

Back to the drawing board.

@innot
Copy link
Author

innot commented Feb 5, 2019

New hack to fix the issue:

diff --git a/src/jtermios/windows/JTermiosImpl.java b/src/jtermios/windows/JTermiosImpl.java
index 28fe18c..a57d0f2 100644
--- a/src/jtermios/windows/JTermiosImpl.java
+++ b/src/jtermios/windows/JTermiosImpl.java
@@ -155,6 +155,8 @@
 						cfsetospeed(m_Termios, B9600);
 						m_Termios.c_cc[VTIME] = 0;
 						m_Termios.c_cc[VMIN] = 0;
+						m_Termios.c_cc[VSTART] = 0x11;
+						m_Termios.c_cc[VSTOP] = 0x13;
 						updateFromTermios(this);
 
 						WaitCommEventCancelObject = CreateEvent(null, false, false, null);

As this is in the open() Method I would guess that all execution paths should run through this method.

With this fix the purejavacomm TestSuite runs almost completly, just Test9 is failing but I guess that this is a different issue (Windows Error 995 ERROR_OPERATION_ABORTED in the select() method)

innot added a commit to innot/purejavacomm that referenced this issue Mar 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant