最近在搞C的串口通讯,C里面没有线程的概念,所以C对串口的读写只能在一个进程里面,这样
如果串口的缓存有问题,就会导致报告丢失(正好是我们遇到的),我们来看看支持线程的java是如何来解决这个问题的。
本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。
本类库主要包括:SerialBean.java (与其他应用程序的接口), SerialBuffer.java (用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。
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 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
SerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。public synchronized String GetMsg(int Length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
public synchronized void PutChar(int c)
本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。
在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此GetMsg和PutChar函数均被声明为synchronized并在具体实现中采取措施实现的数据的同步。
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)
{
没有合适的资源?快使用搜索试试~ 我知道了~
RXTX.JAR和javax.comm.jar包
共145个文件
java:43个
html:23个
class:21个
5星 · 超过95%的资源 需积分: 34 116 下载量 138 浏览量
2013-10-21
17:18:17
上传
评论 1
收藏 937KB ZIP 举报
温馨提示
用于java进行串口读取操作,尤其RXTX.jar找了好久才找到。。。。你值得拥有
资源推荐
资源详情
资源评论
收起资源包目录
RXTX.JAR和javax.comm.jar包 (145个子文件)
SerialPortDisplay.class 9KB
BlackBox.class 6KB
Transmitter.class 5KB
Receiver.class 4KB
ReceiveFraming.class 3KB
CtlSigDisplay.class 3KB
ReceiveThreshold.class 3KB
ReceiveTimeout.class 3KB
BufferSize.class 3KB
FlowCtlDisplay.class 3KB
ByteStatistics.class 2KB
BaudRate.class 2KB
DataBits.class 2KB
StopBits.class 2KB
Parity.class 2KB
ByteCounter.class 2KB
ToggleMouseListener.class 1KB
ReceiveOptions.class 1KB
OneWayMouseListener.class 864B
ToggleLabel.class 689B
ConvertedOutputStream.class 668B
Thumbs.db 36KB
rxtxSerial.dll 76KB
rxtxParallel.dll 46KB
win32com.dll 27KB
win32com.dll 27KB
constructor-index.gif 2KB
exception-index.gif 2KB
interface-index.gif 2KB
package-index.gif 2KB
method-index.gif 2KB
variable-index.gif 2KB
constructors.gif 2KB
class-index.gif 1KB
methods.gif 1KB
variables.gif 1KB
yellow-ball.gif 925B
blue-ball.gif 925B
magenta-ball.gif 896B
green-ball.gif 886B
red-ball.gif 527B
blue-ball-small.gif 255B
red-ball-small.gif 255B
yellow-ball-small.gif 255B
magenta-ball-small.gif 104B
green-ball-small.gif 102B
AllNames.html 37KB
javax.comm.SerialPort.html 31KB
javax.comm.CommPort.html 21KB
javax.comm.CommPortIdentifier.html 15KB
javax.comm.ParallelPort.html 14KB
javax.comm.SerialPortEvent.html 9KB
javax.comm.ParallelPortEvent.html 6KB
javax.comm.CommPortOwnershipListener.html 5KB
API_users_guide.html 4KB
Readme.html 4KB
PlatformSpecific.html 4KB
apichanges.html 3KB
javax.comm.UnsupportedCommOperationException.html 3KB
javax.comm.CommDriver.html 3KB
jdk1.2.html 2KB
javax.comm.PortInUseException.html 2KB
javax.comm.ParallelPortEventListener.html 2KB
tree.html 2KB
javax.comm.SerialPortEventListener.html 2KB
ReceiveBehavior.html 2KB
Package-javax.comm.html 2KB
javax.comm.NoSuchPortException.html 1KB
packages.html 581B
INSTALL 2KB
RXTXcomm.jar 58KB
BlackBox.jar 35KB
comm.jar 27KB
comm.jar 27KB
CommPortIdentifier.java 21KB
SerialDemo.java 18KB
SerialPortDisplay.java 12KB
ParallelPortDisplay.java 11KB
SerialConnection.java 10KB
SerialParameters.java 10KB
NullSerialPort.java 10KB
BlackBox.java 8KB
ParallelBlackBox.java 7KB
CtlSigDisplay.java 6KB
Transmitter.java 6KB
Receiver.java 5KB
Transmitter.java 5KB
Receiver.java 5KB
FlowCtlDisplay.java 5KB
ByteStatistics.java 5KB
ByteStatistics.java 5KB
ReceiveThreshold.java 4KB
ReceiveThreshold.java 4KB
ReceiveTimeout.java 4KB
ReceiveTimeout.java 4KB
ReceiveFraming.java 4KB
ReceiveFraming.java 4KB
SimpleRead.java 4KB
BufferSize.java 4KB
BufferSize.java 4KB
共 145 条
- 1
- 2
Annnnnnnnnnn
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页