The last blog talked about sending data to android development board with a serial assistant. Now let's build a platform by ourselves.
Let's start with the pit I stepped on before, because I used SerialPort.c to successfully open the serial port on android, so I think it's similar on mac. Then I used java to call. so directly on eclipse in the same way to open the serial port, but found that it could not be opened. After searching on the internet, it was found that the call on the mac should not be. so, but should be. jnilib suffix file. Then I converted SerialPort to. jnilib, and the result was that the serial port could not be opened. All kinds of debugging, various searches, various forums to ask questions, no eggs. Just then, the teacher saw my embarrassment, knew my problem, gave me two files, and successfully opened, successfully opened, successfully!!! Is it helpless? But? But?
Well, it's not difficult at all, as long as you use the jar package correctly, there's no problem at all. Here are two files, one is RXTXcomm.jar and the other is librxtxSerial.jnilib. Click to download.
What is RXTX? RXTX project provides the realization of javax.comm serial communication package API compatible with Windows,Linux,Mac os X,Solaris operating system, which provides considerable convenience for other researchers to develop serial applications under such systems. RXTXcomm.jar is one of the jar packages. librxtxSerial.jnilib is the file that the jar package needs to call.
Usage mode
1. Refer to RXTXcommo.jar.
2. Referring to librxtxSerial.jnilib is the same as referring to so. Just put it under the "library directory" of java. Where can I put it and print it out?
System.out.println("lib-->" +System.getProperty("java.library.path"));
You can know where the "library" path of java is, and just copy it in.
3. Open the serial port, for example (this is just the sample code for opening the serial port in the project):
/**
* Open serial port
* @param portName
* @param raudRate
* @param dataBit
* @param stopBit
* @param parityBit
* @return SerialPort
* @throws Throwable
*/
private SerialPort openSerialPort(String portName,int raudRate,int dataBit,int stopBit,int parityBit) throws Throwable{
SerialPort serialPort=null;
//Identify ports by port name
// Enumeration en=CommPortIdentifier.getPortIdentifiers();
// while (en.hasMoreElements()) {
// CommPortIdentifier object = (CommPortIdentifier) en.nextElement();
// System.out.println(object.getName());
//
// }
CommPortIdentifier portIdentifier=CommPortIdentifier.getPortIdentifier(portName);
//Determine whether the port is occupied
if(portIdentifier.isCurrentlyOwned()){
closeSerialPort();
throw new PortInUseException();
}else{
//Open the port and set the port response time to 2 seconds
CommPort commport=portIdentifier.open(portName, 2000);
//Determine whether the port is a serial port
if(commport instanceof SerialPort){
serialPort=(SerialPort) commport;
//Setting the basic parameters of serial port mainly includes baud rate, data bit, stop bit and check bit.
serialPort.setSerialPortParams(raudRate, dataBit, stopBit, parityBit);
System.out.println("Serial port has been opened!");
}else{
//If the port is not a serial port, close the port
closeSerialPort();
throw new Exception("this port is not serial port!");
}
}
return serialPort;
}
Be careful
If you still can't open the serial port, see if there is a folder / var/lock in the root directory, and if not, create the folder. Click to see the role of this folder.