Amanuel Nega
Amanuel Nega

Reputation: 1977

Specify COM port file in java

In Unix like operating systems, we can access serial ports through files such as /dev/ttyUSB0 or something. And according to this question, filenames such as COM1: can be used to access the serial ports. What is the java alternative for such file names? I don't want to use Serial Communication liberaries.

Edit

What I want my code to look like is this.

String INPUT_PORT_FILE_NAME = linux?"/dev/ttyUSB0":"<File name of comport>"

File in = new File(INPUT_PORT_FILE_NAME)

What I want is the widows alternative to a device file.

EDIT I am on a linux machine, and I want to enable my code to be ported easily!

Upvotes: 3

Views: 700

Answers (1)

TadejP
TadejP

Reputation: 1042

Yes, on Linux there is access to serial port for instance through device files /dev/ttyS0, /dev/ttyUSB0 and others. It really depends on hardware/chips used to communicate and even distributions.

If same hardware is used in your program it can be partly achieved. When I worked with Serial Comm libraries and real physical serial ports in Linux I used port numbers in config so number 3 meant n=3, so opens "COM"+(1+n) on windows or "/dev/ttyS"+n on linux. Maybe similar can be used for you to accessing port on /dev/ttyUSB"+n

But there is no grantee that port 2 will be /dev/ttyS1 and COM2 on the same computer after dual boot.

The way not using Serial Comm libraries is hard way and do not recommend it if you want portability in java. I recommend different port config depending on operating system.

Upvotes: 4

Related Questions