私は最近、クロスオーバー アダプター (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());
}
}
}