The simulator can use the serial port of the PC

Source: Internet
Author: User

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 ();

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.