我正在尝试与java中的com端口进行通信。但是,当我尝试将通信端口转换为串行端口时,我反复收到错误。它说无法从CommPort转换为SerialPort。那么我该如何打开端口以便我可以接收数据。任何人都可以帮忙吗?
import gnu.io.*;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import java.io.IOException;
import java.io.InputStream;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class connectnow implements Runnable,SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
byte[] readBuffer;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("portList=" + portList);
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier)portList.nextElement();
System.out.println("portId=" + portId);
}
}
connectnow()
{ try {
System.out.println("In contructor");
serialPort = (SerialPort) portId.open("SimpleReadApp1111",500);// here is the issue
System.out.println(" Serial Port.. " + serialPort);
} catch (PortInUseException e) {
System.out.println("Port in use Exception");
}
try {
inputStream = serialPort.getInputStream();
System.out.println(" Input Stream... " + inputStream);
} catch (IOException e) {
System.out.println("IO Exception");
}
try {
serialPort.addEventListener((javax.comm.SerialPortEventListener) this);
} catch (TooManyListenersException e) {
System.out.println("Tooo many Listener exception");
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// no handshaking or other flow control
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
// timer on any read of the serial port
serialPort.enableReceiveTimeout(500);
System.out.println("................");
} catch (UnsupportedCommOperationException e) {
System.out.println("UnSupported comm operation");
}
readThread = new Thread(this);
readThread.start();}
public void run() {
try {
System.out.println("In run() function ");
Thread.sleep(500);
// System.out.println();
} catch (InterruptedException e) {
System.out.println("Interrupted Exception in run() method");
}
}
public void serialEvent(SerialPortEvent ev) {
try{
if(ev.getEventType()==SerialPortEvent.DATA_AVAILABLE)
{
System.out.println("usha");
}
}
catch(Exception er)
{
}
if(ev.getEventType()== SerialPortEvent.DATA_AVAILABLE)
{
System.out.println("abxxx");
}
}
}
答案 0 :(得分:0)
SerialPort是CommPort的子类。除非您知道声明为CommPort的变量包含SerialPort实例的事实,否则您的强制转换将正确编译但在运行时失败并出现无效的强制转换异常。实际上,将变量强制转换为其子类在编译时始终有效,但是将类的实例强制转换为子类的实例是无效的,并且在运行时会失败。 [编辑] 从文档中可以看出,getPortIdentifiers返回的对象是SerialPort或ParallelPort的实例。因此,如果返回的对象确实是SerialPort而不是ParallelPort,则cast ti SerialPort将工作。添加if语句以确保您获得的是SerialPort实例,而不是其他内容。为此,您可以使用getPortType方法。见http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/javax/comm/CommPortIdentifier.html#getPortType() 您可能希望使用getName()检查端口的名称,或使用文件描述符打开特定端口,而不是选择列表中的最后一个串行端口。
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier)portList.nextElement();
System.out.println("portId=" + portId);
if (portId.getPortType () == CommPortIdentifier.PORT_SERIAL)
break;
}
答案 1 :(得分:0)
在macOS下,另一种获取方式
/usr/local/lib/python3.5/dist-packages
如果/Library/Java/Extensions/RXTXcomm.jar存在,但您尝试使用nrjavaserial jar文件。
诊断这种情况的一种方法是使用Python 3.6.5 (default, Mar 29 2018, 03:28:50)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import spacy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/spacy/__init__.py", line 4, in <module>
from . import util
File "/usr/local/lib/python3.5/dist-packages/spacy/util.py", line 4, in <module>
import ujson
ModuleNotFoundError: No module named 'ujson'
进行编译,并查找CommPort定义的来源。
如果你有
error: incompatible types: CommPort cannot be converted to SerialPort
然后CommPort.class来自/Library/Java/Extensions/RXTXcomm.jar,如果您尝试使用nrjavaserial,这可能不是您想要的。
相反,您希望看到类似的内容:
javac -verbose