java - Serial ports writing/reading in linux -


i need implement communication between 2 devices through serial ports on raspberry pi (with last raspbian on board). both uses cp2102 controller , connected pi.

data flow

terminal:

pi@pi ~ $ ls -l /dev/serial/by-id total 0 silicon_labs_cp2102_usb_to_uart_bridge_controller_0001-if00-port0 -> ../../ttyusb2  pi@pi ~ $ ls -l /dev/serial/by-path total 0 platform-bcm2708_usb-usb-0:1.2.1:1.0-port0 -> ../../ttyusb1 platform-bcm2708_usb-usb-0:1.2.4:1.0-port0 -> ../../ttyusb2 platform-bcm2708_usb-usb-0:1.3:1.0-port0 -> ../../ttyusb0 

normally when send command serial a, device replies through serial , sends data through serial b. after need retranslate data device b (serial c) , receive answer serial c.

the problem serial reply appears on serial b , serial b data appears on serial a.

i've tried different languages , serial libraries result same. questions are: why happening when using raspberry pi? how can implement functionality on raspberry pi?

p.s. both devices working properly. c# code works perfect. used system.io.ports.serialport class implementation , looks pi4j , rxtx solutions.

p.p.s. code tried use on rpi:

serial, c++: (very bad piece of code)

serial port("/dev/ttyusb2", 115200u); serial port1("/dev/ttyusb1", 115200u); port1.settimeout(timeout::max(), 250, 0, 250, 0); port.settimeout(timeout::max(), 250, 0, 250, 0);  cout << "is serial port open?"; if (port1.isopen()) {     cout << " yes." << endl;     uint8_t data[2] = { 0xaa, 0x00 };     port1.write(data, 2);     data[1] = 0xff;     sleep(1);     port1.write(data, 2);     while (port.available() < 7);     int av = port.available();     string ss;     port.read(ss, av);     (int = 0; < av; i++){         cout << (uint)ss.at(i) << " ";     }     cout << "av: " + (uint)av << endl; } 

rxtx, java:

    public class bridge_rxtx {         public static final int baudrate = 115200;          protected serialport spdevb_data;         serialreader devb_datalistener;          protected serialport spdeva_data;         serialreader deva_datalistener;          protected serialport spdeva_control;         serialporteventlistener deva_controllistener;          public bridge_rxtx(string comdevb_data, string comdeva_data, string comdeva_control) {             try {             spdevb_data = setupport(comdevb_data);             spdeva_data = setupport(comdeva_data);             spdeva_control = setupport(comdeva_control);             } catch (exception ignored){                 ignored.printstacktrace();             }              try {                 devb_datalistener = new serialreader(spdevb_data.getinputstream(), spdeva_data.getoutputstream(), "b-a");                 deva_datalistener = new serialreader(spdeva_data.getinputstream(), spdevb_data.getoutputstream(), "a-b");                 deva_controllistener = new serialporteventlistener() {                      @override                     public void serialevent(serialportevent spe) {                         throw new unsupportedoperationexception("not supported yet.");                     }                 };              spdevb_data.notifyondataavailable(true);             spdeva_data.notifyondataavailable(true);             spdeva_control.notifyondataavailable(true);              } catch (ioexception ex) {                 logger.getlogger(bridge_rxtx.class.getname()).log(level.severe, null, ex);             }          }          public void launchbridge(){             system.out.println("starting...");             try {                 spdeva_control.getoutputstream().write(new byte[] {(byte)0xaa, (byte) 0x00}, 0, 2);             } catch (ioexception ex) {                 logger.getlogger(bridge_rxtx.class.getname()).log(level.severe, null, ex);             }             try {                 thread.sleep(200);             } catch (interruptedexception ex) {                 logger.getlogger(bridge_rxtx.class.getname()).log(level.severe, null, ex);             }             try {                 spdeva_control.getoutputstream().write(new byte[] {(byte)0xaa, (byte) 0xff}, 0, 2);             } catch (ioexception ex) {                 logger.getlogger(bridge_rxtx.class.getname()).log(level.severe, null, ex);             }             system.out.println("started");         }          serialport setupport(string portname) throws exception {             serialport serialport = null;             commportidentifier portidentifier = commportidentifier.getportidentifier(portname);             if (portidentifier.iscurrentlyowned()) {                 system.out.println("error: port in use");             } else {                 commport commport = portidentifier.open(this.getclass().getname(), 2000);                 if (commport instanceof serialport) {                     serialport = (serialport) commport;                     serialport.setserialportparams(baudrate, serialport.databits_8, serialport.stopbits_1, serialport.parity_none);                 } else {                     system.out.println("error: serial ports handled code.");                 }             }             return serialport;         }          public static void main(string[] args) {             bridge_rxtx bridge = new bridge_rxtx("/dev/ttyusb0", "/dev/ttyusb2", "/dev/ttyusb1");             bridge.launchbridge();         }     } 

pi4j, java:

public class bridge {     public static bridge instance;      public static final int baudrate = 115200;      protected serial spdevb_data;     serialdatalistener devb_datalistener;      protected serial spdeva_data;     serialdatalistener deva_datalistener;      protected serial spdeva_control;     serialdatalistener deva_controllistener;      private bridge() {      }     public bridge(string comdevb_data, string comdeva_data, string comdeva_control) {         instance = this;          deva_controllistener = new serialdatalistener() {         //serialdataevent in pi4j doesn't support binary         //data default. implemented myself.             public void datareceived(serialdataevent event) {                 system.out.println(bytestohex(toprimitives(event.getbinarydata())));             }          };         devb_datalistener = new serialdatalistener() {             public void datareceived(serialdataevent event) {                 byte[] data = toprimitives(event.getbinarydata());                 instance.spdeva_data.write(data);                 system.out.println("b -> a: " + bytestohex(data));             }          };         deva_datalistener = new serialdatalistener() {             public void datareceived(serialdataevent event) {                 byte[] data = toprimitives(event.getbinarydata());                 instance.spdevb_data.write(data);                 try {                     thread.sleep(15);                 } catch (interruptedexception ex) {                     logger.getlogger(bridge.class.getname()).log(level.severe, null, ex);                 }                 system.out.println("b <- a: " + bytestohex(data));             }          };          spdevb_data = serialfactory.createinstance();         spdevb_data.addlistener(devb_datalistener);          spdeva_data = serialfactory.createinstance();         spdeva_data.addlistener(deva_controllistener);          spdeva_control = serialfactory.createinstance();         spdeva_control.addlistener(deva_datalistener);           spdevb_data.setmonitorinterval(40);         spdeva_data.setmonitorinterval(80);         spdeva_control.setmonitorinterval(25);          spdevb_data.open(comdevb_data, baudrate);          spdeva_data.open(comdeva_data, baudrate);         spdeva_control.open(comdeva_control, baudrate);      }      public void setupbridge() {         spdeva_control.write(new byte[]{(byte) 0xaa, (byte) 0x00});         try {             thread.sleep(20);         } catch (interruptedexception ex) {             logger.getlogger(bridge.class.getname()).log(level.severe, null, ex);         }         spdeva_control.write(new byte[]{(byte) 0xaa, (byte) 0xff});     } } 

not enough reputation comment: educated guess has how linux enumerates hardware. think it's possible usb devices have serial-path depending on used distro, , order of connecting them.

are sure use same usb port every time? , correct usb port mapped /dev/tty?

you can force hid have same name making sure have udev rule it. info here


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -