2

私は最近、クロスオーバー アダプター (Null Modem) を備えたシリアル ケーブルを手に取り、2 台の Linux (Lubuntu) コンピューター間で制御されたバイトの受け渡しを行うことができるかどうかを確認するための教育実験になるのではないかと考えました。/dev/ttyS0「ファイル」を入力お​​よび出力ファイルストリームとして開く基本的なコードを Java で記述しました。

minicom だけでなく、echo や cat でもデータを送受信できます。これらのプログラムの作成者は、私が理解していないことを理解していると思います :) しかし、何らかの理由で、このコードで同じことをしようとすると、LF (ascii 10) 文字が追加されるまで送信側がハングします。データのチャンクを送信する何らかの理由があるまで、OSがバイトを保持していると思います...?さらに、受信側は「10」レシートの 2 つのコピーを報告しますが、これは本当に理解できません。

どういうわけか、バイトを書いたらすぐに反対側に表示されるはずだと思っていますが、そうではありません。

私が言ったように、これはOSがシリアルポートとどのように相互作用するかをよりよく理解すること以外に本当の最終ゲームのない探索的な演習です...情報をありがとう!

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class SOtest {

public static void main(String[] args) {
    SOtest sot = new SOtest();
    sot.rx();    // or sot.tx() for the transmit side
}

public void tx()  {
    FileOutputStream nmoutfile;

    try {
        nmoutfile = new FileOutputStream("/dev/ttyS0");
        nmoutfile.write(49);  //  ascii value 10 still needed...?
        nmoutfile.close();    //  doesn't force value 49 to send

    } catch (Exception ex) {
        System.out.println(ex.getMessage().toString());
    }
}

public void rx()  {
    FileInputStream nminfile; 

    try {
        nminfile = new FileInputStream("/dev/ttyS0");

        while (true) {
            System.out.println(nminfile.read());
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage().toString());
    } 
}
}
4

2 に答える 2

0

JTermios と Maven を使用した PureJavaComm、

Mac OS X、Linux、および Windows プラットフォーム上の Sun および RXTX プロジェクトの JavaComm SerialPort のオープン ソース、純粋な Java、ドロップイン置換。

JTermiosReadDemo.java:

import java.io.IOException;
import java.util.Scanner;

import purejavacomm.CommPortIdentifier;
import purejavacomm.NoSuchPortException;
import purejavacomm.PortInUseException;
import purejavacomm.SerialPort;
import purejavacomm.UnsupportedCommOperationException;


public class JTermiosReadDemo {

    public static void main(String[] args) throws IOException, PortInUseException, NoSuchPortException, UnsupportedCommOperationException {
        String port = "/dev/ttyUSB0";
        SerialPort serialPort = (SerialPort)    CommPortIdentifier.getPortIdentifier(port).open(
                JTermiosDemo.class.getName(), 0);
        Scanner scanner = new Scanner(serialPort.getInputStream());
        while (scanner.hasNext()) {
            System.out.println(scanner.nextLine());
            
        }
        scanner.close();
    }

}

pom.xml:

<dependencies>
    <dependency>
        <groupId>com.sparetimelabs</groupId>
        <artifactId>purejavacomm</artifactId>
        <version>0.0.22</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>com.sparetimelabs</id>
        <url>http://www.sparetimelabs.com/maven2</url>
    </repository>
</repositories>

参照:

于 2015-08-16T21:33:00.903 に答える