欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
利用Java實(shí)現串口全雙工通訊
一個(gè)嵌入式系統通常需要通過(guò)串口與其主控系統進(jìn)行全雙工通訊,譬如一個(gè)流水線(xiàn)控制系統需要不斷的接受從主控系統發(fā)送來(lái)的查詢(xún)和控制信息,并將執行結果或查詢(xún)結果發(fā)送回主控系統。本文介紹了一個(gè)簡(jiǎn)單的通過(guò)串口實(shí)現全雙工通訊的Java類(lèi)庫,該類(lèi)庫大大的簡(jiǎn)化了對串口進(jìn)行操作的過(guò)程。
本類(lèi)庫主要包括:SerialBean.java (與其他應用程序的接口), SerialBuffer.java (用來(lái)保存從串口所接收數據的緩沖區), ReadSerial.java (從串口讀取數據的程序)。另外本類(lèi)庫還提供了一個(gè)例程SerialExample.java 作為示范。在下面的內容中將逐一對這幾個(gè)部分進(jìn)行詳細介紹。
SerialBean是本類(lèi)庫與其他應用程序的接口。該類(lèi)庫中定義了SerialBean的構造方法以及初始化串口,從串口讀取數據,往串口寫(xiě)入數據以及關(guān)閉串口的函數。具體介紹如下:
public SerialBean(int PortID)
本函數構造一個(gè)指向特定串口的SerialBean,該串口由參數PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此類(lèi)推。
public int Initialize()
本函數初始化所指定的串口并返回初始化結果。如果初始化成功返回1,否則返回-1。初始化的結果是該串口被SerialBean獨占性使用,其參數被設置為9600, N, 8, 1。如果串口被成功初始化,則打開(kāi)一個(gè)進(jìn)程讀取從串口傳入的數據并將其保存在緩沖區中。
public String ReadPort(int Length)
本函數從串口(緩沖區)中讀取指定長(cháng)度的一個(gè)字符串。參數Length指定所返回字符串的長(cháng)度。
public void WritePort(String Msg)
本函數向串口發(fā)送一個(gè)字符串。參數Msg是需要發(fā)送的字符串。
public void ClosePort()
本函數停止串口檢測進(jìn)程并關(guān)閉串口。
SerialBean的源代碼如下:
package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* This bean provides some basic functions to implement full dulplex
* information exchange through the srial port.
*
*/
public class SerialBean
{
static String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream  in;
SerialBuffer SB;
ReadSerial   RT;
/**
*
* Constructor
*
* @param PortID the ID of the serial to be used. 1 for COM1,
* 2 for COM2, etc.
*
*/
public SerialBean(int PortID)
{
PortName = "COM" + PortID;
}
/**
*
* This function initialize the serial port for communication. It startss a
* thread which consistently monitors the serial port. Any signal capturred
* from the serial port is stored into a buffer area.
*
*/
public int Initialize()
{
int InitSuccess = 1;
int InitFail    = -1;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)
portId.open("Serial_Communication", 2000);
} catch (PortInUseException e)
{
return InitFail;
}
//Use InputStream in to read from the serial port, and OutputStream
//out to write to the serial port.
try
{
in  = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e)
{
return InitFail;
}
//Initialize the communication parameters to 9600, 8, 1, none.
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e)
{
return InitFail;
}
} catch (NoSuchPortException e)
{
return InitFail;
}
// when successfully open the serial port,  create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. Incoming signals are stored in the serial buffer.
SB = new SerialBuffer();
RT = new ReadSerial(SB, in);
RT.start();
// return success information
return InitSuccess;
}
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public String ReadPort(int Length)
{
String Msg;
Msg = SB.GetMsg(Length);
return Msg;
}
/**
*
* This function sends a message through the serial port.
*
* @param Msg The string to be sent.
*
*/
public void WritePort(String Msg)
{
int c;
try
{
for (int i = 0; i < Msg.length(); i++)
out.write(Msg.charAt(i));
} catch (IOException e)  {}
}
/**
*
* This function closes the serial port in use.
*
*/
public void ClosePort()
{
RT.stop();
serialPort.close();
}
}
SerialBuffer是本類(lèi)庫中所定義的串口緩沖區,它定義了往該緩沖區中寫(xiě)入數據和從該緩沖區中讀取數據所需要的函數。
public synchronized String GetMsg(int Length)
本函數從串口(緩沖區)中讀取指定長(cháng)度的一個(gè)字符串。參數Length指定所返回字符串的長(cháng)度。
public synchronized void PutChar(int c)
本函數望串口緩沖區中寫(xiě)入一個(gè)字符,參數c 是需要寫(xiě)入的字符。
在往緩沖區寫(xiě)入數據或者是從緩沖區讀取數據的時(shí)候,必須保證數據的同步,因此GetMsg和PutChar函數均被聲明為synchronized并在具體實(shí)現中采取措施實(shí)現的數據的同步。
SerialBuffer的源代碼如下:
package serial;
/**
*
* This class implements the buffer area to store incoming data from the serial
* port.
*
*/
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1;
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public synchronized String GetMsg(int Length)
{
LengthNeeded = Length;
notifyAll();
if (LengthNeeded > Content.length())
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
}
CurrentMsg  = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
}
/**
*
* This function stores a character captured from the serial port to the
* buffer area.
*
* @param t The char value of the character to be stored.
*
*/
public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (LengthNeeded < Content.length())
{
available = true;
}
notifyAll();
}
}
ReadSerial是一個(gè)進(jìn)程,它不斷的從指定的串口讀取數據并將其存放到緩沖區中。
public ReadSerial(SerialBuffer SB, InputStream Port)
本函數構造一個(gè)ReadSerial進(jìn)程,參數SB指定存放傳入數據的緩沖區,參數Port指定從串口所接收的數據流。
public void run()
ReadSerial進(jìn)程的主函數,它不斷的從指定的串口讀取數據并將其存放到緩沖區中。
ReadSerial的源代碼如下:
package serial;
import java.io.*;
/**
*
* This class reads message from the specific serial port and save
* the message to the serial buffer.
*
*/
public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private InputStream ComPort;
/**
*
* Constructor
*
* @param SB The buffer to save the incoming messages.
* @param Port The InputStream from the specific serial port.
*
*/
public ReadSerial(SerialBuffer SB, InputStream Port)
{
ComBuffer = SB;
ComPort = Port;
}
public void run()
{
int c;
try
{
while (true)
{
c = ComPort.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}
SerialExample是本類(lèi)庫所提供的一個(gè)例程。它所實(shí)現的功能是打開(kāi)串口COM1,對其進(jìn)行初始化,從串口讀取信息對其進(jìn)行處理后將處理結果發(fā)送到串口。
import serial.*;
import java.io.*;
/**
*
* This is an example of how to use the SerialBean. It opens COM1 and reads
* six messages with different length form the serial port.
*
*/
class SerialExample
{
public static void main(String[] args)
{
//TO DO: Add your JAVA codes here
SerialBean SB = new SerialBean(1);
String Msg;
SB.Initialize();
for (int i = 5; i <= 10; i++)
{
Msg = SB.ReadPort(i);
SB.WritePort("Reply: " + Msg);
}
SB.ClosePort();
}
}
本類(lèi)庫中使用了Java Communication API (javax.comm)。這是一個(gè)Java擴展類(lèi)庫,并不包括在標準的Java SDK當中。如果你尚未安裝這個(gè)擴展類(lèi)庫的話(huà),你應該從Sun公司的Java站點(diǎn)下載這個(gè)類(lèi)庫并將其安裝在你的系統上。在所下載的包里面包括一個(gè)安裝說(shuō)明,如果你沒(méi)有正確安裝這個(gè)類(lèi)庫及其運行環(huán)境的話(huà),運行這個(gè)程序的時(shí)候你會(huì )找不到串口。
正確安裝Java Communication API并將上述程序編譯通過(guò)以后,你可以按如下方法測試這個(gè)程序。如果你只有一臺機器,你可以利用一條RS-232電纜將COM1和COM2連接起來(lái),在COM1上運行SerialExample,在COM2上運行Windows提供的超級終端程序。如果你有兩臺機器的話(huà),你可以利用一條RS-232電纜將兩臺機器的COM1(或者是COM2)連接起來(lái),在一端運行例程,另外一端運行Windows提供的超級終端程序。如果有必要的話(huà),可以對SerialExample中所聲明的串口進(jìn)行相應改動(dòng)。
本程序在Windows 2000 + Java SDK 1.3環(huán)境下編譯通過(guò)并成功運行。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Qt遍歷串口以及串口操作
Android 串口編程原理和實(shí)現方式(附源碼)
C#中使用serialport類(lèi)枚舉串口,并驗證串口是否被占用
android串口通信實(shí)例分析
vue代碼實(shí)現瀏覽器與串口通信
一步步搭建物聯(lián)網(wǎng)系統——簡(jiǎn)單物聯(lián)網(wǎng)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久