小弟我在開發基于 javacomm20-win32.zip 的串口程序。在設置串口狀態時,eclipse環境下,SerialPort打點下拉出的選項有:
.PARITY_NONE
.PARITY_ODD
.PARITY_EVEN.PARITY_MARK
.PARITY_SPACE其中前三個:無校驗和奇偶校驗都好用,且java自帶例程里只有前三項。
但是后兩項是api提示出來的,編譯也通過,但是為何會出現:javax.comm.UnsupportedCommOperationException: Unsupported parity value
at com.sun.comm.Win32SerialPort.setSerialPortParams(Win32SerialPort.java:268)是否java本身不支持  和space 校驗,但是又在api里浮夸了一下?被逼無奈,只好:
System.out.println(SerialPort.PARITY_NONE);
System.out.println(SerialPort.PARITY_ODD);
System.out.println(SerialPort.PARITY_EVEN);
System.out.println(SerialPort.PARITY_MARK);
System.out.println(SerialPort.PARITY_SPACE);
結果是:
0
1
2
3
4
也就是說,java的comm api 定義了校驗的類型,但是
填寫0,1,2這些都是可以的,但是填寫 3,4 就會:javax.comm.UnsupportedCommOperationException: Unsupported parity value serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, XXX);
在XXX位置上應該是放
SerialPort.PARITY_NONE
SerialPort.PARITY_ODD
SerialPort.PARITY_EVEN
都可以
為什么放
SerialPort.PARITY_MARK
SerialPort.PARITY_SPACE
就會出錯?
就會
javax.comm.UnsupportedCommOperationException: Unsupported parity value
后者也是SerialPort 類的常量啊?這是為什么?

解决方案 »

  1.   

    static int  PARITY_EVEN
              EVEN parity scheme.
    static int  PARITY_MARK
              MARK parity scheme.
    static int  PARITY_NONE
              No parity bit.
    static int  PARITY_ODD
              ODD parity scheme.
    static int  PARITY_SPACE
              SPACE parity scheme.這是我在sun網站上看到的,應該支持 和 space的,為什么就是有異常呢?
      

  2.   

    哇塞,寫串口程序都快一年了,才知道有個東東叫 RXTX 是專門為jdk編寫串口支持包的,汗~終于擺脫 javacomm20-win32了,這個版本的 和space校驗是完全好用地!