package com.intelligt.modbus.jlibmodbus.serial;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/*
 * Copyright (C) 2017 Vladislav Y. Kochedykov
 *
 * [http://jlibmodbus.sourceforge.net]
 *
 * This file is part of JLibModbus.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authors: Vladislav Y. Kochedykov, software engineer.
 * email: vladislav.kochedykov@gmail.com
 */

public class SerialPortJSerialComm extends SerialPort {

    private com.fazecast.jSerialComm.SerialPort port;
    private InputStream in;
    private OutputStream out;

    public SerialPortJSerialComm(SerialParameters sp) {
        super(sp);
    }

    @Override
    public void write(int b) throws IOException {
        if (!isOpened()) {
            throw new IOException("Port not opened");
        }
        try {
            out.write((byte) b);
        } catch (Exception e) {
            throw new IOException(e);
        }
    }

    @Override
    public void write(byte[] bytes) throws IOException {
        if (!isOpened()) {
            throw new IOException("Port not opened");
        }
        try {
            out.write(bytes);
        } catch (Exception e) {
            throw new IOException(e);
        }
    }

    @Override
    public void open() throws SerialPortException {
        SerialParameters sp = getSerialParameters();
        port = com.fazecast.jSerialComm.SerialPort.getCommPort(sp.getDevice());
        port.setComPortParameters(sp.getBaudRate(), sp.getDataBits(), sp.getStopBits(), sp.getParity().getValue());
        port.setFlowControl(com.fazecast.jSerialComm.SerialPort.FLOW_CONTROL_DISABLED);

        in = port.getInputStream();
        out = port.getOutputStream();

        port.setComPortTimeouts(com.fazecast.jSerialComm.SerialPort.TIMEOUT_READ_BLOCKING, getReadTimeout(), 0);
    }

    @Override
    public void setReadTimeout(int readTimeout) {
        super.setReadTimeout(readTimeout);

        if (isOpened())
            port.setComPortTimeouts(com.fazecast.jSerialComm.SerialPort.TIMEOUT_NONBLOCKING, getReadTimeout(), getReadTimeout());
    }

    @Override
    public int read() throws IOException {
        if (!isOpened()) {
            throw new IOException("Port not opened");
        }
        int c;
        try {
            c = in.read();
        } catch (Exception e) {
            throw new IOException(e);
        }
        if (c > -1)
            return c;
        else
            throw new IOException("Read timeout");
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (!isOpened()) {
            throw new IOException("Port not opened");
        }
        int c;
        try {
            c = in.read(b, off, len);
        } catch (Exception e) {
            throw new IOException(e);
        }
        if (c > -1)
            return c;
        else
            throw new IOException("Read timeout");
    }

    @Override
    public void close() {
        try {
            if (isOpened()) {
                in.close();
                out.close();
                port.closePort();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isOpened() {
        return port.isOpen();
    }
}

@@ -1086,16 +1086,51 @@
 eam out;
+%0A    final byte %5B%5D b = new byte%5B1%5D;
 %0A%0A    pu
@@ -1982,24 +1982,50 @@
 tDevice());%0A
+        port.openPort();%0A%0A
         port
@@ -2405,16 +2405,17 @@
 (), 0);%0A
+%0A
     %7D%0A%0A 
@@ -2544,16 +2544,18 @@
 pened())
+ %7B
 %0A       
@@ -2673,24 +2673,34 @@
 Timeout());%0A
+        %7D%0A
     %7D%0A%0A    @
@@ -2849,32 +2849,33 @@
 %0A        int c;%0A
+%0A
         try %7B%0A  
@@ -2896,16 +2896,30 @@
 in.read(
+b, 0, b.length
 );%0A     
@@ -3001,34 +3001,33 @@
         if (c %3E 
--1
+0
 )%0A            re
@@ -3023,33 +3023,36 @@
          return 
-c
+b%5B0%5D
 ;%0A        else%0A 

Fix #2 Error in JSerialComm implementation.