接手了使用java开发利用串口进行数据输出的工作。第一次进行此类工作的开发,没有太多的经验。根据网上搜索的结果,初步可以进行半角字符的输出了。但是在进行图像输出以及全角字符输出的时候,输出的结果总是乱码!希望大家可以帮忙看一下。
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.comm.*;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;import java.awt.image.BufferedImage;class S_Frame2 extends Frame implements Runnable,ActionListener
{
/*不使用java的安全管理*/
static { System.setSecurityManager(null); }private static final long serialVersionUID = 7132019526974859519L;static CommPortIdentifier portId;
static Enumeration portList;
OutputStream outputStream;
BufferedWriter bufWrite;
PrintStream printStream;SerialPort serialPort;
Thread readThread;
Panel p=new Panel();
TextField in_message=new TextField("打开COM1,波特率9600,数据位8,停止位1.");
TextArea out_message=new TextArea();
Button btnOpen=new Button("打开COM,通信开始");
Button btnClose=new Button("关闭COM,通信结束");
byte data[]=new byte[10240];boolean ;/*安排窗体*/
S_Frame2(){
super("串口送信");
setSize(200,200);
setVisible(true);
add(out_message,"Center");
add(p,"North");
p.add(btnOpen);
p.add(btnClose);
add(in_message,"South");
btnOpen.addActionListener(this);
btnClose.addActionListener(this);
} //R_Frame() end/*按钮事件响应*/
public void actionPerformed(ActionEvent event) {
if (event.getSource()==btnClose){
serialPort.close();//关闭串口
=true; //用于中止线程的run()方法
in_message.setText("关闭COM,通信结束");
}
else {
=false;
/*从文本区按字符读取数据*/
try{
data=out_message.getText().getBytes();
System.out.println("101");
}
catch(Exception e){
System.out.println("error:7");
e.printStackTrace();
}
/*打开串口*/
startComm();
in_message.setText("打开COM,通信开始、2秒钟通信一次");
}
} //actionPerformed() endpublic void startComm(){
/*获取本机端口列表 */
portList=CommPortIdentifier.getPortIdentifiers();while (portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();
System.out.println(portId.getName());
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM1")) {/*打开端口 */
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
}
catch (PortInUseException e) {
e.printStackTrace();
}
try {outputStream = serialPort.getOutputStream();}
catch (IOException e) {
e.printStackTrace();
}
} //if end
} //if end
} //while endtry{
readThread = new Thread(this);
readThread.start();
}
catch (Exception e) {
e.printStackTrace();
}
} //start() endpublic void run() {
/*设定串口参数*/
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
/*数据通信*/
try {
/*图像数据通信(事先准备好一个jpg文件)*/
InputStream imageIn = new FileInputStream(new File("c:\\temp\\css.jpg"));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage image = decoder.decodeAsBufferedImage();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(image);
imageIn.close();
Thread.sleep(500);/*画面上写入的文本数据通信*/
outputStream.write(data);
outputStream.flush();serialPort.close();
}
catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*传送数据后休眠2秒,然后再重新通信*/
try {
Thread.sleep(2000);
if ()
{
return; //
}
startComm();
}
catch (InterruptedException e) {
e.printStackTrace();
}
} //run() end
} //?S_Frame endpublic class SendComm2
{
static { System.setSecurityManager(null); }public static void main(String args[])
{
S_Frame2 S_win=new S_Frame2();S_win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
S_win.pack();
}
}