2017-01-18 10 views
0

私はデータを送受信したいマイクロコントローラを持っているので、UARTポートを使用してこれが可能かどうかを調べようとしました。私は、液晶画面にchar値(文字自体ではなくASCIIコード)を表示するマイコン用のクイックプログラムを書いていました。ボタンを押したときには、76を返します( 'L'のコードはこのすべてのプロジェクトが私に与えている)。私はMac用のCoolTermをダウンロードした(Macは私の学校が私に与えたものであり、選択肢ではないのでMacを使用している)。this USB to UARTケーブルを購入した。ドライバをダウンロードしたら、CoolTermを起動し、ポートを選択してボーレートを選択しました。私は接続を押して、キーを押すのを開始しました。私がしたとき、私は自分のLCDに正しい対応するASCII値を持っていました。マイコンのボタンを押したとき、私は自分の端末に 'L'を受け取りました。すべてが完璧に機能しました。私はjserialcommをダウンロードし、マイクロコントローラからの値を読み取ることができるかどうかを調べるための小さなプログラムを作った。私がプログラムを始めたときには、今は毎時1バイトしか読み込まれませんでした。それが読み取るバイトはちょうど1になります。次に別の問題が発生しました。港が適切に閉鎖されていないようだった。プログラムが終了した後、私はもう一度それを実行しようとしたが、それはポートを開こうとしたときに邪魔になり、その後は決してコードを実行しません。私がプログラムを終了するときには、「終了しない」と言います。そして、それが私の前提とするものですが、強制はプログラムを終了します。私がCoolTermに戻ってポートを開くと、接続とフリーズにつかまり、強制終了しなければならない。JSerialCommがMacでポートを読み書きできない

//This is what I use to set up the the Port 
    //This gets all of the ports on the machine 
    SerialPort[] q; 
    q = SerialPort.getCommPorts(); 

    //This iterates through the ports and gives a description and the name of the port 
    for(SerialPort a: q){ 
     System.out.println(a.getDescriptivePortName() + " : " + a.getSystemPortName()); 
    } 

    //Allows user to select which port they want 
    System.out.println("Which port do you want?"); 
    Scanner s = new Scanner(System.in); 
    int portnumber = s.nextInt(); 
    s.close(); 

    //Creates a SerialPort object of the port the user selected and opens it 
    SerialPort commPort = q[portnumber]; 
    System.out.println(commPort.getDescriptivePortName()); 

    if(commPort.openPort()){ 
     System.out.println("Port Opened"); 
    }else{ 
     System.out.println("Port Failed to Open"); 
    } 

    commPort.setBaudRate(230400); 


//This is the first method I tried 
commPort.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 1000, 0); 
    try{ 
     while(true){ 
      //Waits till there are bytes available 
      while(commPort.bytesAvailable() == 0){ 
       Thread.sleep(20); 
      } 

      //Creates a buffer to read the bytes 
      byte[] readBuffer = new byte[commPort.bytesAvailable()]; 
      int numOfBytes = commPort.readBytes(readBuffer, readBuffer.length); 
      //Prints out the number of bytes read and the bytes it read 
      System.out.println("Read " + numOfBytes + " bytes. Message:"); 
      for(byte b: readBuffer){ 
       System.out.println("::::" + Integer.toBinaryString(b & 0xFF)); 
      } 

     } 
    }catch(Exception e){ 
     e.printStackTrace(); 

    } 


The second method I did was using InputStreams which I would prefer not to use since I rather just read raw bytes in. 

答えて

0

編集して、タイムアウト:

commPort.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 0, 0); 
。。私は、Cuを使用しています*よくある質問の状態とないのttyとして*

これは私が使用していたJavaコードで明確にするために、

、または行を削除してください。あなたの場合、あなたはそれを必要としません。なぜ変わったボーレートですか? USB-to-RS232ブリッジは、すべてのボーレートで動作しないことがあります。

9600baudで動作します。このことができます

希望、

ハニ

関連する問題