亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

java 串口編程

系統(tǒng) 1787 0

通過(guò)JAVA與串口(RS232)通信實(shí)例

文章分類: Java編程 關(guān)鍵字: java com 串口 rs232

最近了解到的需求是需要需激光打刻機(jī)進(jìn)行(RS232)串口通信,
這里使用的是RXTX開(kāi)源包實(shí)現(xiàn)的。
之前并沒(méi)有用java做過(guò)串口通信,而且這方面資料不是很多。
項(xiàng)目實(shí)際應(yīng)用中可能會(huì)采用VB開(kāi)發(fā)(這個(gè)我就不會(huì)了)

只不過(guò)用java嘗試一下,記個(gè)筆記,希望可以對(duì)相關(guān)開(kāi)發(fā)用些幫助。

下面是實(shí)現(xiàn)代碼
Java代碼 復(fù)制代碼
  1. package ?test; ??
  2. ??
  3. import ?java.io.IOException; ??
  4. import ?java.io.InputStream; ??
  5. import ?java.io.InputStreamReader; ??
  6. import ?java.io.OutputStream; ??
  7. import ?java.util.Date; ??
  8. import ?java.util.Enumeration; ??
  9. import ?java.util.TooManyListenersException; ??
  10. ??
  11. import ?gnu.io.CommPortIdentifier; ??
  12. import ?gnu.io.PortInUseException; ??
  13. import ?gnu.io.SerialPort; ??
  14. import ?gnu.io.SerialPortEvent; ??
  15. import ?gnu.io.SerialPortEventListener; ??
  16. import ?gnu.io.UnsupportedCommOperationException; ??
  17. ??
  18. public ? class ?CommUtil? implements ?SerialPortEventListener?{ ??
  19. ??
  20. ????InputStream?inputStream;? //?從串口來(lái)的輸入流 ??
  21. ????OutputStream?outputStream; //?向串口輸出的流 ??
  22. ????SerialPort?serialPort;? //?串口的引用 ??
  23. ????CommPortIdentifier?portId; ??
  24. ??
  25. ???? public ?CommUtil(Enumeration?portList,?String?name)?{ ??
  26. ???????? while ?(portList.hasMoreElements())?{ ??
  27. ????????????CommPortIdentifier?temp?=?(CommPortIdentifier)?portList.nextElement(); ??
  28. ???????????? if ?(temp.getPortType()?==?CommPortIdentifier.PORT_SERIAL)?{ //?判斷如果端口類型是串口 ??
  29. ???????????????? if ?(temp.getName().equals(name))?{? //?判斷如果端口已經(jīng)啟動(dòng)就連接 ??
  30. ????????????????????portId?=?temp; ??
  31. ????????????????} ??
  32. ????????????} ??
  33. ????????} ??
  34. ???????? try ?{ ??
  35. ????????????serialPort?=?(SerialPort)?portId.open( "My" +name,? 2000 ); ??
  36. ????????}? catch ?(PortInUseException?e)?{ ??
  37. ??
  38. ????????} ??
  39. ???????? try ?{ ??
  40. ????????????inputStream?=?serialPort.getInputStream(); ??
  41. ????????????outputStream?=?serialPort.getOutputStream(); ??
  42. ????????}? catch ?(IOException?e)?{ ??
  43. ????????} ??
  44. ???????? try ?{ ??
  45. ????????????serialPort.addEventListener( this );? //?給當(dāng)前串口天加一個(gè)監(jiān)聽(tīng)器 ??
  46. ????????}? catch ?(TooManyListenersException?e)?{ ??
  47. ????????} ??
  48. ????????serialPort.notifyOnDataAvailable( true );? //?當(dāng)有數(shù)據(jù)時(shí)通知 ??
  49. ???????? try ?{ ??
  50. ????????????serialPort.setSerialPortParams( 2400 ,?SerialPort.DATABITS_8,? //?設(shè)置串口讀寫(xiě)參數(shù) ??
  51. ????????????????????SerialPort.STOPBITS_1,?SerialPort.PARITY_NONE); ??
  52. ????????}? catch ?(UnsupportedCommOperationException?e)?{ ??
  53. ????????} ??
  54. ????} ??
  55. ??
  56. ???? public ? void ?serialEvent(SerialPortEvent?event)?{ ??
  57. ???????? switch ?(event.getEventType())?{ ??
  58. ???????? case ?SerialPortEvent.BI: ??
  59. ???????? case ?SerialPortEvent.OE: ??
  60. ???????? case ?SerialPortEvent.FE: ??
  61. ???????? case ?SerialPortEvent.PE: ??
  62. ???????? case ?SerialPortEvent.CD: ??
  63. ???????? case ?SerialPortEvent.CTS: ??
  64. ???????? case ?SerialPortEvent.DSR: ??
  65. ???????? case ?SerialPortEvent.RI: ??
  66. ???????? case ?SerialPortEvent.OUTPUT_BUFFER_EMPTY: ??
  67. ???????????? break ; ??
  68. ???????? ??
  69. ???????? case ?SerialPortEvent.DATA_AVAILABLE: //?當(dāng)有可用數(shù)據(jù)時(shí)讀取數(shù)據(jù),并且給串口返回?cái)?shù)據(jù) ??
  70. ???????????? byte []?readBuffer?=? new ? byte [ 20 ]; ??
  71. ??
  72. ???????????? try ?{ ??
  73. ???????????????? while ?(inputStream.available()?>? 0 )?{ ??
  74. ????????????????????System.out.println(inputStream.available()); ??
  75. ???????????????????? int ?numBytes?=?inputStream.read(readBuffer); ??
  76. ????????????????????System.out.println(numBytes); ??
  77. ????????????????} ??
  78. ????????????????System.out.println( new ?String(readBuffer).trim()); ??
  79. ????????????}? catch ?(IOException?e)?{ ??
  80. ????????????????e.printStackTrace(); ??
  81. ????????????} ??
  82. ???????????? break ; ??
  83. ????????} ??
  84. ????} ??
  85. ???? public ? void ?send(String?content){ ??
  86. ???????? try ?{ ??
  87. ????????????outputStream.write(content.getBytes()); ??
  88. ????????}? catch ?(IOException?e)?{ ??
  89. ????????????e.printStackTrace(); ??
  90. ????????} ??
  91. ????} ??
  92. ???? ??
  93. ???? public ? void ?ClosePort()?{ ??
  94. ???????? if ?(serialPort?!=? null )?{ ??
  95. ??????????serialPort.close(); ??
  96. ????????} ??
  97. ??????} ??
  98. ??
  99. ???? ??
  100. }??
      package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;

