导航:首页 > 编程语言 > java串口通信编程

java串口通信编程

发布时间:2022-03-14 18:17:31

① 关于使用java实现rs485串口通信的问题

comm.jar
通过串口,PC机可以获取设备的输入数据,也可以让PC机向设备输出输出。你的设备应该有相关的程序与说明。

② 我现在要做Java串口通讯 我把代码做好之后 怎么运行才能通讯呢

什么异常呢?在A机上运行一个串口发送程序,向A上的COMA发送数据,B上运行一个接收的程序,接收B机上的COMB的数据,当然,要用串口线一端链A的COMA,一端连B的COMB。

③ java串口读写问题 写了一个串口通信程序,结果得到的不是我想要的结果。本来我是想输入hello。得到的也是

System.out.println(new String(readBuffer).trim()); 改为
System.out.print(new String(readBuffer).trim()); 就应该是你要的结果了把。
仅供参考。

④ 求一个用java写的串口通信软件,界面如图所示

界面你就自己写吧,串口通讯就使用rxtx,很方便的

⑤ 我想做Java串口通讯编程 谁能帮我介绍点资料!小弟跪谢!

你可以参考一下这篇文章写得挺好的,再发给你一个串口的程序参考一下吧 http://blog.csdn.net/luoyu/archive/2008/03/14/2182321.aspx package cc.dfsoft.ranqi.util;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import javax.comm.CommPortIdentifier;import javax.comm.NoSuchPortException;import javax.comm.PortInUseException;import javax.comm.SerialPort;import javax.comm.;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import cc.dfsoft.ranqi.bo.MachineCommand;import cc.dfsoft.ranqi.service.bean.gz.zngz.Command10Mgr;import cc.dfsoft.ranqi.service.bean.gz.zngz.impl.Command10;public class SerialBean{protected Log logger = LogFactory.getLog("serialBean");private String PortName;CommPortIdentifier portId;private SerialPort serialPort;private OutputStream out;private InputStream in;static int waitTime = 0;public SerialBean(int PortID) {PortName = "COM" + PortID;if(PortName.equalsIgnoreCase("com6")){logger = LogFactory.getLog("singal");}}/*** 新修改的接口,直接传入串口名称* @param PortID*/public SerialBean(String PortID) {PortName = PortID;if(PortName.equalsIgnoreCase("com6")){logger = LogFactory.getLog("singal");}}/**** This function initialize the serial port for communication. It startss a* thread which consistently monitors the serial port. Any signal capturred* from the serial port is stored into a buffer area.* 本函数初始化所指定的串口并返回初始化结果。* 如果初始化成功返回1,否则返回-1。* 初始化的结果是该串口被SerialBean独占性使用,其参数被设置为9600, N, 8, 1。* 如果串口被成功初始化,则打开一个进程读取从串口传入的数据并将其保存在缓冲区中。**/public synchronized int initialize() {int InitSuccess = 1;int InitFail = -1;try {portId = CommPortIdentifier.getPortIdentifier(PortName);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");try {serialPort = (SerialPort) portId.open("Serial_Communication"+PortName,3600);} catch (PortInUseException e) {e.printStackTrace();return InitFail;}//Use InputStream in to read from the serial port, and OutputStream//out to write to the serial port.InitFail = initStream();//Initialize the communication parameters to 9600, 8, 1, none.try {serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);} catch ( e) {logger.info(e.getMessage());e.printStackTrace();return InitFail;}} catch (NoSuchPortException e) {logger.info(e.getMessage());e.printStackTrace();return InitFail;}return InitSuccess;}private int initStream() {try {in = serialPort.getInputStream();out = serialPort.getOutputStream();} catch (IOException e) {logger.info(e.getMessage());e.printStackTrace();return -1;}return 1;}/*** This function returns a string with a certain length from the incomin messages.* 本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。*/public synchronized byte[] readPort(byte[] aByte) {//logger.fatal("线程---串口:" + PortName + " 主机:" + aByte[0] + " 命令:" + aByte[1] + " 进入readPort方法");//initialize();//initStream();PropertyUtil proUtil = new PropertyUtil();waitTime = Integer.parseInt(proUtil.getCom("waitTime"));//读取配置文件中设置的等待时间byte[] bf = new byte[100];//供接收使用byte[] content = new byte[0];//update in 20070918 by liulei ,锁当前资源try {for (int i = 0; i < aByte.length; i++) {out.write(aByte[i]);}out.flush();//Thread.sleep(waitTime);int j = 0;while(in.available() == 0){Thread.sleep(waitTime);if(j > 2){logger.fatal("跳出循环,当前j== " + j + "无法读到字节数组; 串口:" + PortName + ",主机:" + aByte[0] + " ,命令:" + aByte[1]);//update by liulei in 2007.10.20,关闭串口同时关闭输入输出流//in.close();//out.close();//closePort();return content;}j++;}logger.info(PortName +":读到的字节数组长度----------------" + in.available());} catch (Exception e) {logger.info(e.getMessage());e.printStackTrace();}//update in 20070918 by liulei ,锁当前资源try{//logger.info("读到的字节数组长度:=== " + in.available());while(in.available() > 0){int num = in.read(bf);//从输入流中读取一定数量的字节并将其存储在缓冲区数组bf中content = new byte[num];for(int k = 0;k < num;k++){content[k] = bf[k];}}} catch (Exception e) {logger.info(e.getMessage());e.printStackTrace();}//logger.info("得到值,关闭串口正常退出!");//update by liulei in 2007.10.20,关闭串口同时关闭输入输出流/*try {in.close();out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}closePort();*/StringBuffer printResult = new StringBuffer();if(aByte.length > 0){for (int i = 0; i < aByte.length; i++) {printResult.append(aByte[i]);printResult.append(",");}}logger.error("向串口: " + PortName + " 发送指令 : " + printResult.toString());StringBuffer printResult1 = new StringBuffer();if(content.length > 0){for (int i = 0; i < content.length; i++) {printResult1.append(content[i]);printResult1.append(",");}}logger.error("从串口: " + PortName + " 返回值 : " + printResult1.toString());//判断10指令的处理结果if(aByte[1] == 16 && printResult1.toString().trim().length() == 9){if(aByte[2] == 0){logger.info("校验不成功......");}if(aByte[2] == 1){logger.info("校验成功......");}}//判断10指令的处理结果if(aByte[1] == 5 && printResult1.toString().trim().length() == 9){logger.info("校验不成功......");}if(aByte.length > 0 && content.length > 0){if(aByte[0] != content[0] || aByte[1] != content[1]){//logger.error("当前线程:" + Thread.currentThread().getName() + "从串口名称: " + PortName + "得到值不一致,进行拦截处理");return new byte[0];}}return content;}/*** This function sends a message through the serial port.* @param aByte The data of byte to be sent.* 本函数向串口发送一个字符串。参数Msg是需要发送的字符串。*/public void writePort(byte[] aByte) {PropertyUtil proUtil = new PropertyUtil();waitTime = Integer.parseInt(proUtil.getCom("waitTime"));//读取配置文件中设置的等待时间//update in 20070918 by liulei ,锁当前资源try {for (int i = 0; i < aByte.length; i++) {out.write(aByte[i]);}Thread.sleep(waitTime);//closePort();//initialize();} catch (Exception e) {e.printStackTrace();}}/**** This function closes the serial port in use.* 本函数停止串口检测进程并关闭串口。*/public void closePort() {//RT.stop();serialPort.close();}}

