public SerialBean(int PortID) |
public int Initialize() |
public String ReadPort(int Length) |
public void WritePort(String Msg) |
public void ClosePort() |
package serial; import java.io.*; import java.util.*; import javaxm.*; /** * * 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) {uv转印胶 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 = PortIdentifier(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 = InputStream(); out = OutputStream(); } 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(); } } |
public synchronized String GetMsg(int Length) |
public synchronized void PutChar(int c) |
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 incoming * 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 = String()); if (LengthNeeded < Content.length()) { available = true; } notifyAll(); } } |
本文发布于:2024-09-22 07:22:07,感谢您对本站的认可!
本文链接:https://www.17tex.com/tex/4/253243.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |