大家都知道,java是一门内容极其丰富的编程语言,它的应用也极其的广泛,几乎涉及到了我们生活的方方面面。今天就来为大家介绍一下java的基础知识,java中SerialBean的概念,随后也会展示它的源代码。
首先,我们需要知道的是,SerialBean是本类库与和他应用程序的接口。这类库中定义了SerialBean的构造方法以及初始化串口,是从串口读取数据,往串口写入数据以及关闭串口的函数。它的具体内涵如下:
1.public SerialBean(int PortID)
它是本函数构造一个指向特定串口的SerialBean,这个串口由参数PortID所指定。PortID=1表示COM1,PortID=2表示COM2,以此类推。
2.public int Initialize()
本函数初始化所指定的串口并且返回初始化结果。如果初始化成功就返回1,否则返回-1。初始化的结果是这个串口被SerialBean独占性使用,它的参数被设置为9600, N, 8, 1。如果串口被成功初始化,就会打开一个进程读取从串口传入的数据并将其保存在缓冲区中。
3.public String ReadPort(int Length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
4.public void WritePort(String Msg)
本函数向串口发送一个字符串。参数Msg是需要发送的字符串。
5.public void ClosePort()
本函数停止串口检测进程并关闭串口。
它的源代码如下所示:
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(); } }
以上就是java中SerialBean概念的有关内容。如果你对java感兴趣,想要了解更多java基础,敬请关注奇Q工具网。
推荐阅读: