读写文件并用串口发送出去

读写本地文件并用串口发送,同时把GBK内码存入另一个文件中

 

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class COM_Send {
	private CommPortIdentifier portId = null;
	private SerialPort serialPort = null;
	private InputStream in = null;
	private OutputStream out = null;
	private boolean sendNext = true; // 收到响应发送下一条数据

	/**
	 * 串口连接
	 */
	private void connect() {
		try {
			portId = CommPortIdentifier.getPortIdentifier("COM1");
			serialPort = (SerialPort) portId.open("myCom", 1000);

			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
			in = serialPort.getInputStream();
			out = serialPort.getOutputStream();
		} catch (NoSuchPortException e) {
			e.printStackTrace();
		} catch (PortInUseException e) {
			e.printStackTrace();
		} catch (UnsupportedCommOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭串口
	 */
	private void close() {
		try {
			in.close();
			out.close();
			serialPort.close();
			portId = null;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 向文件及串口发送数据
	 * 
	 * @throws IOException
	 */
	private void writeOutput() throws IOException {
		InputStreamReader fromFile = new InputStreamReader(new FileInputStream(
				"c://123.txt"), "GBK");
		BufferedReader fromFileBuffer = new BufferedReader(fromFile);
		String s;
		File file = new File("c://456.txt");
		if (!file.exists()) {
			file.createNewFile();
		}
		OutputStreamWriter toFile = new OutputStreamWriter(
				new FileOutputStream(file), "GBK");
		OutputStreamWriter toCom = new OutputStreamWriter(out, "GBK");

		while ((s = fromFileBuffer.readLine()) != null && sendNext) {
			System.out.println(s);

			byte b[] = s.getBytes("GBK");
			for (int i = 0; i < b.length;) {
				// 第一个字节
				int firstByte = b[i] & 0xFF;
				// 转换成16进制
				String str = Integer.toHexString(firstByte).toUpperCase();
				if (str.length() < 2) {
					str += "0";
				}
				if (firstByte >= 0xB0 && firstByte <= 0xF7) { // 高字节在GBK内码范围内
					int secondByte = b[i + 1] & 0xFF;
					if (secondByte >= 0xA1 && secondByte <= 0xFE) { // 低字节在GBK内码范围内
						String secondStr = Integer.toHexString(secondByte)
								.toUpperCase();
						if (str.length() < 2) {
							secondStr += "0";
						}
						str += secondStr;
						i++;
					}
				}
				// 以16进制方式将汉字写入另一个文件,汉字为两个字节,其他字符为一个字节
				toFile.write(str);
				toFile.write(" ");
				i++;
			}
			toFile.write("\r\n");

			// 将文件写入到串口
			toCom.write(s);
			toCom.write("\r\n");
			toCom.flush();

			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}

			// 等待正确返回“ok”再发下一行数据,否则认为通讯失败
			int i = 0; // 超时的次数
			while (i < 5) {
				if (in.available() == 2) { // 返回“ok”
					if (in.available() >= 2) {// 大于等于2个字节
						byte[] ok = new byte[2];
						in.read(ok, 0, 2);
						if (ok[0] == 0x6F && ok[1] == 0x6B) { // 等于 ok
							System.out.println("Success!");
							break;
						}
					}

				}
				i++;
				try {
					// Thread.sleep(50);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(i);
			if (i == 5) {
				System.out.println("超时!");
				sendNext = false;
			}
		}

		if (sendNext) {
			System.out.println("发送成功!");
		}

		fromFileBuffer.close();
		fromFile.close();
		toFile.close();
		toCom.close();
		close();
	}

	public static void main(String args[]) throws IOException {
		COM_Send cs = new COM_Send();
		cs.connect();
		cs.writeOutput();
		cs.close();
	}
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值