RishiGupta12 / SerialPundit

Serial port communication in Java - FTDI D2XX, HID API, X/Y modem
GNU Affero General Public License v3.0
125 stars 56 forks source link

javax.comm Implementation #39

Closed Bodyash closed 4 years ago

Bodyash commented 4 years ago

Hi, I'm trying to wrap SerialPundit into Javax.comm API, cause javax.comm works only on linx86 or solaris

I need it to be able to connect to serial devices running on any OS and x86 or x64 Java, because a lot of legacy code used javax.comm API.

Currently i cannot test it, but i have a question, in legacy code i have something like:

serialPort.addEventListener(myListener);
serialPort.notifyOnDataAvailable(true);

and in MyListener ->

public void serialEvent(SerialPortEvent arg0) {
    if(arg0.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        while(serialPort.getInputStream().available() > 0 ) {
            byte b = (byte)serialPort.getInputStream().read();
                //SomeCode
                }
               //SomeCode
       }
}

In my implementation of javax.comm.SerialPort i have:

public void addEventListener(SerialPortEventListener spel)  {

        //TODO Handle EVENTS SerialPortEventListener wrap to ISerialComDataListener or ISerialComEventListener
            scm.registerDataListener(handle, new ISerialComDataListener() {
                @Override
                public void onNewSerialDataAvailable(byte[] data) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onDataListenerError(int errorNum) {
                    // TODO Auto-generated method stub
                }
            });
    } 

And now i see some problem. Cause in javax.comm API code i'm recieving event that data is available and then reading bytes from input stream.

But in your code i see onNewSerialDataAvailable(byte[] data), so that means that data is already readed, it's in data variable, so serialPort.getInputStream().read() will not work? Or Im wrong?

RishiGupta12 commented 4 years ago

You are correct, data has been already read. On 08-Aug-2019 4:28 PM, "Bogdan" notifications@github.com wrote:

Hi, I'm trying to wrap SerialPundit into Javax.comm API, cause javax.comm works only on linx86 or solaris

I need it to be able to connect to serial devices running on any OS and x86 or x64 Java, because a lot of legacy code used javax.comm API.

Currently i cannot test it, but i have a question, in legacy code i have something like:

serialPort.addEventListener(myListener); serialPort.notifyOnDataAvailable(true);

and in MyListener ->

public void serialEvent(SerialPortEvent arg0) { if(arg0.getEventType() == SerialPortEvent.DATA_AVAILABLE) { while(serialPort.getInputStream().available() > 0 ) { byte b = (byte)serialPort.getInputStream().read(); //SomeCode } //SomeCode } }

In my implementation of javax.comm.SerialPort i have:

public void addEventListener(SerialPortEventListener spel) {

  //TODO Handle EVENTS SerialPortEventListener wrap to ISerialComDataListener or ISerialComEventListener
      scm.registerDataListener(handle, new ISerialComDataListener() {
          @Override
          public void onNewSerialDataAvailable(byte[] data) {
              // TODO Auto-generated method stub
          }

          @Override
          public void onDataListenerError(int errorNum) {
              // TODO Auto-generated method stub
          }
      });

}

And now i see some problem. Cause in javax.comm API code i'm recieving event that data is available and then reading bytes from input stream.

But in your code i see onNewSerialDataAvailable(byte[] data), so that means that data is already readed, it's in data variable, so serialPort.getInputStream().read() will not work? Or Im wrong?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/RishiGupta12/SerialPundit/issues/39?email_source=notifications&email_token=ACJVFLGILZ3BCOZISA2WNG3QDP36BA5CNFSM4IKJB2Y2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HEDY5WA, or mute the thread https://github.com/notifications/unsubscribe-auth/ACJVFLDPHYDGKKA6WNLFA33QDP36BANCNFSM4IKJB2YQ .

Bodyash commented 4 years ago

Hmmm, okay, but there is another way:

    public void addEventListener(SerialPortEventListener spel) throws TooManyListenersException {
        //TODO: HANDLE more events from lineEvent.getXXX();
        ISerialComEventListener listener = new ISerialComEventListener() {
            SerialPortEventListener javaxListener = spel;
            @Override
            public void onNewSerialEvent(SerialComLineEvent lineEvent) {
                if( lineEvent.getDSR() == 1) {
                    javaxListener.serialEvent(new SerialPortEvent(thisPort, SerialPortEvent.DATA_AVAILABLE , false, true));
                }
            }
        };
        try {
            scm.registerLineEventListener(handle, listener);
        } catch (SerialComException e) {
            e.printStackTrace();
        }
    }

Listening to line events, "understand them" and send it like javax.comm events. I Think it will work. Or there is another solution - copying data to local input\output stream

Thx for reply!

RishiGupta12 commented 4 years ago

Data and line events are two different things. Keep them separate.

RishiGupta12 commented 4 years ago

If your question has been answered please close the ticket.