串口 RS232代码

Read.java
package serial;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import javaxm.CommPortIdentifier;
import javaxm.SerialPort;
import javaxm.SerialPortEvent;
import javaxm.SerialPortEventListener;
import javaxm.UnsupportedCommOperationException;
public class read {
    //检测系统中可用的通讯端口
    static CommPortIdentifier portId;
    static Enumeration portList;
    InputStream inputStream;
  //声明RS-232串行端口的成员变量
    static SerialPort serialPort;
    static InputStream input;
    public static void main(String[] args) throws Exception{
       
        PortIdentifiers();
        while (portList.hasMoreElements()){
            //强制转换为通讯端口类型
            portId=(Element();
            PortType() == CommPortIdentifier.PORT_SERIAL){
螺旋焊缝钢管
            //    System.out.Name());
                if (Name().equals("COM2")) {
                    System.out.println("用于接收的串口:"+Name());
                    //打开串口
                    serialPort = (SerialPort) portId.open("send", 2000);
                    //设置串口通讯参数
                    serialPort.addEventListener(new SerialPortEventListener(){
                        public void serialEvent(SerialPortEvent event) {
                        //设置串口通讯参数:波特率、数据位、停止位、奇偶校验
                        try {
                            serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                            }
压电陶瓷驱动器                        catch (UnsupportedCommOperationException e) {}
                        byte[] readBuffer = new byte[1024];
                        try {
                            input = InputStream();
                            }
                        catch (IOException e) {}
                        try {
                        //读取数据流
                            while (input.available() > 0) {
                                ad(readBuffer);
                                }
                            String str=new String(readBuffer);
                            System.out.println(str);
                            }
                        catch (IOException e) { }
                        }
                        });
                    // 侦听到串口有数据,触发串口事件
                    ifyOnDataAvailable(true);                   
                    }
                }
甲酸沸点
            }
        }
    }
废盐焚烧炉选型Send.java
package serial;j biol chem
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Scanner;
import javaxm.CommPortIdentifier;
import javaxm.SerialPort;
public class send {
    static CommPortIdentifier portId;
    static Enumeration portList;
    //声明RS-232串行端口
京洲灯饰    static SerialPort serialPort;
    public static void main(String[] args) throws Exception{
        //检测系统中可用的通讯端口类
        PortIdentifiers();
        while (portList.hasMoreElements()){
            //强制转换为通讯端口类型
            portId=(Element();
            PortType() == CommPortIdentifier.PORT_SERIAL){   
                if (Name().equals("COM1")) {
                    System.out.println("用于发送的串口:"+Name());
                    //打开串口
                    serialPort = (SerialPort) portId.open("Read", 2000);
                    //设置串口通讯参数
                    serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                  BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
                  String adLine();
                  //System.out.println(str);
                  byte[] b=new byte[1024];
                  Bytes();
                    //设置串口输出流
                    //OutputStream output = OutputStream();
                  PrintWriter output = new OutputStream());
                    //output.write(b);
                    output.println(str);
                    output.flush();
                    output.close();
                    serialPort.close();
                    }
                }
            }
        }
    }

本文发布于:2024-09-22 14:26:40,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/253051.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:串口   端口   设置   参数   串行
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议