这个简单的程序包括以下文件:
imu.java (主程序)
readbuffer.java (从缓冲区读取一个消息)
readserial.java (读取串口数据并放入缓冲区)
serialbuffer.java (缓冲区)
writeserial.java (不断的往串口送星号'*')
测试程序:
sendcom.java (将一个数据文件往串口发送)
send.txt (供测试用的数据文件)
在这个通讯程序中使用了一个简单的协议,既不同的消息之间用星号'*'作为分隔。这个程序中的问题是readserial进程和writeserial进程不能够同时启动,出错信息是不能够打开串口,因为同样一个串口不能够同时被打开两次(在readserial中声明了filereader
测试程序:
sendcom.java (将一个数据文件往串口发送)
send.txt (供测试用的数据文件)
在这个通讯程序中使用了一个简单的协议,既不同的消息之间用星号'*'作为分隔。这个程序中的问题是readserial进程和writeserial进程不能够同时启动,出错信息是不能够打开串口,因为同样一个串口不能够同时被打开两次(在readserial中声明了filereader和在writeserial中声明了filewriter)。这样是不能够实现全双工通讯的。不知道有没有做过的大侠能够讲讲处理的办法。
/*
*
* imu.java 1.0
* main program for serial communication
*
* created: march 27, 2001
*
* author : qingye jiang (john)
* american gnc corporation
* 888 easy st, simi valley ca 93065-1812
*
* contact: (805) 582-0582 (tel), (805) 582-0098 (fax)
* qjiang@tsinghua.edu
*
*/
import java.io.*;
class imu
{
public static void main(string[] args)
{
//to do: add your java codes here
file comport = new file("com1");
serialbuffer sb = new serialbuffer();
readserial r1 = new readserial(sb, comport);
readbuffer r2 = new readbuffer(sb);
writeserial r3 = new writeserial(comport);
r1.start();
r2.start();
r3.start();
}
}
/*
*
* readbuffer.java 1.0
* program to read the serial buffer
*
* created: march 27, 2001
*
* author : qingye jiang (john)
* american gnc corporation
* 888 easy st, simi valley ca 93065-1812
*
* contact: (805) 582-0582 (tel), (805) 582-0098 (fax)
* qjiang@tsinghua.edu
*
*/
import java.io.*;
public class readbuffer extends thread
{
private serialbuffer combuffer;
public readbuffer(serialbuffer sb)
{
combuffer = sb;
}
public void run()
{
string msg;
while (true)
{
msg = combuffer.getmsg();
system.out.println(msg);
}
}
}
/*
*
* readserial.java 1.0
* program to read characters from the serial port and put it
* to the buffer
*
* created: march 27, 2001
*
* author : qingye jiang (john)
* american gnc corporation
* 888 easy st, simi valley ca 93065-1812
*
* contact: (805) 582-0582 (tel), (805) 582-0098 (fax)
* qjiang@tsinghua.edu
*
*/
import java.io.*;
public class readserial extends thread
{
private serialbuffer combuffer;
private file comport;
public readserial(serialbuffer sb, file port)
{
combuffer = sb;
comport = port;
}
public void run()
{
int c;
try
{
filereader in = new filereader(comport);
while (true)
{
c = in.read();
combuffer.putchar(c);
}
try
{
filereader in = new filereader(comport);
while (true)
{
c = in.read();
combuffer.putchar(c);
}
} catch (ioexception e) {}
}
}
/*
*
* serialbuffer.java 1.0
* class that implements the serial buffer
*
* created: march 27, 2001
*
* author : qingye jiang (john)
* american gnc corporation
* 888 easy st, simi valley ca 93065-1812
*
* contact: (805) 582-0582 (tel), (805) 582-0098 (fax)
* qjiang@tsinghua.edu
*
*/
public class serialbuffer
{
private string content = "";
private string currentmsg, tempcontent;
private boolean available = false;
public synchronized string getmsg()
{
int sepmark;
if ((sepmark = content.indexof('*')) == -1)
{
available = false;
while (available == false)
{
try
{
wait();
} catch (interruptedexception e) { }
}
sepmark = content.indexof('*');
}
currentmsg = content.substring(0, sepmark);
tempcontent = content.substring(sepmark+1);
content = tempcontent;
notifyall();
return currentmsg;
}
public synchronized void putchar(int c)
{
character d = new character((char) c);
content = content.concat(d.tostring());
if (c == '*')
{
available = true;
}
notifyall();
}
}
/*
*
* writeserial.java 1.0
* program to send a character to the serial port
*
* created: march 27, 2001
*
* author : qingye jiang (john)
* american gnc corporation
* 888 easy st, simi valley ca 93065-1812
*
* contact: (805) 582-0582 (tel), (805) 582-0098 (fax)
* qjiang@tsinghua.edu
*
*/
import java.io.*;
public class writeserial extends thread
{
private serialbuffer combuffer;
private file comport;
public writeserial(file port)
{
comport = port;
}
public void run()
{
int c;
try
{
filewriter out = new filewriter(comport);
while (true)
{
out.write('*');
}
} catch (ioexception e)
{
system.out.println(e.getmessage());
}
}
}
/*
*
* sendcom.java 1.0
*
* project: java based information exchange support system
* onboard plug-in system
* sending data through serial port
*
* created: march 15, 2001
*
* author : qingye jiang (john)
* american gnc corporation
* 888 easy st, simi valley ca 93065-1812
*
* contact: (805) 582-0582 (tel), (805) 582-0098 (fax)
*
*/
import java.io.*;
public class sendcom
{
public static void main(string[] args)
{
file outfile = new file("send.txt");
file comport = new file("com2");
int c;
try
{
filereader in = new filereader(outfile);
filewriter out = new filewriter(comport);
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
} catch (ioexception e) {}
}
}
send.txt*
this is a sample of the data file for program testing. *
it should be in the same directory as the sendcom.class file.*
when you run this sample program, connect your com1 and com2 with a
serial cable so that you can test this program on one machine. if
you have two machines, you can connect the two machine via a serial
cable and test it. modified the definition of comport in the program
if necessary. *
thank you for testing this program. if you have any suggestions please
kindly let me know. *
闽公网安备 35060202000074号