package com; import java.io.*; import java.net.Socket; import java.util.*; import javax.comm.*; /** * * Mar 26, 2010 * @author zcj * * 普通模式和VIP模式下的:打开投影仪【通过串口、通过plc】 */ public class Projector { static Enumeration portList; static CommPortIdentifier portId; static SerialPort serialPort; static OutputStream outputStream; /** * 通过串口打开投影机【投影机和pc主机连接在一起】 * @throws Exception */ public static void openProjectorBySerial(String command) { try { /* 获得CommPortIdentifier对象的Enumeration对象 */ portList = CommPortIdentifier.getPortIdentifiers(); byte[] bt = new byte[4]; bt[0] = (byte)0x43; bt[1] = (byte)0x30; bt[2] = (byte)0x30; //开启投影仪【0x30】,关闭投影仪【0x31】 bt[3] = (byte)0x0D; /* 迭代此枚举对象*/ while (portList.hasMoreElements()) { /* 获得一个CommPortIdentifier对象 */ portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM1")) { //打开通讯 serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000); serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //打开输出流,写入命令 outputStream = serialPort.getOutputStream(); outputStream.write(bt); outputStream.flush(); outputStream.close(); serialPort.close(); } } } }catch (Exception e) { System.out.println("earth2 Projector 62 通过串口打开投影机时异常出现:"+e.getMessage()); } finally{ //关闭流和端口 try { if(outputStream != null){ outputStream.close(); } if(serialPort != null){ serialPort.close(); } }catch(Exception e){ System.out.println("earth2 Projector 76 关闭端口时异常出现:"+e.getMessage()); } } } /** * 通过服务器打开投影机【投影机不是和pc主机连在一起,而是与单板机连接在一起】 */ public static void openProjectorByServer() { Socket socket = null; PrintWriter os = null; try { socket=new Socket("remoteIP",20681); os=new PrintWriter(socket.getOutputStream()); os.write("$00-001,50"); //打开投影仪 // os.write("$00-001,51"); //关闭投影仪 os.flush(); os.close(); // 关闭Socket输出流 socket.close(); // 关闭Socket } catch (Exception e) { System.out.println("earth2 Projector 106 通过与单板机打开投影机异常:"+e.getMessage()); } finally { try { if(os != null){ os.close(); } if(socket != null){ socket.close(); } } catch (Exception e) { System.out.println("earth2 Projector 117 关闭Socket对象时异常..."+e.getMessage()); } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。