導航:首頁 > 編程語言 > 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 瀏覽:285
python編譯器小程序 瀏覽:944
在app上看視頻怎麼光線調暗 瀏覽:540
可以中文解壓的解壓軟體 瀏覽:593
安卓卸載組件應用怎麼安裝 瀏覽:913
使用面向對象編程的方式 瀏覽:339
程序員項目經理的年終總結範文 瀏覽:929
內衣的加密設計用來幹嘛的 瀏覽:432
淮安數據加密 瀏覽:292
魔高一丈指標源碼 瀏覽:982
松下php研究所 瀏覽:168
c回調java 瀏覽:399
夢幻端游長安地圖互通源碼 瀏覽:745
電腦本地文件如何上傳伺服器 瀏覽:313