用户登陆 用户注册
您的位置:首页>
技术文章>内容正文
利用Java实现串口全双工通讯
[正文]:一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。
本文介绍了一个简单的通过串口实现全双工通讯的java类库,该类库大大的简化了对串口进行操作的过程。
本类库主要包括:serialbean.java (与其他应用程序的接口), serialbuffer.java (用来保存从串口所接收数据的缓冲区), readserial.java (从串口读取数据的程序)。
另外本类库还提供了一个例程serialexample.java 作为示范。
在下面的内容中将逐一对这几个部分进行详细介绍。
1. serialbeanserialbean是本类库与其他应用程序的接口。
该类库中定义了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 serialbean123;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)123;portname = "com" + portid;125;/**** 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()123;int initsuccess = 1;int initfail = -1;try123;portid = commportidentifier.getportidentifier(portname);try123;serialport = (serialport)portid.open("serial_communication", 2000);125; catch (portinuseexception e)123;return initfail;125;//use inputstream in to read from the serial port, and outputstream//out to write to the serial port.try123;in = serialport.getinputstream();out = serialport.getoutputstream();125; catch (ioexception e)123;return initfail;125;//initialize the communication parameters to 9600, 8, 1, none.try123;serialport.setserialportparams(9600,serialport.databits_8,serialport.stopbits_1,serialport.parity_none);125; catch (unsupportedcommoperationexception e)123;return initfail;125;125; catch (nosuchportexception e)123;return initfail;125;// 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 informationreturn initsuccess;125;/**** 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)123;string msg;msg = sb.getmsg(length);return msg;125;/**** this function sends a message through the serial port.** @param msg the string to be sent.**/public void writeport(string msg)123;int c;try123;for (int i = 0; i < msg.length(); i++)out.write(msg.charat(i));125; catch (ioexception e) 123;125;125;/**** this function closes the serial port in use.**/public void closeport()123;rt.stop();serialport.close();125;125;2. 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 serialbuffer123;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)123;lengthneeded = length;notifyall();if (lengthneeded > content.length())123;available = false;while (available == false)123;try123;wait();125; catch (interruptedexception e) 123; 125;125;125;currentmsg = content.substring(0, lengthneeded);tempcontent = content.substring(lengthneeded);content = tempcontent;lengthneeded = 1;notifyall();return currentmsg;125;/**** 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)123;character d = new character((char) c);content = content.concat(d.tostring());if (lengthneeded < content.length())123;available = true;125;notifyall();125;125;3. readserialreadserial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。
public readserial(serialbuffer sb, inputstream port)本函数构造一个readserial进程,参数sb指定存放传入数据的缓冲区,参数port指定从串口所接收的数据流。
public void run()readserial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。
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 thread123;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)123;combuffer = sb;comport = port;125;public void run()123;int c;try123;while (true)123;c = comport.read();combuffer.putchar(c);125;125; catch (ioexception e) 123;125;125;125;4. serialexampleserialexample是本类库所提供的一个例程。
它所实现的功能是打开串口com1,对其进行初始化,从串口读取信息对其进行处理后将处理结果发送到串口。
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 serialexample123;public static void main(string[] args)123;//to d add your java codes hereserialbean sb = new serialbean(1);string msg;sb.initialize();for (int i = 5; i <= 10; i++)123;msg = sb.readport(i);sb.writeport("reply: " + msg);125;sb.closeport();125;125;5. 编译与调试 本类库中使用了java communication api (javax.comm)。
这是一个java扩展类库,并不包括在标准的java sdk当中。
如果你尚未安装这个扩展类库的话,你应该从sun公司的java站点下载这个类库并将其安装在你的系统上。
在所下载的包里面包括一个安装说明,如果你没有正确安装这个类库及其运行环境的话,运行这个程序的时候你会找不到串口。
正确安装java communication api并将上述程序编译通过以后,你可以按如下方法测试这个程序。
如果你只有一台机器,你可以利用一条rs-232电缆将com1和com2连接起来,在com1上运行serialexample,在com2上运行windows提供的超级终端程序。
如果你有两台机器的话,你可以利用一条rs-232电缆将两台机器的com1(或者是com2)连接起来,在一端运行例程,另外一端运行windows提供的超级终端程序。
如果有必要的话,可以对serialexample中所声明的串口进行相应改动。
网站首页
培训课程
维修指南
技术文章
家电专栏
供应信息
求购信息
培训资讯
展会信息
电脑专栏
教程下载
资料下载
常用软件
PLC教程
PLC资料
变频伺服
低压电器
维修资料
人机界面
自控仪表
工控机类
文章标题:
中国工控资源网手机版 2012
电话:010-67577139 13811659603
培训咨询QQ:657167934 471895637 销售咨询QQ:623769457
联系邮箱:zggkzyw@163.com
京ICP备11002135号
报时(2026-04-06 06:40:15)