Android Serial Operation Method Example _android

Source: Internet
Author: User
Tags chmod define abstract int size

1. First download a libserial_port.so, create a new directory Libs/armeabi, put the so file into the directory.
2. Define the serial port class, modify the permissions in the constructor of the class, open the device, create the input stream and output stream, access the serial port through the native interface, turn off the function

Copy Code code as follows:

public class SerialPort {
/*do not remove or rename the field mfd:it be used by native method Close ();
Public SerialPort (File device, int baudrate, int flags) throws SecurityException, IOException, invalidparameterexception {
If the serial port permissions are not sufficient, change the permissions
/* Check access Permission * *
if (!device.canread () | |!device.canwrite ()) {
try {
/* Missing read/write permission, trying to chmod the file * * *
Process su;
Su = Runtime.getruntime (). EXEC ("/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, flags);//Open serial port
if (mFd = = null) {
LOG.E (TAG, "native Open returns null");
throw new IOException ();
}
Mfileinputstream = new FileInputStream (mFd);//serial Port input stream
Mfileoutputstream = new FileOutputStream (mFd);//serial output stream
}
Getters and Setters
Public InputStream getInputStream () {
return mfileinputstream;
}
Public OutputStream Getoutputstream () {
return mfileoutputstream;
}
Jni
Private native static FileDescriptor open (String path, int baudrate, int flags);//c file serial open () function
Public native void Close ();//c serial Close () function in file
static {
System.loadlibrary ("Serial_port");//loading serial Port Library
}
}
}

3. Define abstract class Serverdata
Copy Code code as follows:

Public abstract class Serverdata {
protected SerialPort Mserialport;
protected OutputStream Moutputstream;
Private InputStream Minputstream;
Private Readthread Mreadthread;
Private class Readthread extends Thread {
@Override
Read data and process data in threads
public void Run () {
Super.run ();
byte[] buffer = new byte[128];
int size;
while (true) {
try {
if (Minputstream = null) return;
Size = minputstream.read (buffer);//Read data
if (Size > 0) {
ondatareceived (buffer, size);//Processing data
}
catch (IOException e) {
E.printstacktrace ();
Return
}
}
}
}
4. Instantiate the serial port class, output stream and input stream, instantiate the read thread and start execution of the thread
[Code]
Public Serverdata (String path, int baudrate) {
try {
Mserialport = new SerialPort (new File (path), baudrate, 0);
Moutputstream = Mserialport.getoutputstream ();
Minputstream = Mserialport.getinputstream ();
/* Create a receiving thread * *
Mreadthread = new Readthread ();
Mreadthread.start ();
catch (SecurityException e) {
catch (IOException e) {
catch (Invalidparameterexception e) {
}
}
protected abstract void ondatareceived (final byte[] buffer, final int size);
}

[/code]
5. Then create a new class, implement the above abstract function in the new class, and write a function to return the read data.
Copy Code code as follows:

Package View;
Import R class, the package is different, you can not drink directly, you need to import before you can use
Import ANDROID_SERIALPORT_API.SAMPLE.R;
/* Etcview class, ETC interface Management * *
public class Serialview {
Private activity context = NULL;
Private serial metcserver = NULL;
//ETC Interface Constructor * *
Public Serialview (activity context) {
This.context = context;
}
public void Etcinitview () {
So we can find the ID under the Android_serialport_api.sample package.
TextView mytext= (TextView) Context.findviewbyid (R.id.mytext);
Metcserver = new Serial ("/dev/s3c2410_serial3", 9600);
}
public void Etcrefresh () {
Returns the data read by a serial thread
byte[] buffer = Metcserver.getdata ();
String Recstring=new string (buffer);//Convert an array of byte[] to string strings
Mytext.settext (recstring);//Set character text
buffer = NULL;
}
}

Related Article

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.