3

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!

Community
  • 1
  • 1
Amanuel Nega
  • 1,899
  • 1
  • 24
  • 42
  • See if [this](http://stackoverflow.com/questions/21799178/where-to-download-java-communications-api) if of any help. – Ravindra HV Dec 30 '16 at 19:31

1 Answers1

4

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.

TadejP
  • 912
  • 11
  • 21
  • I've added what I want my code to look like.... I wanted to use `File` read write to access my ports. much like how I can access the device files on Linux. – Amanuel Nega Dec 27 '16 at 06:01
  • It is no simpler way as use {Input,Output}stream and Threads. Serial ports are not regular files with "size of bytes" known. It changes and we should implement hooks to catch when data is ready. – TadejP Dec 27 '16 at 18:01
  • In short u mean you can't access them as a regular file in windows as you can in java? – Amanuel Nega Dec 28 '16 at 05:45
  • No in short I am affraid. – TadejP Dec 29 '16 at 07:11