91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 6KB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發(fā)布日期: 2021-05-12
  • 語言: Java
  • 標(biāo)簽: Java??串口??RXTX??

資源簡介

Java 串口(rxtx)demo(包含發(fā)送、接收、以及連接數(shù)據(jù)庫和連接實際的串口硬件的代碼和工具類)

資源截圖

代碼片段和文件信息

package?com.example.demo.rxtx3;

import?com.example.demo.rxtx3.util.DBHelper;
import?gnu.io.*;


import?java.io.IOException;
import?java.io.InputStream;
import?java.sql.PreparedStatement;
import?java.util.TooManyListenersException;
?//寫好之后,就是讀串口類,要保存至數(shù)據(jù)庫的話就是在讀出數(shù)據(jù)后保存,此處無保存至數(shù)據(jù)庫的代碼:
public?class?EventListener?implements?SerialPortEventListener?{
????//1.定義變量
????SerialPort?serialPort?=?null;
????InputStream?inputStream?=?null;//輸入流
????Thread?readThread?=?null;
?????java.sql.Connection?conn?=?null;
?????PreparedStatement?stmt?=?null;
????//2.構(gòu)造函數(shù):
????//實現(xiàn)初始化動作:獲取串口COM21、打開串口、獲取串口輸入流對象、為串口添加事件監(jiān)聽對象

????public?EventListener()?throws?NoSuchPortException?PortInUseException?{
????????try?{
????????????//獲取串口、打開窗串口、獲取串口的輸入流。
????????????serialPort?=?SerialTool.openPort(“COM3“?9600);
????????????inputStream?=?serialPort.getInputStream();
????????????//向串口添加事件監(jiān)聽對象。
????????????serialPort.addEventListener(this);
????????????//設(shè)置當(dāng)端口有可用數(shù)據(jù)時觸發(fā)事件,此設(shè)置必不可少。
????????????serialPort.notifyondataavailable(true);
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}?catch?(TooManyListenersException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}finally?{
????????????//??SerialTool.closePort(serialPort);
????????}
????}

????//重寫繼承的監(jiān)聽器方法
????@Override
????public?void?serialEvent(SerialPortEvent?event)?{
????????//定義用于緩存讀入數(shù)據(jù)的數(shù)組
????????byte[]?cache?=?new?byte[1024];
????????//記錄已經(jīng)到達串口COM21且未被讀取的數(shù)據(jù)的字節(jié)(Byte)數(shù)。
????????int?availableBytes?=?0;

????????//如果是數(shù)據(jù)可用的時間發(fā)送,則進行數(shù)據(jù)的讀寫
????????if(event.getEventType()?==?SerialPortEvent.DATA_AVAILABLE){
????????????try?{
????????????????availableBytes?=?inputStream.available();
????????????????while(availableBytes?>?0){
????????????????????inputStream.read(cache);
????????????????????for(int?i?=?0;?i?????????????????????????//解碼并輸出數(shù)據(jù)
????????????????????????System.out.print((char)cache[i]);
????????????????????????//存儲到數(shù)據(jù)庫中
????????????????????????//使用的sql語句
????????????????????????String?sql?=?“insert?into?entity_test?(idstr)?values(1)“;
????????????????????????//獲得數(shù)據(jù)庫連接‘“+(char)cache[i]+“‘
????????????????????????try?{
????????????????????????????conn?=?DBHelper.getConnection();
????????????????????????????stmt?=?conn.prepareStatement(sql);
????????????????????????????stmt.executeUpdate();
????????????????????????}?catch?(Exception?e)?{

????????????????????????????e.printStackTrace();
????????????????????????}

????????????????????}
????????????????????availableBytes?=?inputStream.available();
????????????????}
????????????????System.out.println();
????????????}?catch?(IOException?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????}

????public?static?void?main(String[]?args)?throws?NoSuchPortException?PortInUseException?{

????????new?EventListener();

????}
????/*
?????如果是自己虛擬的兩個端口,則運行讀串口類,再運行寫串口類,則能看到接收到的數(shù)據(jù)。

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-05-21?17:45??rxtx3\
?????文件????????3689??2018-05-21?16:16??rxtx3\EventListener.java
?????文件????????6710??2018-05-18?14:26??rxtx3\SerialTool.java
?????目錄???????????0??2018-05-21?17:45??rxtx3\util\
?????文件????????1475??2018-05-21?09:37??rxtx3\util\DBHelper.java
?????文件????????1231??2018-05-21?17:23??rxtx3\util\HexUtil.java
?????文件?????????777??2018-05-21?17:44??rxtx3\Write.java

評論

共有 條評論