Java 与串行端口通信时强制转换异常
我正在尝试与串行端口通信。在下面的程序中,我列出了可用的端口,它给了我正确的输出,但当我试图与串行端口建立通信时,它给了我以下异常 java.lang.ClassCastException:gnu.io.LPRPort不能强制转换为gnu.io.SerialPort 节目:Java 与串行端口通信时强制转换异常,java,Java,我正在尝试与串行端口通信。在下面的程序中,我列出了可用的端口,它给了我正确的输出,但当我试图与串行端口建立通信时,它给了我以下异常 java.lang.ClassCastException:gnu.io.LPRPort不能强制转换为gnu.io.SerialPort 节目: import gnu.io.CommPortIdentifier; import gnu.io.*; import java.io.*; import java.util.Enumeration; public class
import gnu.io.CommPortIdentifier;
import gnu.io.*;
import java.io.*;
import java.util.Enumeration;
public class PortList {
private static CommPortIdentifier port;
private SerialPort serialport;
private InputStream inputstream;
private OutputStream outputstream;
private static Enumeration ports;
public static void main(String args[]) {
System.out.println("fdsgfjh");
ports = CommPortIdentifier.getPortIdentifiers();
System.out.println(ports.nextElement());
while (ports.hasMoreElements()) {
port = (CommPortIdentifier)ports.nextElement();
String type;
switch (port.getPortType()) {
case CommPortIdentifier.PORT_PARALLEL:
type = "Parallel";
break;
case CommPortIdentifier.PORT_SERIAL:
type = "Serial";
break;
default: /// Shouldn't happen
type = "Unknown";
break;
}
System.out.println(port.getName() + ": " + type);
}
PortList objOfClass=new PortList();
objOfClass.readData();
}
public void readData(){
try{
if (port.isCurrentlyOwned()) {
System.out.println("Port Is In Use");
}
else {
serialport=(SerialPort)port.open(this.getClass().getName(), 2000);//Giving Exception on this line.
System.out.println("Port Is Opened now");
int baudRate=serialport.getBaudRate();
System.out.println(Integer.toString(baudRate));
serialport.setSerialPortParams(1200, 8, 1, serialport.PARITY_NONE);
System.out.println("Properties are set");
inputstream=serialport.getInputStream();
outputstream=serialport.getOutputStream();
byte[] write={12,45,78};
outputstream.write(write);//you have to write the data in the byte format for that status is given in the byte.
outputstream.flush();
byte[] read=new byte[30];
inputstream.read(read);
for(int i =0; i< read.length;i++){
System.out.println(i+" "+read);
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
导入gnu.io.CommPortIdentifier;
输入gnu.io.*;
导入java.io.*;
导入java.util.Enumeration;
公共类端口列表{
专用静态通信识别器端口;
专用串行端口串行端口;
私有输入流输入流;
私有输出流输出流;
专用静态枚举端口;
公共静态void main(字符串参数[]){
System.out.println(“fdsgfjh”);
ports=CommPortIdentifier.getPortIdentifiers();
System.out.println(ports.nextElement());
while(ports.hasMoreElements()){
port=(CommPortIdentifier)ports.nextElement();
字符串类型;
交换机(port.getPortType()){
外壳CommPortIdentifier.PORT_并行:
type=“Parallel”;
打破
外壳CommPortIdentifier.PORT_串行:
type=“串行”;
打破
默认值:///不应该发生
type=“未知”;
打破
}
System.out.println(port.getName()+“:”+类型);
}
PortList objOfClass=新PortList();
objOfClass.readData();
}
public void readData(){
试一试{
if(port.isCurrentlyOwned()){
System.out.println(“端口正在使用”);
}
否则{
serialport=(serialport)port.open(this.getClass().getName(),2000);//在此行中给出异常。
System.out.println(“端口现在已打开”);
int baudRate=serialport.getBaudRate();
System.out.println(整数.toString(波特率));
serialport.setSerialPortParams(1200,8,1,serialport.PARITY_NONE);
System.out.println(“属性已设置”);
inputstream=serialport.getInputStream();
outputstream=serialport.getOutputStream();
字节[]写={12,45,78};
outputstream.write(write);//您必须以字节格式写入数据,因为状态在字节中给出。
outputstream.flush();
字节[]读取=新字节[30];
inputstream.read(读取);
for(int i=0;i
谢谢你的帮助。
谢谢。根据您的实现,
端口将是最后列出的端口。最后列出的端口可能不是串行端口。
在您的情况下,最后列出的端口似乎是并行端口。通过这种方式,我选择了可用的特定端口
CommPortIdentifier.getPortIdentifier(“/dev/ttyS0”) 好的,事情是,gnu.io.LPRPort
作为gnu.io.ParallelPort
的子类,即您的port.open
调用不会像您期望的那样返回串行端口实例。您可能需要使用更通用的类gnu.io.CommPort
,然后检查它是否是gnu.io.SerialPort
,如果不是,请尝试另一个端口,或者错误退出。