求助帖;RxtxFAX怎样通过调用传真服务器的串口COM1,COM2,通过电话线座机号发送传真?

求助帖;RxtxFAX怎样通过调用传真服务器的串口COM1,COM2,通过电话线座机号发送传真?

目前我通过引用fax4j的jar包,然后在服务器配置rxtx-2.2pre2的RXTXcomm.jar和rxtxSerial.dll文件

    net.sf.fax4jfax4j0.42.9

然后通过代码能够打开串口,连接串口,监听串口,但是不知道怎样通过串口,电话线,座机号发送传真

package com.hx.controller;/**//**@作者:*@时间:2019/8/5 15:23*@功能:*/import com.alibaba.fastjson.JSONObject;
import com.hx.common.PortName;
import com.oracle.jrockit.jfr.Producer;
import gnu.io.*;
import org.fax4j.FaxClient;
import org.fax4j.FaxClientFactory;
import org.fax4j.FaxJob;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.TooManyListenersException;
//串口2
@Controller
public class TestController2 implements SerialPortEventListener,ServletContextListener {private static SerialPort serialPort;private static BufferedInputStream inputStream;private static CommPortIdentifier commPort;private static String appName = "电话传真通讯测试";private static int timeout = 2000;// open 端口时的等待时间private BufferedOutputStream outputStream;private static void checkPort() {if (commPort == null){selectPort();}if (serialPort == null) {openPort();}}public static void selectPort() {commPort = null;CommPortIdentifier cpid;Enumeration en = CommPortIdentifier.getPortIdentifiers();while (en.hasMoreElements()) {cpid = (CommPortIdentifier) en.nextElement();if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL && cpid.getName().equals(PortName.PORTNAME2)) {commPort = cpid;break;}}}private static void openPort() {try {serialPort = (SerialPort) commPort.open(appName, timeout);System.out.println("实例 SerialPort 成功!");} catch (PortInUseException e) {System.out.println(e+"...端口"+commPort.getName()+"正在使用中!");}}@Overridepublic void serialEvent(SerialPortEvent serialPortEvent) {switch (serialPortEvent.getEventType()) {case SerialPortEvent.BI:/* Break interrupt,通讯中断 */case SerialPortEvent.OE:/* Overrun error,溢位错误 */case SerialPortEvent.FE:/* Framing error,传帧错误 */case SerialPortEvent.PE:/* Parity error,校验错误 */case SerialPortEvent.CD:/* Carrier detect,载波检测 */case SerialPortEvent.CTS:/* Clear to send,清除发送 */case SerialPortEvent.DSR:/* Data set ready,数据设备就绪 */case SerialPortEvent.RI:/* Ring indicator,响铃指示 */case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/** Output buffer is* empty,输出缓冲区清空*/break;case SerialPortEvent.DATA_AVAILABLE:/** Data available at the serial* port,端口有可用数据。读到缓冲数组,输出到终端*/byte[] readBuffer = new byte[1024];String readStr = "";String s2 = "";try {while (inputStream.available() > 0) {inputStream.read(readBuffer);readStr += new String(readBuffer).trim();}s2 = new String(readBuffer).trim();String portname=commPort.getName();System.out.println("接收到"+portname+"端口返回数据(长度为" + readStr.length() + "):" + readStr);System.out.println(s2+"22222222222222");} catch (IOException e) {}}}//监听端口接收数据@Overridepublic void contextInitialized(ServletContextEvent sce) {//选择接收数据的串口,然后监听该串口,并且从tomcat 启动开始就开始监听,并接收数据\//在开始监听时先检查是否能正确连接,没有的话就打开链接checkPort();String str="";try {serialPort.addEventListener(this);//设置当有数据到达时唤醒监听接收线程serialPort.notifyOnDataAvailable(true);//设置当通信中断时唤醒中断线程serialPort.notifyOnBreakInterrupt(true);inputStream = new BufferedInputStream(serialPort.getInputStream());} catch (IOException e) {System.out.println("获取端口的InputStream出错:" + e.getMessage());}catch (TooManyListenersException e) {System.out.println(e.getMessage());}}@Overridepublic void contextDestroyed(ServletContextEvent sce) {}@RequestMapping("write")@ResponseBodypublic String write() {String message="111111155555";checkPort();try {outputStream = new BufferedOutputStream( serialPort.getOutputStream() );outputStream.write( message.getBytes( "GBK" ), 0, message.getBytes( "GBK" ).length );System.out.println( "信息发送成功!" + new String( message.getBytes() ) );}catch (IOException e){System.out.println( "获取端口的OutputStream出错:" + e.getMessage() );}finally{try {outputStream.close();} catch (Exception e) {System.out.println( "关闭串口时出错:" + e.getMessage() );}}return JSONObject.toJSONString( "成功" );}}

求助,有大神吗?急..


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部