nyholku / purejavacomm

Pure Java implementation of JavaComm SerialPort
http://www.sparetimelabs.com/purejavacomm/index.html
BSD 3-Clause "New" or "Revised" License
363 stars 146 forks source link

[Ubuntu VM] All Serial Ports currently in use. #77

Closed BGR360 closed 8 years ago

BGR360 commented 8 years ago

I have set up a very simple Serial Reading program that is supposed to take in a serial port as its first argument. If you pass in an invalid one, it spits out a list of all the available serial ports. However, every port on this list, when I try to pass any of them into my program, reports a PortInUse exception. Is this an issue with being in a virtual machine (VMWare)?

nyholku commented 8 years ago

That is a likely the problem.

To check single step/trace into the CommPortIdentifier.open(String appname, int timeout) and see where the exception is thrown. Most likely it is thrown inside PureJavaSerialPort(String name, int timeout) , if you look at the line 1083 in PureJavaSerialPort:

    while ((m_FD = open(name, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {

you see that this happens in a very simple direct call to the OS for the given port name, not much that can go wrong inside PJC so the problem is at OS level. The OS won't open the port for one reason or another.

Could also be an access right issue. Again this is an OS issue.

BGR360 commented 8 years ago

Thank you for your response. I am going to test it on a booted Ubuntu computer next week, I will let you know the results.

BGR360 commented 8 years ago

Hi @nyholku, So I tested the code on a real Ubuntu machine, and still the same error occurs. I get a list of 32 tty ports (ttyS0 - ttyS31) that are "available," but every single one is "in use." Do you have any advice?

nyholku commented 8 years ago

Pretty much what I said above, most likely an OS issue, most likely permissions. "In use" basically just means that the the port could not be opened (there is no separate exception for other errors because the original JavaComm spec did not have that).

Can you open any of the port in any other program, say with 'screen' or something?

dlandoll commented 8 years ago

I had a similar issue in Windows when trying to use a virtual serial port for testing my application. It was unable to open any virtual serial ports due to the parameter error shown below. I was able to use Putty to open the serial ports and when I switched to a real USB serial port PureJavaComm worked also.

og: 715434,> open('COM1',00020006) log: 715434,> CreateFileA(.\COM1, 0xC0000000, 0x00000000, null, 0x00000003, 0x40000000,null) log: 715434,< CreateFileA(.\COM1, 0xC0000000, 0x00000000, null, 0x00000003, 0x40000000,null) => native@0x5f0 (jtermios.windows.WinAPI$HANDLE@5f0) log: 715434,> SetCommTimeouts(native@0x5f0 (jtermios.windows.WinAPI$HANDLE@5f0), 2048, 2048) log: 715434,< SetCommTimeouts(native@0x5f0 (jtermios.windows.WinAPI$HANDLE@5f0), 2048, 2048) => true log: 715434,> GetCommState(native@0x5f0 (jtermios.windows.WinAPI$HANDLE@5f0), [BaudRate 0 fFlags 0000 wReserved 0 XonLim 0 XoffLim 0 ByteSize 0 Parity 0 StopBits 0 XonChar 00 XoffChar 00 ErrorChar 00 EofChar 00 EvtChar 00 wReserved1 0]) log: 715434,< GetCommState(native@0x5f0 (jtermios.windows.WinAPI$HANDLE@5f0), [BaudRate 9600 fFlags 1011 wReserved 0 XonLim 2048 XoffLim 1024 ByteSize 8 Parity 0 StopBits 0 XonChar 11 XoffChar 13 ErrorChar 00 EofChar 00 EvtChar 00 wReserved1 0]) => true log: 715434,> SetCommState(native@0x5f0 (jtermios.windows.WinAPI$HANDLE@5f0), [BaudRate 9600 fFlags 0001 wReserved 0 XonLim 2048 XoffLim 1024 ByteSize 8 Parity 0 StopBits 0 XonChar 00 XoffChar 00 ErrorChar 00 EofChar 00 EvtChar 0A wReserved1 0]) log: 715434,< SetCommState(native@0x5f0 (jtermios.windows.WinAPI$HANDLE@5f0), [BaudRate 9600 fFlags 0001 wReserved 0 XonLim 2048 XoffLim 1024 ByteSize 8 Parity 0 StopBits 0 XonChar 00 XoffChar 00 ErrorChar 00 EofChar 00 EvtChar 0A wReserved1 0]) => false log: 715434,> GetLastError() log: 715434,< GetLastError() => 87 log: 715434,> FormatMessageW(00001200, null, 87, 1024, allocated@0x58676710 (2048 bytes), 2048, null) log: 715434,< FormatMessageW(00001200, null, 87, 1024, allocated@0x58676710 (2048 bytes), 2048, null) => 29 log: 715449,fail() class jtermios.windows.JTermiosImpl line 723, Windows GetLastError()= 87, The parameter is incorrect.

nyholku commented 8 years ago

Hmm ... makes me think: if we allowed PureJavaComm to ignore SetCommState errors while opening a port, perhaps the port would still be usable (of course this goes against JavaComm spec which says IIRC that the port needs to default to 9600,8,1). This might be useful... if anyone has this situation (SetCommState fails with GetLastError == 87) I would be happy receive reports on testing this idea.