Java 串行端口如何连接?

Java 串行端口如何连接?,java,Java,嗨,我正在使用Netbeans作为我的IDE。我想玩一下串口。我在笔记本电脑中使用FTDI电缆,将usb端口转换为RS232串行端口 我发现了这些有趣的网站: 我已经从第一个链接中编写了这段代码 在下面 示例用法 •如何找到系统上可用的串行端口,请参见此处 代码: 语句中的代码中有错误 导入com.embeddendInviled.serial.SerialCommManager 网站上还有第二个链接 预构建的文件夹包含随时可用的jar文件(scm-1.0.3.jar),可以在任何项目中导入并

嗨,我正在使用Netbeans作为我的IDE。我想玩一下串口。我在笔记本电脑中使用FTDI电缆,将usb端口转换为RS232串行端口

我发现了这些有趣的网站:

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

代码:

语句中的代码中有错误 导入com.embeddendInviled.serial.SerialCommManager

网站上还有第二个链接 预构建的文件夹包含随时可用的jar文件(scm-1.0.3.jar),可以在任何项目中导入并立即引用。 在哪里可以找到预生成文件夹

谢谢

----------第二职位----------------------------------------------

继续,我有以下代码:

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(句柄,“testing hello”,0)=true)


此外,我试图理解代码,但我不明白“句柄”的作用。这是C++中的句柄吗?如果是,那么具体是什么?我试图在Java Deitel和wrox书籍中找到关于它的信息,但没有任何信息。

好的!我玩了一点代码,现在它工作得很好。请记住,必须将ftdi电缆插入连接到COM4串行端口的usb端口

代码:


没有句柄就是一个数字。如果仔细查看try块中的代码第二条语句。长句柄=scm.openComPort(“/dev/ttyCOM4”,真、真、假);这里的句柄是scm.openComPort方法返回的一个很长的数字。另外,请对错误进行堆栈后跟踪。我查看了文档,发现scm.openComPort返回打开的端口的句柄。如果您试图打开一个已经打开的端口,则会引发异常。看这里的javadocs是的,我读过doc。问题1:Java中的句柄是什么?是的,我读过doc。那么问题1:Java中的句柄是什么?问题2:方法openComPort()的第一个参数“/dev/ttyUSB1”应该更改为“COM4”或“/dev/ttyCOM4”?问题3。openComPort()的第四个参数对于windows应该为true。看看在post中编辑的错误。你说得对。唯一剩下的错误就是这个语法错误。
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();
        }
    }   
}
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)
/*
 * 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();
        }
    }   
}