jssc串口通信代码

时间:2025-02-18 07:19:52
  • package ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • /**
  • * jssc工具类
  • */
  • public class JsscUtilSingleton {
  • public static final String HEX_PLACE_HOLDER = "0";
  • /**
  • * 十六进制进位阈值
  • */
  • public static final int HEX_CARRY_THRESHOLD = 15;
  • private static class JsscUtil {
  • public static final JsscUtilSingleton JSSC_UTIL_SINGLETON = new JsscUtilSingleton();
  • }
  • public static JsscUtilSingleton getInstance() {
  • return JsscUtil.JSSC_UTIL_SINGLETON;
  • }
  • /**
  • * 发送数据
  • * 统一发送指令的方法
  • *
  • * @param serialPort
  • * @param bytes 命令字节数组
  • */
  • public static List<String> sendData(SerialPort serialPort, byte[] bytes) {
  • if (serialPort != null && ()) {
  • try {
  • (bytes);
  • // 延时时间,50ms多次测试最稳当
  • (50);
  • // 读取命令返回数据
  • String[] resCode = ();
  • if (resCode == null || == 0) {
  • return ();
  • } else {
  • ("命令发送后的返回值: " + (resCode));
  • return (resCode);
  • }
  • } catch (SerialPortException e) {
  • ();
  • } catch (InterruptedException e) {
  • throw new RuntimeException(e);
  • }
  • }
  • return ();
  • }
  • public static String sendDataResStr(SerialPort serialPort, byte[] bytes) {
  • if (serialPort != null && ()) {
  • try {
  • (bytes);
  • // 延时时间,50ms多次测试最稳当
  • (50);
  • // 读取命令返回数据
  • String resCode = ();
  • if (resCode == null || ()) {
  • return null;
  • } else {
  • ("命令发送后的返回值: " + resCode);
  • return (" ", "");
  • }
  • } catch (SerialPortException e) {
  • ();
  • } catch (InterruptedException e) {
  • throw new RuntimeException(e);
  • }
  • }
  • return null;
  • }
  • /**
  • * jssc链接端口
  • *
  • * @param portName
  • * @param baudRate
  • * @return
  • */
  • public SerialPort openSerialPort(String portName, int baudRate) {
  • final SerialPort serialPort = new SerialPort(portName);
  • try {
  • ();
  • (baudRate, 8, 1, 0);
  • if (()) {
  • ("打开串口: " + ());
  • }
  • } catch (SerialPortException e) {
  • throw new RuntimeException(e);
  • }
  • return serialPort;
  • }
  • /**
  • * 关闭端口
  • *
  • * @param serialPort
  • * @return
  • */
  • public static boolean closePort(final SerialPort serialPort) {
  • try {
  • return ();
  • } catch (SerialPortException e) {
  • throw new RuntimeException(e);
  • }
  • }
  • /**
  • * 获取校验和
  • * 光谱仪
  • *
  • * @param lengthHigh
  • * @param lengthLow
  • * @param commandId
  • * @param dataHigh
  • * @param dataLow
  • * @return
  • */
  • public static String getChecksum(int lengthHigh, int lengthLow, int commandId, int dataHigh, int dataLow) {
  • // 求和
  • int sumRes = lengthHigh + lengthLow + commandId + dataHigh + dataLow;
  • int check = sumRes & 0xFF;
  • ("check is: " + check);
  • String checkStr = (check);
  • if (check > HEX_CARRY_THRESHOLD) {
  • return checkStr;
  • } else {
  • return HEX_PLACE_HOLDER + checkStr;
  • }
  • }
  • /**
  • * 初始化命令
  • * 光谱仪
  • *
  • * @param commandId
  • * @param data
  • * @return
  • */
  • public static String generateCommand(int commandId, int data) {
  • int dataLength = 0;
  • if (data > 0) {
  • // 当data有数据的时候则,数据位两位
  • dataLength = 2;
  • }
  • // 从length到checksum
  • int length = 4 + dataLength;
  • int lengthHigh = (length >> 8) & 0xFF;
  • // 低位
  • int lengthlow = length & 0xFF;
  • // 数据高位
  • int dataHigh = (data >> 8) & 0xFF;
  • int dataLow = data & 0xFF;
  • // 计算checksum
  • String checkSum = getChecksum(lengthHigh, lengthlow, commandId, dataHigh, dataLow);
  • ("checkSum is: " + checkSum);
  • // 构造命令
  • StringBuilder commandStr = new StringBuilder("AA,55")
  • // 设置长度length
  • .append(lengthHigh <= 0 ? "00" : (lengthHigh > 15 ? (lengthHigh) : "0" + (lengthHigh)))
  • .append(lengthlow <= 0 ? "00" : (lengthlow > 15 ? (lengthlow) : "0" + (lengthlow)))
  • // commandId
  • .append(commandId > 15 ? (commandId) : "0" + (commandId));
  • // 如果0存在
  • if (data > 0) {
  • (dataHigh > 15 ? (dataHigh) : "0" + (dataHigh))
  • .append(dataLow > 15 ? (dataLow) : "0" + (dataLow));
  • }
  • //.append(dataHigh <= 0 ? "00" : (dataHigh > 15 ? (dataHigh) : "0" + (dataHigh)))
  • //.append(dataLow <= 0 ? "00" : (dataLow > 15 ? (dataLow) : "0" + (dataLow)))
  • // 设置checksum
  • (checkSum);
  • return ();
  • }
  • /**
  • * 光谱仪发送命令封装
  • *
  • * @param commandId
  • * @param data
  • * @return
  • */
  • public static String spectrometerSendCommand(SerialPort serialPort, int commandId, int data) {
  • // 构造命令字符串
  • String commandStr = generateCommand(commandId, data);
  • ("发送字符串命令: " + commandStr);
  • // 十六进制字符串转字节数组
  • byte[] command = hexStringToByteArray(commandStr);
  • // 发送命令
  • String resStr = sendDataResStr(serialPort, command);
  • ("返回字符串为:" + resStr);
  • // 光谱仪设置积分时间,如果第一次没发送成功则重新调用一次发送命令方法
  • if (resStr == null) {
  • resStr = (serialPort, command);
  • }
  • // 切出返回数据
  • String dataStr = (10, () - 2);
  • ("切片出data值为: " + dataStr);
  • return dataStr;
  • }
  • /**
  • * 16进制转Ascii
  • *
  • * @param hexStr
  • * @return
  • */
  • public static String hexToAscii(String hexStr) {
  • StringBuilder output = new StringBuilder("");
  • for (int i = 0; i < (); i += 2) {
  • String str = (i, i + 2);
  • ((char) (str, 16));
  • }
  • return ();
  • }
  • /**
  • * 16进制表示的字符串转换为字节数组
  • *
  • * @param hexString 16进制表示的字符串
  • * @return byte[] 字节数组
  • */
  • public static byte[] hexStringToByteArray(String hexString) {
  • hexString = (",", "");
  • int len = ();
  • byte[] bytes = new byte[len / 2];
  • for (int i = 0; i < len; i += 2) {
  • // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
  • bytes[i / 2] = (byte) ((((i), 16) << 4) + Character
  • .digit((i + 1), 16));
  • }
  • return bytes;
  • }
  • }