十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
public static void process() {
站在用戶的角度思考問題,與客戶深入溝通,找到阜南網(wǎng)站設(shè)計與阜南網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋阜南地區(qū)。
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端口則退出循環(huán)
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);//設(shè)置串口速率為9600,數(shù)據(jù)位8位,停止位1們,奇偶校驗無
InputStream in = serialPort.getInputStream();//得到輸入流
OutputStream out = serialPort.getOutputStream();//得到輸出流
//進(jìn)行輸入輸出操作
//操作結(jié)束后
in.close();
out.close();
serialPort.close();//關(guān)閉串口
} catch (PortInUseException e) {
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我有,我之前就是做串口通信的,SerialPort
using system.io.port;
SerialPort port = new SerialPort();
string []portName = SerialPort.GetPortName();//獲取串口名數(shù)組
port.PortName = portName[0];
//在這里添加設(shè)置串口的一些屬性,例如波特率等等
if(!port.IsOpen)
{
try{
port.Open();
}catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
界面部分我到時再發(fā)給你
Comm.Output=字符串或byte
如果是可見字符,則可以直接輸出字符串,如Comm.Output="hello"
不然得用byte(數(shù)組),如
Comm.CommPort = 3 '...使用Com3口
Comm.Settings = "57600,n,8,1" '對串口通訊的相關(guān)參數(shù)。包括串口通訊的比特率,奇偶校驗,數(shù)據(jù)位長度、停止位等。其默認(rèn)值 是“9600,N,8,1”,表示串口比特率是9600bit/s,不作奇偶校驗,8位數(shù)據(jù)位,1個停止位。
Comm.OutBufferSize = 1024
If Comm.PortOpen = False Then
Comm.PortOpen = True '...打開串口
End If
Comm.OutBufferCount = 0 '...清空輸出寄存器
Dim buffer(6) as Byte
buffer(0) = 255
buffer(1) = 1
buffer(2) = 0
buffer(3) = 0
buffer(4) = 0
buffer(5) = 0
buffer(6) = 1
Comm.Output = buffer
Comm.PortOpen = False
上面確實是VB的代碼。
在VBS中,沒有類型,所以聲明數(shù)組與初始化可能為:
Dim buffer(6)
buffer(0) = CByte(255)
...
我沒試過,不一定正確喲。
CommPortIdentifier serialPortId = CommPortIdentifier.getPortIdentifier(“COM2”);
SerialPort port = (SerialPort) serialPortId.open(“Read”, 30); //打開COM2串口,其中30是打開串口的超時時間
port.setSerialPortParams(1200, 8, 1, 0); //設(shè)置COM2的波特率,數(shù)據(jù)位,停止位,校驗方式
//從串口中得到輸入輸出流了
OutputStream out = port.getOutputStream();
out .write(buffer);
out .flush();
InputStream in = port.getInputStream();
in.read(data); //data是一個byte[]
最近在做java串口通訊,主要是用個人電腦通過串口從RS485讀取數(shù)據(jù),并通過crc循環(huán)冗余校驗,把接收正確的數(shù)據(jù)解析,插入數(shù)據(jù)庫mysql,并用SSH技術(shù)把數(shù)據(jù)庫數(shù)據(jù)以表格以及圖表形式顯示 \x0d\x0a 思路: \x0d\x0a1.為了從RS485讀取數(shù)據(jù),由于暫時沒有硬件設(shè)備,系統(tǒng)是win7,故采用Virtual Serial Port Drive(VSPD)這塊虛擬串口軟件代替。并下載sscom32.exe模擬串口通信軟件。 \x0d\x0a\x0d\x0a2. 要想實現(xiàn)串口通信,用Java實現(xiàn)串口通信(windows系統(tǒng)下),需要用到sun提供的串javacomm20-win32.zip。其中要用到三個文件,配置如下: \x0d\x0acomm.jar放置到 JAVA_HOME/jre/lib/ext; \x0d\x0awin32com.dll放置到 JAVA_HOME/bin; \x0d\x0ajavax.comm.properties 兩個地方都要放 \x0d\x0ajre/lib(也就是在JAVA文件夾下的jre),JAVA_HOME/jre/lib下 \x0d\x0a這個配置在我電腦上測試成功,也許不需要這樣麻煩。注意的是,如果你使用myeclipse,因為它自帶jre,你需要在它所在的jre相應(yīng)位置放dll以及properties文件。 \x0d\x0a\x0d\x0a是不是感覺這個很麻煩,還有windows的限制。后來我們下載rxtx這款開源包代替了剛才的comm。不僅windows下可以,linux下也可以。使用方法很簡單,配置如下: \x0d\x0a\x0d\x0aRXTXcomm.jar放到JAVA_HOME/jre/lib/ext \x0d\x0arxtxSerial.dll放到JAVA_HOME/bin \x0d\x0a如果你使用myeclipse工具,你需要把rxtxSerial.dll放到它自帶的jre里。 \x0d\x0a\x0d\x0a3.新建eclipse工程,添加comm.jar或者RXTXcomm.jar包。因為javacomm20-win32.zip包里有樣例SimpleRead.java,可以通過這個例子測試串口是否正確 \x0d\x0a\x0d\x0a4.接收數(shù)據(jù)正確后,根據(jù)傳送接收雙方的協(xié)議,采用CRC循環(huán)校驗,根據(jù)傳輸?shù)囊环降男r灪瘮?shù)判定是否是正確傳輸 \x0d\x0a\x0d\x0a5.把正確結(jié)束的數(shù)據(jù)解析,查看自己指定的通訊規(guī)則,然后解析 \x0d\x0a\x0d\x0a6.插入數(shù)據(jù)庫,jdbc插入 \x0d\x0a\x0d\x0a7.數(shù)據(jù)統(tǒng)計,定時統(tǒng)計每小時,每天,每月,每年的平均值,采用quartz服務(wù)來實現(xiàn)。 \x0d\x0a\x0d\x0a8.建立web工程,采用hibernate3,spring3,dwr技術(shù)把數(shù)據(jù)庫數(shù)據(jù)動態(tài)顯示,圖表采用jfreechart,以及AJAX的運用