0

I'm working on creating a emulator in Java, the emulator is to mimic Captron button which connects to a serial port: /dev/ttyS0, another micro-service is listening to this port and detect any byte array that writes to the port, for example, when the button is pressed, there will be a byte array: [04 20 00 27 02 00 00 00 06 04 01 F9 2E 05] written to the serial port and the micro service will detect the byte array message and interpret it to something meaningful.

In order to mimic the behavior of the button, I was trying to manually echo a byte array to the serial port by: echo "[04 20 00 27 02 00 00 00 06 04 01 F9 2E 05]" >> /dev/ttyS0

But nothing is detected by the micro-service, does anyone know if it is because I can't echo a byte array to a serial port like this? What is the proper way to write a byte array to serial port?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Yamazaki
  • 3
  • 2
  • 1
    I don’t think your `echo` statement is doing what you think it’s doing. You probably want to send the byte value 04, then the byte value 20, then the byte value 00, etc., to the device. Your `echo` statement is sending the ASCII value for `[` (that is, 5b), then the ASCII value for `0` (that is, 30), then the ASCII value for `4` (that is, 34), and so on. (What does this have to do with Java?) – VGR Feb 05 '22 at 23:03
  • These articles may be helpful. [Send binary file to ttyUSB0 serially in linux](https://unix.stackexchange.com/q/561173), [slow file transfer through serial port with cat](https://unix.stackexchange.com/q/291084), [How can i send a binary file via serial in linux?](https://stackoverflow.com/q/38576504/9014308), [How to write binary data into a serial port (RS232) device file in linux?](https://stackoverflow.com/q/21511761/9014308), [serial port in linux (raspbian)](https://stackoverflow.com/q/28298669/9014308) – kunif Feb 05 '22 at 23:24
  • Why have you tagged this Java? – tgdavies Feb 06 '22 at 00:17

0 Answers0