[Serial SerialPort test in Android primary]android simulator

Source: Internet
Author: User

Recent projects to use the serial communication, on the board with the Android system to listen to the COM port data, this piece did not contact before, I searched for an open source project: Android-serialport-api, through this open source project, I can easily listen and request data on the device ports required on the Android application layer.

Before using, considering that there is no hardware, I first do a virtual test. On the computer virtual build 2 COM port, (com2<-------->com3), they are interoperability, I want to do this, through the Android app listening COM2, receive data from COM2 and display, and then send data to COM2. So let's get started!

First use VSPD to create 2 virtual ports COM2, COM3,



Then you download the SERIALPORTAPI, import it into Eclipse, build your own new project, put Android_serialport_api and Android_serialport_ Api.sample this entire package to the new test project, and the JNI and the Libs are copied over, the equivalent of the entire project into your own project!

Then create a new test class that receives and sends the data

Serialporttest

Package Com.test.serialport;import Java.io.ioexception;import Java.io.unsupportedencodingexception;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Com.example.test1.r;public class Serialporttest extends serialportactivity implements onclicklistener{private static Final String Tag = SerialPortTest.class.getSimpleName ();p rivate Button msendbutton;private EditText Msendedit, Mshowedit;private application app;private Handler mhandler; @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (r.layout.serial_test_layout); Initview (); mHandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {if (msg.what = = 1) {Mshowedit.settext (""); Mshowedit.settext (New String ((byte[)) msg.obj,0,msg.arg1));}};} private void Initview () {Msendbutton = (Button) Findviewbyid (R.id.send_button); Msendbutton.setonclicklistener (this); Msendedit = (EditText) Findviewbyid (r.id.send_edit_text); Mshowedit = ( EditText) Findviewbyid (r.id.show_edit_text);} private void sendstring (String str) {if (moutputstream!=null) {try {moutputstream.write (str.getbytes ("GBK")); Moutputstream.flush ();} catch (Unsupportedencodingexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}} @Overrideprotected void OnDestroy () {Super.ondestroy ();} @Overrideprotected void ondatareceived (byte[] buffer, int size) {message msg = new Message (); msg.what = 1;msg.obj = Buffer ; msg.arg1 = Size;mhandler.sendmessage (msg);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.send_button:sendstring (Msendedit.gettext (). ToString ()); break;}}}


Also, when starting the Android emulator, you must bind the specified device port, in this article I listen to the virtual port COM2, so you have to start the emulator, command:emulator @avdname-qemu-serial COM2

In the code we want to listen to the port in the directory "/dev/tts2"



This is a simple test of this API, specific to the use you can see for yourself the dem0 he offers.



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.