Skip to content

Commit 8761a40

Browse files
Merge pull request #179 from riedlse/addConstructors
added new constructors
2 parents 84e1f67 + 4e1dd0b commit 8761a40

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

src/main/java/gnu/io/NRSerialPort.java

+64-1
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,52 @@ public class NRSerialPort
7676
private String port = null;
7777
private boolean connected = false;
7878
private int baud = 115200;
79+
private int parity = SerialPort.PARITY_NONE;
80+
private int dataBits = SerialPort.DATABITS_8;
81+
private int stopBits = SerialPort.STOPBITS_1;
7982

8083
/**
8184
* Class Constructor for a NRSerialPort with a given port and baudrate.
8285
*
8386
* @param port the port to connect to (i.e. COM6 or /dev/ttyUSB0)
8487
* @param baud the baudrate to use (i.e. 9600 or 115200)
8588
*/
89+
90+
public NRSerialPort(String port)
91+
{
92+
setPort(port);
93+
}
94+
8695
public NRSerialPort(String port, int baud)
8796
{
8897
setPort(port);
8998
setBaud(baud);
9099
}
91100

101+
public NRSerialPort(String port, int baud, int parity)
102+
{
103+
setPort(port);
104+
setBaud(baud);
105+
setParity(parity);
106+
}
107+
108+
public NRSerialPort(String port, int baud, int parity, int dataBits)
109+
{
110+
setPort(port);
111+
setBaud(baud);
112+
setParity(parity);
113+
setDataBits(dataBits);
114+
}
115+
116+
public NRSerialPort(String port, int baud, int parity, int dataBits, int stopBits)
117+
{
118+
setPort(port);
119+
setBaud(baud);
120+
setParity(parity);
121+
setDataBits(dataBits);
122+
setStopBits(stopBits);
123+
}
124+
92125
public boolean connect()
93126
{
94127
if (isConnected())
@@ -103,7 +136,7 @@ public boolean connect()
103136
serial = new RFC2217PortCreator().createPort(port);
104137
else
105138
serial = new RxTxPortCreator().createPort(port);
106-
serial.setSerialPortParams(getBaud(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
139+
serial.setSerialPortParams(getBaud(), getDataBits(), getStopBits(), getParity());
107140
setConnected(true);
108141
}
109142
catch (NativeResourceException e)
@@ -236,6 +269,36 @@ public int getBaud()
236269
return baud;
237270
}
238271

272+
public void setParity(int parity)
273+
{
274+
this.parity = parity;
275+
}
276+
277+
public int getParity()
278+
{
279+
return this.parity;
280+
}
281+
282+
public void setStopBits(int stopBits)
283+
{
284+
this.stopBits = stopBits;
285+
}
286+
287+
public int getStopBits()
288+
{
289+
return this.stopBits;
290+
}
291+
292+
public void setDataBits(int dataBits)
293+
{
294+
this.dataBits = dataBits;
295+
}
296+
297+
public int getDataBits()
298+
{
299+
return this.dataBits;
300+
}
301+
239302
/**
240303
* Enables RS485 half-duplex bus communication for Linux. The Linux kernel uses the RTS pin as bus enable. If you use a device that is configured via the Linux
241304
* device tree, take care to add "uart-has-rtscts" and to configure the RTS GPIO correctly.

0 commit comments

Comments
 (0)