Android Operation Serial Port (Android serial Port API)

Source: Internet
Author: User

A few days ago, the company has the use of the Android system through the Development Board to the needs of the clock machine statistics, for the siege lion needs to read and write the serial number of the clock machine on the Android platform, search for something on the internet after the discovery of Android serial on Google code Port API can be used, after the wall to get the source code discovery and demo, good good, I this post through the serial Port API source simple to achieve a read and write serial port, this course is written in native, If there are any child shoes that are not clear on Android using JNI and native words can jump to my previous post point I dot I


in the Android project to build a tool class, the role of this class is to invoke the native method declared in Jni OpenSerialPort, to open the parameters of the path of the serial port, the specific parameters of the serial port is: baudrate bitrate, DataBits data bits, stopbits stop bit, and parity parity. If opened successfully, this method will return the file description instance of the path serial port, so as to obtain the IO stream which can read and write the serial port, and the serial port can be used as a file for us ( It is really a device file on a Linux system. Success is based on a different command or data protocol R/W the serial port is OK.

/** * @Title: Serialportutil.java * @Description: The Util of serial port * @author Jesse * @date Nov 21, 2014 10:10:49 AM * @version V1.0 */public class Serialportutil {private final String TAG = SerialPortUtil.class.getSimpleName ();p Rivat e static Serialportutil minstance = null;private Studiojni studiojni = studiojni.getinstance ();p rivate FileDescriptor mFd ;p rivate FileInputStream mfileinputstream = null; Private FileOutputStream mfileoutputstream = null; Private Boolean isrunning = False;public static Serialportutil getinstance () {if (minstance = = null) {minstance = new Serialp Ortutil ();} return minstance;} public boolean openserialport (String path,int baudrate,int databits, int stopbits, char parity) {LOG.I (TAG, " Openserialport,path: "+ path +", baudrate: "+ baudrate +", DataBits: "+databits+", StopBits: "+ stopbits +", Parity: " + parity), if (isrunning) {log.i (TAG, "openserialport,the serial port is running"); return false;} MFd = Studiojni.serialportopen (path, baudrate,databits,stopbits,parity); if (mfd! = null) {isrunning = True;mfileinputstream = new FileInputStream (MFD); Mfileoutputstream = new FileOutputStream (mFd); }ELSE{LOG.I (TAG, "openserialport," + "The Deivce is null");} return isrunning;} public void Closeserialport () {log.i (TAG, "Closeserialport"); Studiojni.serialportclose (); isrunning = false;}  Public InputStream getInputStream () {return mfileinputstream;  } public OutputStream Getoutputstream () {return mfileoutputstream; }  }
in the native implementation, is to take the configured parameters to open the serial port, configure the serial port. Successfully returns a Java file description that failed to return null.

Jniexport jobject jnicall serial_port_open (jnienv *env,jclass thiz,jstring path, Jint baudrate,    Jint databits,jint Stopbits,jchar parity) {Logi (Camera_tag, "Serial_port_open");    int FD;    speed_t speed;    Jobject Mfiledescriptor;        /* Check arguments */{speed = Getbaudrate (baudrate); if (speed = =-1) {/* Todo:throw an exception */Logi (Camera_tag, "Serial_port_open,invalid baudrate            ");            LOGE (Camera_tag, "Serial_port_open,invalid baudrate");        return NULL;        }}/* Opening device */{Jboolean iscopy;        const char *PATH_UTF = env-getstringutfchars (path, &iscopy);        Logi (Camera_tag, "serial_port_open,opening serial port%s with flags 0x%x", Path_utf, O_RDWR);        FD = open (Path_utf, O_RDWR);        Logi (Camera_tag, "serial_port_open,open () fd =%d", FD);        Env->releasestringutfchars (path, PATH_UTF);    if (fd = =-1) {        /* Throw an exception */Logi (Camera_tag, "Serial_port_open,cannot open port");            LOGE (Camera_tag, "Serial_port_open", "Cannot open port");        /* Todo:throw an exception */return NULL;        }}/* Configure device */{struct Termios cfg;        Logi (Camera_tag, "serial_port_open,configuring serial port");            if (Tcgetattr (FD, &cfg)) {Logi (Camera_tag, "serial_port_open,tcgetattr () failed");            LOGE (Camera_tag, "Serial_port_open", "Tcgetattr () failed");            Close (FD);        /* Todo:throw an exception */return NULL;        } cfmakeraw (&AMP;CFG);        Cfsetispeed (&cfg, speed);        Cfsetospeed (&cfg, speed);            if (tcsetattr (FD, Tcsanow, &cfg)) {Logi (Camera_tag, "Serial_port_open", "tcsetattr () failed");            LOGE (Camera_tag, "Serial_port_open", "tcsetattr () failed");            Close (FD); /* Todo:throw an exceptIon */return NULL;        } set_parity (FD, DataBits, StopBits, Parity);    FD = FD; }/* Create a corresponding file descriptor */{Jclass cfiledescriptor = Env->findclass ("Java/io/filedesc        Riptor ");        Jmethodid ifiledescriptor = Env->getmethodid (Cfiledescriptor, "<init>", "() V");        Jfieldid Descriptorid = Env->getfieldid (cfiledescriptor, "descriptor", "I");        Mfiledescriptor = Env->newobject (Cfiledescriptor, ifiledescriptor);    Env->setintfield (Mfiledescriptor, Descriptorid, (Jint) FD); } return mfiledescriptor;}


  



Android Operation Serial Port (Android serial Port API)

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.