0

Can I read from and write to a serial port or device using Java on a Mac?

I know the filename of the device (in the /dev directory). I have a small Python program that can read and write that device. In Java I'm trying to read it with a FileInputStream or a FileReader. I get some response, usually a single byte of 0 or FF, and then nothing else.

Perhaps I need to set the baud rate and other serial port parameters. The Python program does this but I don't know how to do it in Java.

I've read that there used to be a "javax.comm" library, but it's no longer available, and probably wouldn't work on a Mac anyway.

Thanks, Frank

Flarosa
  • 1,287
  • 1
  • 13
  • 27
  • Short answer...Yes. Research [Serial Port Communications with Java](https://www.google.ca/search?q=Serial+Port+Communication+with+Java&spell=1&sa=X&ved=2ahUKEwiC1v7pw-36AhW2HjQIHYIIAncQBSgAegQIBxAB&biw=1440&bih=735&dpr=1) – DevilsHnd - 退職した Oct 20 '22 at 00:16
  • Every one of these solutions is just telling me to install some library. This is not what I want. I want to do this in plain Java. Java supports reading files, and the Mac file system presents devices as files. It should be as easy as reading any other file. – Flarosa Oct 20 '22 at 00:18
  • What about the links in https://stackoverflow.com/questions/900950/how-to-send-data-to-com-port-using-java ? – Old Dog Programmer Oct 20 '22 at 00:32

1 Answers1

1

Yes Unix presents Serial Ports as files, but you still need to configure the baud rate, start bit, stop bits, parity,.. so you cannot just open the file.

Unfortunately there is no native Java functionality to open serial ports, but there are some librarys that do that for you.

Examples are:

there are propably more, but these are the ones i have used

You might need to research which one fits your usecase the best.

B.Letz
  • 136
  • 1
  • 16