2013-07-17 60 views
1

希望在此找到解決方案。通過Comport以14400波特率與Java進行通信 - 串行通信

我有一個控制器,它在UART上使用特定於應用程序的協議,並且此協議僅在14400 bps下工作。

我必須使用java代碼通過串行端口與此控制器進行通信。

但我從我使用的API不支持14400波特率,雖然他們稱它爲標準!!

我試過使用javax.comm和rxtx jar's。沒有真正的幫助,因爲它們都不支持這個波特率。

任何人都可以在這方面幫助我,這將是非常有幫助的。

謝謝!

確定這裏是代碼片段

 selectedPort = (SerialPort) portID.open("FlashProgramming", 
       TIMEOUT); 
     System.out.println("port " + selectedPort + " is opened"); 



    try { 

     selectedPort.setSerialPortParams(14400, 
       SerialPort.DATABITS_8, SerialPort.STOPBITS_1, 
       SerialPort.PARITY_NONE); 

//這裏試圖波特率設置爲14400,但它是回滾到9600,默認爲14400,不支持!

} 


    // no handshaking or other flow control 
    try { 
     selectedPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); 
    } catch (UnsupportedCommOperationException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    // timer on any read of the serial port 
    try { 
     selectedPort.enableReceiveTimeout(TIMEOUT); 
    } catch (UnsupportedCommOperationException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    System.out.println("Comport Configeration is done"); 

    try { 

     Serialdriver.in = selectedPort.getInputStream(); 
     Serialdriver.out = selectedPort.getOutputStream(); 

     System.out.println("i/p & o/p's are initialized"); 

     Serialdriver.handlerParamselScreen.displaystatus("Seleted Com port " 
       + selectedPort + " is Successfully opened"); 
    } 



     selectedPort.addEventListener(Serialdriver); 



    selectedPort.notifyOnDataAvailable(true); 


    return selectedPort; 

} 
+0

沒人確定答案?來吧! – Abhi4ever

+0

你的問題還不夠清楚。你正在使用哪個API?你得到了什麼異常?你正在使用哪個IDE?還發布你寫的代碼 –

+1

它在Linux上的已知問題:http://stackoverflow.com/questions/3192478/specifying-non-standard-baud-rate-for-ftdi-virtual-serial-port-under-linux – user2053898

回答

0

那麼這裏是解決方案,

我取代從javax.comm低級別的DLL來RXTX。

這裏是鏈接 - rxtx latest driveres

而最好的部分是它同時支持X32和x64 JRE的!將代碼從javax.comm移植到rxtx只需5分鐘的工作。

我希望這是有幫助的。

乾杯!

相關問題