public class CommUtil implements SerialPortEventListener {

	InputStream inputStream; // 從串口來(lái)的輸入流
	OutputStream outputStream;// 向串口輸出的流
	SerialPort serialPort; // 串口的引用
	CommPortIdentifier portId;

	public CommUtil(Enumeration portList, String name) {
		while (portList.hasMoreElements()) {
			CommPortIdentifier temp = (CommPortIdentifier) portList.nextElement();
			if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判斷如果端口類型是串口
				if (temp.getName().equals(name)) { // 判斷如果端口已經(jīng)啟動(dòng)就連接
					portId = temp;
				}
			}
		}
		try {
			serialPort = (SerialPort) portId.open("My"+name, 2000);
		} catch (PortInUseException e) {

		}
		try {
			inputStream = serialPort.getInputStream();
			outputStream = serialPort.getOutputStream();
		} catch (IOException e) {
		}
		try {
			serialPort.addEventListener(this); // 給當(dāng)前串口天加一個(gè)監(jiān)聽(tīng)器
		} catch (TooManyListenersException e) {
		}
		serialPort.notifyOnDataAvailable(true); // 當(dāng)有數(shù)據(jù)時(shí)通知
		try {
			serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, // 設(shè)置串口讀寫(xiě)參數(shù)
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {
		}
	}

