全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1489
推到 Plurk!
推到 Facebook!

關於rs232的問題

尚未結案
kanbo
一般會員


發表:1
回覆:0
積分:0
註冊:2004-08-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-23 21:33:26 IP:211.23.xxx.xxx 未訂閱
請問使用rs232要如何open和定義鮑率呢? 我的程式如下,只是要輸出字串到指定的com port, compile 有過,卻出現 Exception in thread "main" java.lang.NullPointerException at Ctrl.main(Ctrl.java:27) 請問是何原因呢? ************************************************** import java.io.*; import java.util.*; import javax.comm.*; public class Ctrl { static Enumeration portList; static SerialPort serialPort; static CommPortIdentifier portId; protected OutputStream outputStream; protected InputStream inputStream; protected String portSettingString; protected boolean portOpenNow; private String settingString = "9600,n,8,1"; public static void main(String[] args) { if (args.length < 1) { System.out.println("Usage:port name"); } else { try { serialPort = (SerialPort) portId.open("Ctrl", 20); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); CopyThread input = new CopyThread(System.in, serialPort.getOutputStream()); CopyThread output = new CopyThread(serialPort.getInputStream(), System.out); input.start(); output.start(); } catch (PortInUseException e) { e.printStackTrace(); } catch (UnsupportedCommOperationException e1) { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } } } class CopyThread extends Thread { InputStream theInput; OutputStream theOutput; CopyThread(InputStream in) { this(in, System.out); } CopyThread(OutputStream out) { this(System.in, out); } CopyThread(InputStream in, OutputStream out) { theInput = in; theOutput = out; } public void run() { try { byte[] buffer = new byte[256]; String byteRead = "#010001"; ByteArrayInputStream bis = new ByteArrayInputStream(byteRead .getBytes()); int byte_read = 0; while ((byte_read = bis.read()) != -1) { theOutput.write(byte_read); } } catch (IOException e) { System.err.println(e); } } } *************************************
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-08-24 10:37:44 IP:61.56.xxx.xxx 未訂閱
dear kanbo: 你的portId尚未初使化 請在你的:
  ...
  ...
  serialPort = (SerialPort) portId.open("Ctrl", 20);
  ...
該行之前,加入:CommPortIdentifier.getPortIdentifier("COMM1");//假設是要開comm1 使之成為:
    ...
    ...
    ...
  portId = CommPortIdentifier.getPortIdentifier(args[1]); //args 0 為執行類別本身(就是main所在的這個java檔,而 arg[1]才是第一個參數            
  serialPort = (SerialPort) portId.open("Ctrl", 20);              
   .....
系統時間:2024-04-27 16:38:27
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!