⑥ 用java做串口通信,配置后,执行显示串口信息程序,出现

HKEY_LOCAL_MACHINE ???

⑦ java串口通信,如何实现用同一个串口实现发送数据和接收数据

虚拟串口吧,直接的同串口貌似是不能同时接收和发送的吧,除非全双工的

等等,如果说按照全双工方式来实现的话,理论上可以的,我去帮你找找资料

恩,资料地址在附件里

⑧ 我用java代码写串口通信,串口测试通过了!串口发送给程序的命令,如何用程序回复返回的结果

……你自己把逻辑锁死了。收到一回4字节序列后,flag1置位,之后发送数据。可是在发送数据的时候又会进入中断的,由于接收缓冲区中还是那4字节的序列,于是flag1再次被置位……于是乎进入永不停歇的死循环。你的串口中断应当判断一下是发送还是接收……

⑨ 在java的web程序中怎么使用串口通讯

方法如下:

  1. 新建eclipse工程,添加comm.jar或者RXTXcomm.jar包。因为javacomm20-win32.zip包里有样例SimpleRead.java,可以通过这个例子测试串口是否正确。

  2. 接收数据正确后,根据传送接收双方的协议,采用CRC循环校验,根据传输的一方的校验函数判定是否是正确传输。

  3. 把正确结束的数据解析,查看自己指定的通讯规则,然后解析。

  4. 插入数据库,jdbc插入。

  5. 数据统计,定时统计每小时,每天,每月,每年的平均值,采用quartz服务来实现。

  6. 建立web工程,采用hibernate3,spring3,dwr技术把数据库数据动态显示,图表采用jfreechart,以及AJAX的运用

⑩ java实现串口通信代码

public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称
{
if(portId.getName().equals("COM1")){//如果是COM1端口则退出循环
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打开串口的超时时间为1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无
InputStream in = serialPort.getInputStream();//得到输入流
OutputStream out = serialPort.getOutputStream();//得到输出流

//进行输入输出操作

//操作结束后
in.close();
out.close();
serialPort.close();//关闭串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

阅读全文

与java串口通信编程相关的资料

热点内容
优信二手车解压后过户 浏览:62
Windows常用c编译器 浏览:778
关于改善国家网络安全的行政命令 浏览:833
安卓如何下载网易荒野pc服 浏览:654
javainetaddress 浏览:104
苹果4s固件下载完了怎么解压 浏览:1003
命令zpa 浏览:286
python编译器小程序 浏览:945
在app上看视频怎么光线调暗 浏览:540
可以中文解压的解压软件 浏览:593
安卓卸载组件应用怎么安装 浏览:913
使用面向对象编程的方式 浏览:339
程序员项目经理的年终总结范文 浏览:929
内衣的加密设计用来干嘛的 浏览:433
淮安数据加密 浏览:292
魔高一丈指标源码 浏览:982
松下php研究所 浏览:168
c回调java 浏览:401
梦幻端游长安地图互通源码 浏览:746
电脑本地文件如何上传服务器 浏览:313