本篇主要讲述使用Java对串口进行读取和发送操作
准备
在项目中导入第三方Jar包

 
 Jar包已经在资源中绑定,或者去官网上自行下载jSerialComm
 注意当前jar包是配合JDK1.8环境使用,如果是1.8以下程序将直接中断
安装虚拟串口的软件
Configure Virtual Serial Port Driver:可以在没有物理意义上的设备时,虚拟出来两个串口,免费使用14天
  sscom5.13.1可以通过端口进行发送数据和接收数据的软件
sscom5.13.1可以通过端口进行发送数据和接收数据的软件
 设置发送端,端口使用COM2

 同样的设置第二个页面接收页面,接收端设置为COM1

端口连接是否正常

 
测试通过
测试通过以后再进行代码的编写,不然不利于排除错误。
Java代码读取串口数据
- Java代码接收串口传输的数据
package dzz.com;
import com.fazecast.jSerialComm.SerialPort;  
import com.fazecast.jSerialComm.SerialPortDataListener;  
import com.fazecast.jSerialComm.SerialPortEvent;  public class SerialPortReaderData {  private SerialPort serialPort;  public void openAndReadSerialPort(String portName) {  // 查找并打开串口  serialPort = SerialPort.getCommPort(portName);  if (serialPort.openPort()) {  System.out.println("Port is open: " + portName);  // 设置串口参数(这些参数应该与你的设备相匹配)  serialPort.setBaudRate(9600);  serialPort.setNumDataBits(8);  serialPort.setNumStopBits(SerialPort.ONE_STOP_BIT);serialPort.setParity(SerialPort.ODD_PARITY);try {//休眠,因为防止过度的循环Thread.sleep(1000L);} catch (InterruptedException e) {closeSerialPort();}boolean flag=false;StringBuffer sb= new StringBuffer();while(serialPort.isOpen()) {//获取传输过来的数据长度int bytesAvailable = serialPort.bytesAvailable();
//            	System.out.println(bytesAvailable);//判断端口是否传输数据if(bytesAvailable!=-1&&bytesAvailable!=0) {byte[] newData = new byte[bytesAvailable]; //读取端口数据 int numRead = serialPort.readBytes(newData, newData.length);  //解析成字符串并拼接sb.append(new String(newData, 0, numRead));flag=true;}//读取完成跳出循环if(flag&&(bytesAvailable==-1||bytesAvailable==0)) {break;}}System.out.println(sb.toString());closeSerialPort();}}  //关闭端口释放资源private void closeSerialPort() {  //判断是否已经关闭了端口if (serialPort != null && serialPort.isOpen()) {  //移除端口的监听serialPort.removeDataListener();  //关闭端口serialPort.closePort();  System.out.println("Port closed.");  }  }  public static void main(String[] args) {  SerialPortReaderData reader = new SerialPortReaderData();  reader.openAndReadSerialPort("COM1"); // 替换为你的串口名  // 可以在这里添加其他逻辑,比如等待用户输入来退出程序等  // 注意:在实际应用中,你可能需要有一种机制来确保程序不会无限期地运行下去  // 例如,你可以设置一个超时时间或等待用户输入来关闭程序  }  
}
- Java通过端口发送数据
package dzz.com;
import com.fazecast.jSerialComm.SerialPort;  public class SerialSender {  public static void main(String[] args) {  // 列出所有可用的串口  SerialPort[] comPorts = SerialPort.getCommPorts();  for (SerialPort port : comPorts) {  System.out.println("Found port: " + port.getSystemPortName());  }  // 选择一个串口(这里假设我们选择第一个串口)  SerialPort comPort = SerialPort.getCommPort("COM1"); // 打开串口  if (comPort.openPort()) {  System.out.println("Port is open: " + comPort.getSystemPortName());  // 设置串口参数(这些参数应该与你的设备相匹配)  comPort.setBaudRate(9600);  comPort.setNumDataBits(8);  comPort.setNumStopBits(SerialPort.ONE_STOP_BIT);comPort.setParity(SerialPort.ODD_PARITY);// 发送数据  String dataToSend = "Hello, Serial Port!";  byte[] data = dataToSend.getBytes();  int numWritten = comPort.writeBytes(data, data.length);  System.out.println("Wrote " + numWritten + " bytes.");  // 关闭串口  comPort.closePort();  } else {  System.out.println("Unable to open " + comPort.getSystemPortName());  }  }  
}
- 读配置文件的形式动态设置端口值
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Properties;  public class ConfigReader {  //路径private static final String CONFIG_FILE = "app.properties";  public static void main(String[] args) {  Properties properties = new Properties();  InputStream input = null;  try {  input = new FileInputStream(CONFIG_FILE);  // 从输入流加载属性列表  properties.load(input);  // 读取属性值  String name = properties.getProperty("port.name");  String baudrate = properties.getProperty("port.baudrate");  String numdataits = properties.getProperty("port.numdataits");  String stopbits = properties.getProperty("port.stopbits"); String parity = properties.getProperty("port.parity"); // 打印属性值  System.out.println("name" + name);  } catch (IOException ex) {  ex.printStackTrace();  } finally {  if (input != null) {  try {  input.close();  } catch (IOException e) {  e.printStackTrace();  }  }  }  }  
}
配置文件以properties为后缀名
port.name=COM1
port.baudrate=9600
port.numdataits=8
port.stopbits=1
port.parity=1