代码如下
import java.io.*;
import java.util.*;
import javax.comm.*;public class Communication { private static SerialPort serialPort;
private static String portName;
private static InputStream in;
private InputStream inputStream;
public static void main (String arg[]){
try{
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName) ;
try {
serialPort = (SerialPort)portId.open("Serial_Communication",5000);
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serialPort. setSerialPortParams (4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1 ,SerialPort.PARITY_NONE);
try {
in = serialPort.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (UnsupportedCommOperationException e)
{
System.out.println("UnsupportedCommOperation");
}
catch (NoSuchPortException e)
{
System.out.println("NoSuchPort");
}
System.out.println("done") ;
}运行结果
NoSuchPort
done鼠标放在CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName)上面,会提示
This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.求高手指教。。
import java.io.*;
import java.util.*;
import javax.comm.*;public class Communication { private static SerialPort serialPort;
private static String portName;
private static InputStream in;
private InputStream inputStream;
public static void main (String arg[]){
try{
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName) ;
try {
serialPort = (SerialPort)portId.open("Serial_Communication",5000);
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serialPort. setSerialPortParams (4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1 ,SerialPort.PARITY_NONE);
try {
in = serialPort.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (UnsupportedCommOperationException e)
{
System.out.println("UnsupportedCommOperation");
}
catch (NoSuchPortException e)
{
System.out.println("NoSuchPort");
}
System.out.println("done") ;
}运行结果
NoSuchPort
done鼠标放在CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName)上面,会提示
This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.求高手指教。。
portName没有进行初始化。那肯定就是空了。
我的外接设备就是连在COM4上。。