国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用Java實現串口全雙工通訊

2019-11-18 11:09:57
字體:
來源:轉載
供稿:網友

  一個嵌入式系統通常需要通過串口與其主控系統進行全雙工通訊,譬如一個流水線控制系統需要不斷的接受從主控系統發送來的查詢和控制信息,并將執行結果或查詢結果發送回主控系統。本文介紹了一個簡單的通過串口實現全雙工通訊的java類庫,該類庫大大的簡化了對串口進行操作的過程。

  本類庫主要包括:SerialBean.java (與其他應用程序的接口), SerialBuffer.java(用來保存從串口所接收數據的緩沖區), ReadSerial.java (從串口讀取數據的程序)。另外本類庫還提供了一個例程SeriAlexample.java 作為示范。在下面的內容中將逐一對這幾個部分進行具體介紹。

  1. SerialBean

  SerialBean是本類庫與其他應用程序的接口。該類庫中定義了SerialBean的構造方法以及初始化串口,從串口讀取數據,往串口寫入數據以及關閉串口的函數。具體介紹如下:

  public SerialBean(int PortID)
  本函數構造一個指向特定串口的SerialBean,該串口由參數PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此類推。

  public int Initialize()
  本函數初始化所指定的串口并返回初始化結果。假如初始化成功返回1,否則返回-1。初始化的結果是該串口被SerialBean獨占性使用,其參數被設置為9600, N, 8, 1。假如串口被成功初始化,則打開一個進程讀取從串口傳入的數據并將其保存在緩沖區中。

  public String ReadPort(int Length)
  本函數從串口(緩沖區)中讀取指定長度的一個字符串。參數Length指定所返回字符串的長度。

  public void WritePort(String Msg)
  本函數向串口發送一個字符串。參數Msg是需要發送的字符串。

  public void ClosePort()
  本函數停止串口檢測進程并關閉串口。

  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 starts a
 * thread which consistently monitors the serial port. Any signal captured
 * 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 incoming
 * 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();
 }
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 札达县| 古浪县| 浏阳市| 吉木萨尔县| 克什克腾旗| 社会| 宁海县| 田阳县| 灵台县| 淮阳县| 安国市| 阳西县| 丹寨县| 家居| 兴文县| 南平市| 得荣县| 南乐县| 南川市| 宿州市| 保定市| 肇东市| 龙胜| 进贤县| 若羌县| 栖霞市| 理塘县| 阜新| 莆田市| 习水县| 毕节市| 博爱县| 怀集县| 罗山县| 祁门县| 大港区| 龙山县| 布尔津县| 黔江区| 乌拉特中旗| 武清区|