Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to detect Parity errors? #40

Open
ghost opened this issue Aug 26, 2019 · 6 comments
Open

How to detect Parity errors? #40

ghost opened this issue Aug 26, 2019 · 6 comments

Comments

@ghost
Copy link

ghost commented Aug 26, 2019

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

@RishiGupta12
Copy link
Owner

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);

@ghost
Copy link
Author

ghost commented Aug 27, 2019

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.

@RishiGupta12
Copy link
Owner

  • What exactly you want to do, What is your application.
  • How you are ensuring that parity error happens in hardware. When parity error happens, hardware tells driver that this error has happened. How you are doing this.

@ghost
Copy link
Author

ghost commented Aug 28, 2019

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?

  1. I have two machines that are connected with RS 232 null modem cable.
  2. First machine has Eltima Serial Monitor that generates messages where i can adjust parity, stop bits, size, etc..
  3. Second machine has my host app.

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.

  1. 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.

  1. 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?

@RishiGupta12
Copy link
Owner

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.

@ghost
Copy link
Author

ghost commented Sep 1, 2019

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant