溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Java讀取串口的程序(轉(zhuǎn))

發(fā)布時(shí)間:2021-11-12 10:24:56 來源:億速云 閱讀:408 作者:小新 欄目:編程語言

小編給大家分享一下如何使用Java讀取串口的程序(轉(zhuǎn)),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

這個(gè)簡單的程序包括以下文件:

IMU.java (主程序)
ReadBuffer.java (從緩沖區(qū)讀取一個(gè)消息)
ReadSerial.java (讀取串口數(shù)據(jù)并放入緩沖區(qū))
SerialBuffer.java (緩沖區(qū))
WriteSerial.java (不斷的往串口送星號(hào)′*′)

測試程序:

SendCom.java (將一個(gè)數(shù)據(jù)文件往串口發(fā)送)
SEND.TXT (供測試用的數(shù)據(jù)文件)

在這個(gè)通訊程序中使用了一個(gè)簡單的協(xié)議,既不同的消息之間用星號(hào)′*′作為分隔。這個(gè)程序中的問題是ReadSerial進(jìn)程和WriteSerial進(jìn)程不能夠同時(shí)啟動(dòng),出錯(cuò)信息是不能夠打開串口,因?yàn)橥瑯右粋€(gè)串口不能夠同時(shí)被打開兩次(在ReadSerial中聲明了FileReader

測試程序:

SendCom.java (將一個(gè)數(shù)據(jù)文件往串口發(fā)送)
SEND.TXT (供測試用的數(shù)據(jù)文件)

在這個(gè)通訊程序中使用了一個(gè)簡單的協(xié)議,既不同的消息之間用星號(hào)′*′作為分隔。這個(gè)程序中的問題是ReadSerial進(jìn)程和WriteSerial進(jìn)程不能夠同時(shí)啟動(dòng),出錯(cuò)信息是不能夠打開串口,因?yàn)橥瑯右粋€(gè)串口不能夠同時(shí)被打開兩次(在ReadSerial中聲明了FileReader和在WriteSerial中聲明了FileWriter)。這樣是不能夠?qū)崿F(xiàn)全雙工通訊的。
------------------------------------------
import java.io.*;

class IMU
{
public static void main(String[] args)
{
//TO DO: Add your JAVA codes here

File ComPort = new File("COM1");

SerialBuffer SB = new SerialBuffer();
ReadSerial r1 = new ReadSerial(SB, ComPort);
ReadBuffer r2 = new ReadBuffer(SB);
WriteSerial r3 = new WriteSerial(ComPort);

r1.start();
r2.start();
r3.start();
}
}
---------------------------------------------
import java.io.*;

public class ReadBuffer extends Thread
{
private SerialBuffer ComBuffer;

public ReadBuffer(SerialBuffer SB)
{
ComBuffer = SB;
}

public void run()
{
String Msg;

while (true)
{
Msg = ComBuffer.GetMsg();
System.out.println(Msg);
}

}
}
-----------------------------------------------
import java.io.*;

public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private File ComPort;

public ReadSerial(SerialBuffer SB, File Port)
{
ComBuffer = SB;
ComPort = Port;
}

public void run()
{
int c;
try
{
FileReader in = new FileReader(ComPort);
while (true)
{
c = in.read();
ComBuffer.PutChar(c);
}
try
{
FileReader in = new FileReader(ComPort);
while (true)
{
c = in.read();

ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}
------------------------------------------------
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;

public synchronized String GetMsg()
{
int SepMark;

if ((SepMark = Content.indexOf(′*′)) == -1)

{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
SepMark = Content.indexOf(′*′);
}

CurrentMsg = Content.substring(0, SepMark);
TempContent = Content.substring(SepMark+1);
Content = TempContent;
notifyAll();
return CurrentMsg;
}

public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (c == ′*′)
{
available = true;
}
notifyAll();
}
}
-------------------------------------------------
import java.io.*;

public class WriteSerial extends Thread
{
private SerialBuffer ComBuffer;
private File ComPort;

public WriteSerial(File Port)
{
ComPort = Port;
}

public void run()
{
int c;
try
{
FileWriter out = new FileWriter(ComPort);
while (true)
{
out.write(′*′);
}
} catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
-----------------------------------------------
import java.io.*;

public class SendCom
{
public static void main(String[] args)
{

File OutFile = new File("SEND.TXT");
File ComPort = new File("COM2");

int c;

try
{
FileReader in = new FileReader(OutFile);
FileWriter out = new FileWriter(ComPort);
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
} catch (IOException e) {}

}
}
----------------------------------------------
SEND.TXT*

This is a sample of the data file for program testing. *

It should be in the same directory as the SendCom.class file.*

When you run this sample program, connect your COM1 and COM2 with a
serial cable so that you can test this program on one machine. If
you have two machines, you can connect the two machine via a serial
cable and test it. Modified the definition of ComPort in the program
if necessary. *

看完了這篇文章,相信你對(duì)“如何使用Java讀取串口的程序(轉(zhuǎn))”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI