1. The simulator can use the serial port of the PC.
Run the following command to start the simulator and load the PC serial port:
Run emulator @ simulator name-qemu-serial COM1
2. Check whether the serial port is loaded
Run the ADB shell command to open the command line.
Check the CD Dev and you will find that ttys0 ttys1 ttys2, and other ttys2 are the serial port COM1 we loaded.
3. Modify permissions
Chmod 777 ttys2
Now we can develop a serial program.
4. Serial Port program instance
Download libserial_port.so and put it in the libs/armeabi directory. You can create this directory by yourself.
Libserial_port.so:
Http://code.google.com/p/android-serialport-api/
Import java. Io. file;
Import java. Io. filedescriptor;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import Android. util. log;
Public class SerialPort {
Private Static final string tag = "SerialPort ";
Private filedescriptor MFD;
Private fileinputstream mfileinputstream;
Private fileoutputstream mfileoutputstream;
Public SerialPort (File device, int baudrate) throws securityexception, ioexception {
If (! Device. Canread () |! Device. canwrite ()){
Try {
Process Su;
Su = runtime.getruntime(cmd.exe C ("/system/bin/su ");
String cmd = "chmod 666" + device. getabsolutepath () + "\ n"
+ "Exit \ n ";
Su. getoutputstream (). Write (CMD. getbytes ());
If (SU. waitfor ()! = 0) |! Device. Canread ()
|! Device. canwrite ()){
Throw new securityexception ();
}
} Catch (exception e ){
E. printstacktrace ();
Throw new securityexception ();
}
}
MFD = open (device. getabsolutepath (), baudrate );
If (MFD = NULL ){
Log. E (TAG, "Native open returns NULL ");
Throw new ioexception ();
}
Mfileinputstream = new fileinputstream (MFD );
Mfileoutputstream = new fileoutputstream (MFD );
}
Public inputstream getinputstream (){
Return mfileinputstream;
}
Public outputstream getoutputstream (){
Return mfileoutputstream;
}
// JNI
Private native static filedescriptor open (string path, int baudrate );
Public native void close ();
Static {
System. loadlibrary ("serial_port ");
}
}
####################################
Import java. Io. file;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
/**
* Serial port Printing Interface in Linux
* @ Author douk
*
*/
Public class printclass {
// Input stream
Private Static inputstream in;
// Output stream
Private Static outputstream out;
Private Static final string port = "/dev/ttys2"; // serial port
Private SerialPort;
/**
* Connect to the serial port
*/
Private void connect ()
{
Try {
SerialPort = new SerialPort (new file (port), 38400 );
In = SerialPort. getinputstream ();
Out = SerialPort. getoutputstream ();
} Catch (securityexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
/**
* Close the serial port
* @ Throws ioexception
*/
Public void closeserialport ()
{
Try {
Out. Close ();
In. Close ();
SerialPort. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
Run the following command to start the simulator and load the PC serial port:
Run emulator @ simulator name-qemu-serial COM1
2. Check whether the serial port is loaded
Run the ADB shell command to open the command line.
Check the CD Dev and you will find that ttys0 ttys1 ttys2, and other ttys2 are the serial port COM1 we loaded.
3. Modify permissions
Chmod 777 ttys2
Now we can develop a serial program.
4. Serial Port program instance
Download libserial_port.so and put it in the libs/armeabi directory. You can create this directory by yourself.
Libserial_port.so:
Http://code.google.com/p/android-serialport-api/
Import java. Io. file;
Import java. Io. filedescriptor;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import Android. util. log;
Public class SerialPort {
Private Static final string tag = "SerialPort ";
Private filedescriptor MFD;
Private fileinputstream mfileinputstream;
Private fileoutputstream mfileoutputstream;
Public SerialPort (File device, int baudrate) throws securityexception, ioexception {
If (! Device. Canread () |! Device. canwrite ()){
Try {
Process Su;
Su = runtime.getruntime(cmd.exe C ("/system/bin/su ");
String cmd = "chmod 666" + device. getabsolutepath () + "\ n"
+ "Exit \ n ";
Su. getoutputstream (). Write (CMD. getbytes ());
If (SU. waitfor ()! = 0) |! Device. Canread ()
|! Device. canwrite ()){
Throw new securityexception ();
}
} Catch (exception e ){
E. printstacktrace ();
Throw new securityexception ();
}
}
MFD = open (device. getabsolutepath (), baudrate );
If (MFD = NULL ){
Log. E (TAG, "Native open returns NULL ");
Throw new ioexception ();
}
Mfileinputstream = new fileinputstream (MFD );
Mfileoutputstream = new fileoutputstream (MFD );
}
Public inputstream getinputstream (){
Return mfileinputstream;
}
Public outputstream getoutputstream (){
Return mfileoutputstream;
}
// JNI
Private native static filedescriptor open (string path, int baudrate );
Public native void close ();
Static {
System. loadlibrary ("serial_port ");
}
}
####################################
Import java. Io. file;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
/**
* Serial port Printing Interface in Linux
* @ Author douk
*
*/
Public class printclass {
// Input stream
Private Static inputstream in;
// Output stream
Private Static outputstream out;
Private Static final string port = "/dev/ttys2"; // serial port
Private SerialPort;
/**
* Connect to the serial port
*/
Private void connect ()
{
Try {
SerialPort = new SerialPort (new file (port), 38400 );
In = SerialPort. getinputstream ();
Out = SerialPort. getoutputstream ();
} Catch (securityexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
/**
* Close the serial port
* @ Throws ioexception
*/
Public void closeserialport ()
{
Try {
Out. Close ();
In. Close ();
SerialPort. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}