简体   繁体   English

在java中指定COM端口文件

[英]Specify COM port file in java

In Unix like operating systems, we can access serial ports through files such as /dev/ttyUSB0 or something. 在类Unix操作系统中,我们可以通过/dev/ttyUSB0等文件访问串口。 And according to this question , filenames such as COM1: can be used to access the serial ports. 根据这个问题COM1:等文件名可用于访问串口。 What is the java alternative for such file names? 这种文件名的java替代品是什么? I don't want to use Serial Communication liberaries. 我不想使用Serial Communication库。

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! 编辑我在linux机器上,我想让我的代码轻松移植!

Yes, on Linux there is access to serial port for instance through device files /dev/ttyS0 , /dev/ttyUSB0 and others. 是的,在Linux上可以访问串口,例如通过设备文件/dev/ttyS0/dev/ttyUSB0等。 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. 当我在Linux中使用串行通信库和真实物理串行端口时,我在配置中使用端口号,因此数字3表示n = 3,因此在Windows上打开“COM”+(1 + n)或“/ dev / ttyS”+ n在linux上。 Maybe similar can be used for you to accessing port on /dev/ttyUSB"+n 也许类似的可以用来访问/ dev / ttyUSB上的端口“+ n

But there is no grantee that port 2 will be /dev/ttyS1 and COM2 on the same computer after dual boot. 但是在双启动后,没有受让人认为端口2将是/dev/ttyS1和COM2在同一台计算机上。

The way not using Serial Comm libraries is hard way and do not recommend it if you want portability in java. 不使用串行通信库的方式很难,如果你想在java中实现可移植性,不建议使用它。 I recommend different port config depending on operating system. 我建议不同的端口配置取决于操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM