Closed Kosmachevskiy closed 7 years ago
Sorry, it should be a message to other library.
Hi , you have to download modbus4j source and reference your project if you want to solve serial library problems in linux & windows. Then ,
i am kind of busy right now , so i can't write too much. you have to figure out , it's easy.
import java.io.InputStream; import java.io.OutputStream; import java.io.File; import java.io.IOException;
import com.fazecast.jSerialComm.SerialPort;
public static SerialPort serialLocalPort;
serialLocalPort= SerialPort.getCommPort("/dev/tty0");
serialLocalPort.setBaudRate(115200);
serialLocalPort.setParity( 0);
serialLocalPort.setNumDataBits(8);
serialLocalPort.setNumStopBits(1);
SerialPortWrapper spw = new SerialPortWrapper()
{
@Override
public void open() throws Exception
{
// TODO Auto-generated method stub
if (serialLocalPort.openPort())
{
}
}
@Override
public int getStopBits()
{
return serialLocalPort.getNumStopBits();
}
@Override
public int getParity()
{
return serialLocalPort.getParity();
}
@Override
public OutputStream getOutputStream()
{
return serialLocalPort.getOutputStream();
}
@Override
public InputStream getInputStream()
{
return serialLocalPort.getInputStream();
}
@Override
public int getFlowControlOut()
{
return 0;
}
@Override
public int getFlowControlIn()
{
return 0;
}
@Override
public int getDataBits()
{
return serialLocalPort.getNumDataBits();
}
@Override
public int getBaudRate()
{
return serialLocalPort.getBaudRate();
}
@Override
public void close() throws Exception
{
if (serialLocalPort.closePort())
{
}
}
};
master = factory.createRtuMaster(spw);
}
@cevatbostancioglu Thanks a lot! I am going to use this library.
@cevatbostancioglu I tried to run your code - it is works. Thanks a lot!
your welcome.
Hi!
I have some trouble with ModbusSerialMaster. I got a NPE during reading registers.
My code:
`
` And stack:
Exception in thread "main" java.lang.NullPointerException at com.ghgande.j2mod.modbus.net.SerialConnection.setComPortTimeouts(SerialConnection.java:221) at com.ghgande.j2mod.modbus.io.ModbusSerialTransport.setTimeout(ModbusSerialTransport.java:142) at com.ghgande.j2mod.modbus.io.ModbusSerialTransport.open(ModbusSerialTransport.java:131) at com.ghgande.j2mod.modbus.io.ModbusSerialTransport.writeMessage(ModbusSerialTransport.java:73) at com.ghgande.j2mod.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:143) at com.ghgande.j2mod.modbus.facade.AbstractModbusMaster.readMultipleRegisters(AbstractModbusMaster.java:236)
Library version: 2.3.1