	public void serialEvent(SerialPortEvent event) {
		switch (event.getEventType()) {
		case SerialPortEvent.BI:
		case SerialPortEvent.OE:
		case SerialPortEvent.FE:
		case SerialPortEvent.PE:
		case SerialPortEvent.CD:
		case SerialPortEvent.CTS:
		case SerialPortEvent.DSR:
		case SerialPortEvent.RI:
		case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
			break;
		
		case SerialPortEvent.DATA_AVAILABLE:// 當(dāng)有可用數(shù)據(jù)時(shí)讀取數(shù)據(jù),并且給串口返回?cái)?shù)據(jù)
			byte[] readBuffer = new byte[20];

			try {
				while (inputStream.available() > 0) {
					System.out.println(inputStream.available());
					int numBytes = inputStream.read(readBuffer);
					System.out.println(numBytes);
				}
				System.out.println(new String(readBuffer).trim());
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		}
	}
	public void send(String content){
		try {
			outputStream.write(content.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void ClosePort() {
	    if (serialPort != null) {
	      serialPort.close();
	    }
	  }

	
}


    


測(cè)試
Java代碼 復(fù)制代碼
  1. package ?test; ??
  2. ??
  3. import ?gnu.io.CommPortIdentifier; ??
  4. ??
  5. import ?java.util.Enumeration; ??
  6. ??
  7. public ? class ?Test?{ ??
  8. ??
  9. ???? public ? static ? void ?main(String[]?args)? throws ?InterruptedException?{ ??
  10. ????????Enumeration?portList?=?CommPortIdentifier.getPortIdentifiers();? //得到當(dāng)前連接上的端口 ??
  11. ???????? ??
  12. ????????CommUtil?comm3?=? new ?CommUtil(portList, "COM3" ); ??
  13. ???????? int ?i?=? 0 ; ??
  14. ???????? while (i< 5 ) ??
  15. ????????{ ??
  16. ????????????Thread.sleep( 3000 ); ??
  17. ????????????comm3.send( "hello" ); ??
  18. ????????????i++; ??
  19. ????????} ??
  20. ????????comm3.ClosePort(); ??
  21. ????} ??
  22. ??
  23. }??
      package test;

import gnu.io.CommPortIdentifier;

import java.util.Enumeration;

public class Test {

	public static void main(String[] args) throws InterruptedException {
		Enumeration portList = CommPortIdentifier.getPortIdentifiers(); //得到當(dāng)前連接上的端口
		
		CommUtil comm3 = new CommUtil(portList,"COM3");
		int i = 0;
		while(i<5)
		{
			Thread.sleep(3000);
			comm3.send("hello");
			i++;
		}
		comm3.ClosePort();
	}

}

    

?

java 串口編程


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 久久精品国产大片免费观看 | 一级片免费看 | 免费国产成人午夜私人影视 | 日韩久久精品视频 | 在线免费黄色网址 | 日本一区二区三区在线观看 | 亚洲日韩精品欧美一区二区一 | 一级高清在线观看影片 | 鲁一鲁中文字幕久久 | 欧洲做视频在线观看 | 人人爱人人做 | 九九热观看视频 | 亚洲精品乱码久久久久久 | 91精品国产91久久久久 | 青青青视频精品中文字幕 | 亚洲欧洲国产精品久久 | 亚洲国产成人99精品激情在线 | 亚洲天码中文字幕第一页 | 亚洲国产天堂久久精品网 | 国产色资源| 日韩毛片在线 | 久久亚洲精品国产亚洲老地址 | 久久精品94精品久久精品 | 好吊色青青草 | 欧美成人h精品网站 | 国产尤物在线视频 | 欧美香蕉爽爽人人爽观看猫咪 | 欧美久草视频 | 欧美日韩精品一区二区三区四区 | 国产精品亚洲综合第一区 | 综合好色| 欧美e片成 人 在线播放乱妇 | 欧美成人免费高清网站 | 亚洲国产精品成人午夜在线观看 | 日本高清一道本 | 国产精品久久免费视频 | 亚洲免费久久 | 米奇7777狠狠狠狠视频影院 | 国产色婷婷精品综合在线 | 久久久噜噜噜久噜久久 | 欧美另类性视频在线看 |