<i id='13LUp'><tr id='13LUp'><dt id='13LUp'><q id='13LUp'><span id='13LUp'><b id='13LUp'><form id='13LUp'><ins id='13LUp'></ins><ul id='13LUp'></ul><sub id='13LUp'></sub></form><legend id='13LUp'></legend><bdo id='13LUp'><pre id='13LUp'><center id='13LUp'></center></pre></bdo></b><th id='13LUp'></th></span></q></dt></tr></i><div id='13LUp'><tfoot id='13LUp'></tfoot><dl id='13LUp'><fieldset id='13LUp'></fieldset></dl></div>

      <small id='13LUp'></small><noframes id='13LUp'>

    1. <tfoot id='13LUp'></tfoot>

        <bdo id='13LUp'></bdo><ul id='13LUp'></ul>
      <legend id='13LUp'><style id='13LUp'><dir id='13LUp'><q id='13LUp'></q></dir></style></legend>

      如何将字符串转换为十六进制和十六进制转换为字符串?

      时间:2024-08-24
        <tbody id='ENRHd'></tbody>
      <tfoot id='ENRHd'></tfoot>

      1. <i id='ENRHd'><tr id='ENRHd'><dt id='ENRHd'><q id='ENRHd'><span id='ENRHd'><b id='ENRHd'><form id='ENRHd'><ins id='ENRHd'></ins><ul id='ENRHd'></ul><sub id='ENRHd'></sub></form><legend id='ENRHd'></legend><bdo id='ENRHd'><pre id='ENRHd'><center id='ENRHd'></center></pre></bdo></b><th id='ENRHd'></th></span></q></dt></tr></i><div id='ENRHd'><tfoot id='ENRHd'></tfoot><dl id='ENRHd'><fieldset id='ENRHd'></fieldset></dl></div>
            <bdo id='ENRHd'></bdo><ul id='ENRHd'></ul>

              • <legend id='ENRHd'><style id='ENRHd'><dir id='ENRHd'><q id='ENRHd'></q></dir></style></legend>

                <small id='ENRHd'></small><noframes id='ENRHd'>

              • 本文介绍了如何将字符串转换为十六进制和十六进制转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我做了一个简单的程序,用于在java中向串口发送和接收数据.我将串行端口设备与环回测试(Rx 到 Tx)连接起来.它工作正常.但我无法将十六进制数据发送和接收到串行端口和接收串行端口.在我的设备中使用了 FT232BL 芯片.那么是否需要任何 dll 或其他库来向串口设备发送和接收十六进制数据.我的代码如下.

                I made simple program for sending and receiving data to serial port in java. I connect serial port device with loop back testing (Rx to Tx). it works fine. but i can't send and recive hex data to serial port and recive serial port. In my device there are FT232BL chip is used.So is there any dll or other library required to send and receive hex data to serial port device. my code is below.

                enter code here
                package x.rayimagecapture;
                
                import gnu.io.CommPort;
                import gnu.io.CommPortIdentifier;
                import gnu.io.SerialPort;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import java.io.IOException;
                import java.io.InputStream;
                import java.io.OutputStream;
                import java.util.logging.Level;
                import java.util.logging.Logger;
                import javax.swing.JFrame;
                import javax.swing.JPanel;
                import javax.swing.Timer;
                
                public class TwoWaySerialComm extends JPanel implements ActionListener {
                
                    OutputStream out;
                    private javax.swing.JButton btn;
                    private javax.swing.JScrollPane jScrollPane1;
                    public static javax.swing.JLabel jLabel1;
                    public static javax.swing.JTextField textField;
                    public static boolean flag = false;
                    public static int count = 50;
                //    TimerWindow tmr=new TimerWindow();
                    public static Timer tmr = new Timer(1000, new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                                        count++;
                            jLabel1.setText("" + count);
                        }
                    });
                
                    public TwoWaySerialComm() {
                        super();
                        textField = new javax.swing.JTextField();
                        btn = new javax.swing.JButton();
                        jScrollPane1 = new javax.swing.JScrollPane();
                        jLabel1 = new javax.swing.JLabel();
                        textField.setText(""); // NOI18N
                //        tmr.setDelay(0);
                
                        btn.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                        btn.setText("Send"); // NOI18N
                        btn.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                serialwrite();
                            }
                        });
                
                //        textArea.setColumns(20);
                //        textArea.setRows(20);
                //        textArea.setTabSize(0);
                        jScrollPane1.setViewportView(jLabel1);
                
                        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(this);
                        this.setLayout(panelLayout);
                        panelLayout.setHorizontalGroup(
                                panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(panelLayout.createSequentialGroup()
                                .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btn, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                                .addContainerGap())
                                .addComponent(jScrollPane1));
                        panelLayout.setVerticalGroup(
                                panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(panelLayout.createSequentialGroup()
                                .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)));
                    }
                
                    void connect(String portName) throws Exception {
                        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
                        if (portIdentifier.isCurrentlyOwned()) {
                            System.out.println("Error: Port is currently in use");
                        } else {
                            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
                
                            if (commPort instanceof SerialPort) {
                                SerialPort serialPort = (SerialPort) commPort;
                                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                                serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                                InputStream in = serialPort.getInputStream();
                                out = serialPort.getOutputStream();
                
                                (new Thread(new SerialReader(in))).start();
                //                (new Thread(new SerialWriter(out))).start();
                
                            } else {
                                System.out.println("Error: Only serial ports are handled by this example.");
                            }
                        }
                    }
                
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        serialwrite();
                    }
                
                    public void serialwrite() {
                        try {
                            this.out.write(textField.getText().toString().getBytes());
                            count = 00;
                //            tmr.start();
                            tmr.start();
                            this.out.flush();
                        } catch (IOException ex) {
                            Logger.getLogger(TwoWaySerialComm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                
                    /**
                     *
                     */
                    public static class SerialReader implements Runnable {
                
                        InputStream in;
                
                        public SerialReader(InputStream in) {
                            this.in = in;
                        }
                
                        public void run() {
                            byte[] buffer = new byte[1024];
                            int len = -1;
                            try {
                                while ((len = this.in.read(buffer)) != -1) {
                //                    textArea.append(" " + new String(buffer, 0, len));
                                    String str = new String(buffer, 0, len);
                                    System.out.print(str);
                                    if (!str.isEmpty()) {
                //                        tmr.stop();
                                    }
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                
                    /**
                     *
                     */
                    public static class SerialWriter implements Runnable {
                
                        OutputStream out;
                
                        public SerialWriter(OutputStream out) {
                            this.out = out;
                        }
                
                        public void run() {
                            try {
                                int c = 0;
                                while ((c = System.in.read()) != -1) {
                //                    this.out.write(c);
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                
                    public static void main(String[] args) {
                        TwoWaySerialComm tws = new TwoWaySerialComm();
                        JFrame frame = new JFrame();
                        frame.setSize(400, 300);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.add(tws);
                        try {
                            tws.connect("COM11");
                        } catch (Exception ex) {
                            Logger.getLogger(TwoWaySerialComm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        frame.setVisible(true);
                    }
                }
                

                推荐答案

                String转十六进制:

                Integer.decode(hexString);
                

                十六进制转字符串:

                Integer.toHexString(integer);
                

                这篇关于如何将字符串转换为十六进制和十六进制转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Java 串口关闭块 下一篇:当我使用 NetBeans 6.8 和 Eclipse 运行此代码时,为什么输出会有所不同?

                相关文章

                <i id='BxVEH'><tr id='BxVEH'><dt id='BxVEH'><q id='BxVEH'><span id='BxVEH'><b id='BxVEH'><form id='BxVEH'><ins id='BxVEH'></ins><ul id='BxVEH'></ul><sub id='BxVEH'></sub></form><legend id='BxVEH'></legend><bdo id='BxVEH'><pre id='BxVEH'><center id='BxVEH'></center></pre></bdo></b><th id='BxVEH'></th></span></q></dt></tr></i><div id='BxVEH'><tfoot id='BxVEH'></tfoot><dl id='BxVEH'><fieldset id='BxVEH'></fieldset></dl></div>

                <small id='BxVEH'></small><noframes id='BxVEH'>

                <legend id='BxVEH'><style id='BxVEH'><dir id='BxVEH'><q id='BxVEH'></q></dir></style></legend>

                    <bdo id='BxVEH'></bdo><ul id='BxVEH'></ul>
                  1. <tfoot id='BxVEH'></tfoot>