永发信息网

java串口,读取和发送数据

答案:4  悬赏:20  手机版
解决时间 2021-01-22 00:38
  • 提问者网友:心如荒岛囚我终老
  • 2021-01-21 01:59
java串口,读取和发送数据
最佳答案
  • 五星知识达人网友:一袍清酒付
  • 2021-01-21 03:33
public static void process() {
 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端口则退出循环
           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);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无
  InputStream in = serialPort.getInputStream();//得到输入流
  OutputStream out = serialPort.getOutputStream();//得到输出流
  
  //进行输入输出操作
//操作结束后
  in.close();
  out.close();
  serialPort.close();//关闭串口
  
 } catch (PortInUseException e) {
  e.printStackTrace();
 } catch (UnsupportedCommOperationException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 }
    
}
全部回答
  • 1楼网友:荒野風
  • 2021-01-21 05:37
是android吗追问不是,就是读取硬件设备接收的数据
  • 2楼网友:想偏头吻你
  • 2021-01-21 05:24
169行,是不是 port.feeding(02) 这行?
那就是port未初始化,还是null。
  • 3楼网友:七十二街
  • 2021-01-21 04:31
上帝知道你在讲什么追问就是跟硬件相连,读取硬件接收的数据
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