Open ghost opened 4 years ago
First invoke configureComPortData and then configureComPortControl.
scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_EVEN, BAUDRATE.B9600, 0); scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', true, false);
Hi @RishiGupta12 . Thanks for prompt response.
Here is the code that i tested on Windows 10 x64 :
public static void main(String[] args) {
String PORT = null;
long handle = -1;
long context;
byte[] dataRead = new byte[1];
SerialComManager scm;
SerialComPlatform scp = new SerialComPlatform(new SerialComSystemProperty());
// get serial communication manager instance
try {
scm = new SerialComManager();
} catch (Exception e) {
e.printStackTrace();
return;
}
PORT = "COM1";
try {
handle = scm.openComPort(PORT, true, true, true);
scm.configureComPortData(handle, DATABITS.DB_7, STOPBITS.SB_1, PARITY.P_MARK, BAUDRATE.B9600, 0);
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', true, false);
System.out.println(Arrays.toString(scm.getCurrentConfiguration(handle)));
context = scm.createBlockingIOContext();
SerialComLineErrors lineErrors = null;
for (int x = 0; x < 5; x++) {
scm.readBytes(handle, dataRead, 0, 1, context, lineErrors);
if (dataRead != null) {
System.out.println("Data read : " + dataRead[0]);
}
if (!Objects.isNull(lineErrors)) {
System.out.println("Error >> " + lineErrors.hasAnyErrorOccurred() + " Parity Error >> "
+ lineErrors.hasParityErrorOccurred());
}
}
scm.unblockBlockingIOOperation(context);
scm.destroyBlockingIOContext(context);
scm.closeComPort(handle);
} catch (SerialComException e) {
if (handle == -1) {
try {
scm.closeComPort(handle);
} catch (SerialComException e1) {
}
}
e.printStackTrace();
}
}
`
Output:
[DCBlength : 28, BaudRate : 9600, fBinary : TRUE, fParity : FALSE, fOutxCtsFlow : FALSE, fOutxDsrFlow : FALSE, fDtrControl : DTR_CONTROL_ENABLE, fDsrSensitivity : FALSE, fTXContinueOnXoff : TRUE, fOutX : FALSE, fInX : FALSE, fErrorChar : FALSE, fNull : FALSE, fRtsControl : RTS_CONTROL_ENABLE, fAbortOnError : FALSE, fDummy2 : NA, wReserved : NA, XonLim : 2048, XoffLim : 2048, ByteSize : 8, Parity : 3, StopBits : 0, XonChar : , XoffChar : , ErrorChar : , EofChar : , EvtChar : , wReserved1 : NA]
fParity : FALSE
I tested this on two different Windows 10 x64 machines the output is the same, parity is not enabled.
I also tested parity check functionality on Ubuntu 19 :
public static void main(String[] args) {
String PORT = null;
long handle = -1;
long context;
byte[] dataRead = new byte[1];
SerialComManager scm;
SerialComPlatform scp = new SerialComPlatform(new SerialComSystemProperty());
// get serial communication manager instance
try {
scm = new SerialComManager();
} catch (Exception e) {
e.printStackTrace();
return;
}
PORT = "/dev/ttyS0";
try {
handle = scm.openComPort(PORT, true, true, true);
scm.configureComPortData(handle, DATABITS.DB_7, STOPBITS.SB_1, PARITY.P_MARK, BAUDRATE.B9600, 0);
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', true, false);
System.out.println(Arrays.toString(scm.getCurrentConfiguration(handle)));
context = scm.createBlockingIOContext();
SerialComLineErrors lineErrors = null;
for (int x = 0; x < 5; x++) {
scm.readBytes(handle, dataRead, 0, 1, context, lineErrors);
if (dataRead != null) {
System.out.println("Data read : " + dataRead[0]);
}
if (!Objects.isNull(lineErrors)) {
System.out.println("Error >> " + lineErrors.hasAnyErrorOccurred() + " Parity Error >> "
+ lineErrors.hasParityErrorOccurred());
}
}
scm.unblockBlockingIOOperation(context);
scm.destroyBlockingIOContext(context);
scm.closeComPort(handle);
} catch (SerialComException e) {
if (handle == -1) {
try {
scm.closeComPort(handle);
} catch (SerialComException e1) {
}
}
e.printStackTrace();
}
}
Output:
[0, 8216, 0, 1073745853, 0, 0, 3, 28, 127, 21, 4, 1, 0, 0, 17, 19, 26, 0, 18, 15, 23, 22, 0, 9600, 9600]
Data read: -1 Data read: 0 Data read: 127
SerilComLineErrors class didn't picked up parity error. The POSIX did mark the parity error that is why the app received -1 and 0 before the byte value 127.
SerilComLineErrors mechanism does not work on Ubuntu 19.
I'm working on implementation of full duplex host that uses RS 232 physical interface.
How are you ensuring that parity error happens in hardware?
Test scenario:
My host app expects the parity bit to be set, but i send data from serial monitor that does not have the bit set, this is the way i ensure parity error should happen.
Note: I only use parity type that OS supports.
I have tested your library on Windows 10 x64.
First problem:
Parity checking does not work on WIndows 10 x64. I think the reason is your configureComPortControl method does not set fParity to TRUE. (check above in my previous post for code examples and output)
Parity checking does work on Ubuntu 19. When the parity error happens two error bytes are passed which is ok.
Second problem:
But why do you have SerilComLineError class? Shouldn't this class work as filter mechanism that provides you the info about occurred errors?
Your test case will generate Framing error not parity error. Receiving end will see logic high as parity bit. This means receiving end will always receive 1 as parity bit value.
Try this set odd parity at receiver side and send 8 bit data as 00011111 from sending side (odd number of once in data frame). Due to odd parity setting, receiving side will expect 0 as parity bit value which it will not get as sender side will always send 1 as parity bit, hence parity error will be generate at receiving end.
@RishiGupta12 I've tested your library thoroughly. Parity checking does not work on Windows 10 x64 nor does framing errors. Your filter mechanism that provides you the info about occurred errors does not work at all on Windows 10 x64 and Ubuntu 19.
The library won't enable parity check on Windows 10 x64.
I've tried this example that you provided https://github.com/RishiGupta12/SerialPundit/tree/master/applications/d1-blocking-read-com
with enabling ParFraError like this:
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', true, false);
after this i printed:
System.out.println(Arrays.toString(scm.getCurrentConfiguration(handle)));
and i got this:
[DCBlength : 28, BaudRate : 19200, fBinary : TRUE, fParity : FALSE, fOutxCtsFlow : FALSE, fOutxDsrFlow : FALSE, fDtrControl : DTR_CONTROL_ENABLE, fDsrSensitivity : FALSE, fTXContinueOnXoff : TRUE, fOutX : FALSE, fInX : FALSE, fErrorChar : FALSE, fNull : FALSE, fRtsControl : RTS_CONTROL_ENABLE, fAbortOnError : FALSE, fDummy2 : NA, wReserved : NA, XonLim : 2048, XoffLim : 2048, ByteSize : 5, Parity : 1, StopBits : 0, XonChar : , XoffChar : , ErrorChar : , EofChar : , EvtChar : , wReserved1 : NA]
fParity is false, but it should be true