串口我怎么样?

时间:2015-10-28 12:41:12

标签: java

您好我使用Netbeans作为我的IDE。我想用串口播放一下。我在我的笔记本电脑中使用FTDI电缆将usb端口转换为RS232串口。

我找到了这些有趣的网站: http://www.embeddedunveiled.com/ https://github.com/RishiGupta12/serial-communication-manager

我已经从第一个链接中编写了这段代码 下 用法示例 •如何在系统中找到可用的串行端口。

代码:

package findserialports;

import com.embeddedunveiled.serial.SerialComManager;

/**
 *
 * @author Alexandros
 */

public class FindSerialPorts {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            SerialComManager scm = new SerialComManager();
            String[] ports = scm.listAvailableComPorts();
            for(String port: ports){
                System.out.println(port);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

语句中的代码有错误 import com.embeddedunveiled.serial.SerialComManager;

此外它还说现场第二个链接 预构建的文件夹包含可立即使用的jar文件(scm-1.0.3.jar),可以在任何项目中导入并立即引用。 我在哪里可以找到prebuild文件夹?

感谢

----------第二篇文章----------------------------------- -----------

继续我有以下代码:

package serialportftdi;

import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;


/**
 *
 * @author Alexandros
 */
public class SerialPortFTDI {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            SerialComManager scm = new SerialComManager();
            long handle = scm.openComPort("/dev/ttyUSB1", true, true, false);               scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
            scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
            scm.writeString(handle, "testing hello", 0) == true);
            String data = scm.readString(handle);
            System.out.println("data read is :" + data);
            scm.closeComPort(handle);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }   
}

在第scm.writeString(handle, "testing hello", 0) == true);

行发生错误
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\src\serialportftdi\SerialPortFTDI.java:31: error: not a statement
            scm.writeString(handle, "testing hello", 0) == true);
                                                        ^
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\src\serialportftdi\SerialPortFTDI.java:31: error: ';' expected
            scm.writeString(handle, "testing hello", 0) == true);
                                                               ^
2 errors
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 3 seconds)

此外,我正在尝试理解代码,但我不明白'句柄'的作用。这是C ++中的句柄吗?如果是,那究竟是什么?我试图在Java Deitel和wrox书中找到有关它的信息,但没有信息。

1 个答案:

答案 0 :(得分:1)

确定!我玩了一些代码,现在它工作正常。请记住,必须将ftdi电缆插入发现连接到COM4串行端口的USB端口。

代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package serialportftdi;

import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;


/**
 *
 * @author Alexandros
 */
public class SerialPortFTDI {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            SerialComManager scm = new SerialComManager();
            long handle = scm.openComPort("COM4", true, true, true);
            scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
            scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
            scm.writeString(handle, "testing hello", 0); //== true);
            String data = scm.readString(handle);
            System.out.println("data read is :" + data);
            scm.closeComPort(handle);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }   
}