Java利用串口编程技术操作继电器(三)

2014-11-23 17:52:29 · 作者: · 浏览: 52
c final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RunServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); // String code = "O(00,20,1)E"; String address = request.getParameter("address"); // 地址 String state = request.getParameter("state"); // 状态 String bofile = request.getParameter("file"); // 视频地址 if (address != null && state != null) { runSerial(address, state, bofile); } else { address = "00"; state = "0"; runSerial(address, state, bofile); } System.out.println("bofile:" + bofile); } // 运行串口 public void runSerial(String address, String state, String bofile) { if (address != null && state != null) { String PORT_NAME = "COM1"; String code = "O(00," + address + "," + state + ")E"; DSerialPort sp = new DSerialPort(); sp.listPort(); sp.selectPort(PORT_NAME); sp.write(code); /* if (bofile != null) { */ if (state.equals("1")) { play(bofile); } /* } */ sp.startRead(1); } } // 播放视频 public void play(String path) { System.out.println("path:" + path); // String // potplayer="D:/The entertainment software/PotPlayer/PotPlayerMini64.exe"; String mediaPath = "C:/MPlayer_Windows/mplayer/MPlayer.exe"; // 文件路径 // 调用mplayer命令行 String cmd = " -vo directx identify wid String.valueOf(false) -colorkey 0x030303 -slave -osdlevel String.valueOf(1)" + " -fullscreen"; try { Process rn = Runtime.getRuntime().exec( mediaPath + " " + path + " " + cmd); // 在单独的进程中执行指定命令和变量 /* * Process rn = Runtime.getRuntime().exec( potplayer + " " + path); * // 在单独的进程中执行指定命令和变量 */ System.out.println("视频开始播放"); } catch (IOException e1) { e1.printStackTrace(); return; } } } 我这个是再加了一个播放视频的 说的很简单,要是有不懂的,我会的,可以来和我探讨哦,QQ在左上角