欢迎来到天天文库
浏览记录
ID:6070526
大小:88.71 KB
页数:16页
时间:2018-01-02
《android 串口编程原理和实现方式(附源码)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
提到串口编程,就不得不提到JNI,不得不提到JavaAPI中的文件描述符类:FileDescriptor。下面我分别对JNI、FileDescriptor以及串口的一些知识点和实现的源码进行分析说明。这里主要是参考了开源项目android-serialport-api。 串口编程需要了解的基本知识点:对于串口编程,我们只需对串口进行一系列的设置,然后打开串口,这些操作我们可以参考串口调试助手的源码进行学习。在Java中如果要实现串口的读写功能只需操作文件设备类:FileDescriptor即可,其他的事都由驱动来完成不用多管!当然,你想了解,那就得看驱动代码了。这里并不打算对驱动进行说明,只初略阐述应用层的实现方式。 (一)JNI: 关于JNI的文章网上有很多,不再多做解释,想详细了解的朋友可以查看云中漫步的技术文章,写得很好,分析也很全面,那么在这篇拙文中我强调3点: 1、如何将编译好的SO文件打包到APK中?(方法很简单,直接在工程目录下新建文件夹libs/armeabi,将SO文件Copy到此目录即可) 2、命名要注意的地方?(在编译好的SO文件中,将文件重命名为:libfilename.so即可。其中filename.so是编译好后生成的文件) 3、MakeFile文件的编写(不用多说,可以直接参考package/apps目录下用到JNI的相关项目写法) 这是关键的代码:[cpp] viewplaincopy1. int fd; 2. speed_t speed; 3. jobject mFileDescriptor; 4. 5. /* Check arguments */ 6. { 7. speed = getBaudrate(baudrate); 8. if (speed == -1) { 9. /* TODO: throw an exception */ 10. LOGE("Invalid baudrate"); 11. return NULL; 12. } 13. } 14. 15. /* Opening device */ 16. { 17. jboolean iscopy; 1. const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy); 2. LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags); 3. fd = open(path_utf, O_RDWR | flags); 4. LOGD("open() fd = %d", fd); 5. (*env)->ReleaseStringUTFChars(env, path, path_utf); 6. if (fd == -1) 7. { 8. /* Throw an exception */ 9. LOGE("Cannot open port"); 10. /* TODO: throw an exception */ 11. return NULL; 12. } 13. } 14. 15. /* Configure device */ 16. { 17. struct termios cfg; 18. LOGD("Configuring serial port"); 19. if (tcgetattr(fd, &cfg)) 20. { 21. LOGE("tcgetattr() failed"); 22. close(fd); 23. /* TODO: throw an exception */ 24. return NULL; 25. } 26. 27. cfmakeraw(&cfg); 28. cfsetispeed(&cfg, speed); 29. cfsetospeed(&cfg, speed); 30. 31. if (tcsetattr(fd, TCSANOW, &cfg)) 32. { 33. LOGE("tcsetattr() failed"); 34. close(fd); 35. /* TODO: throw an exception */ 36. return NULL; 37. } 38. } 39. (二)FileDescritor: 文件描述符类的实例用作与基础机器有关的某种结构的不透明句柄,该结构表示开放文件、开放套接字或者字节的另一个源或接收者。文件描述符的主要实际用途是创建一个包含该结构的FileInputStream 或FileOutputStream。这是API的描述,不太好理解,其实可简单的理解为:FileDescritor就是对一个文件进行读写。(三)实现串口通信细节1) 建工程:SerialDemo包名:org.winplus.serial,并在工程目录下新建jni和libs两个文件夹和一个org.winplus.serial.utils,如下图:2)新建一个类:SerialPortFinder,添加如下代码:[java] viewplaincopy1.package org.winplus.serial.utils; 2. 3.import java.io.File; 4.import java.io.FileReader; 5.import java.io.IOException; 6.import java.io.LineNumberReader; 7.import java.util.Iterator; 8.import java.util.Vector; 9. 10.import android.util.Log; 11. 12.public class SerialPortFinder { 13. 14. private static final String TAG = "SerialPort"; 1. 2. private Vector mDrivers = null; 3. 4. public class Driver { 5. public Driver(String name, String root) { 6. mDriverName = name; 7. mDeviceRoot = root; 8. } 9. 10. private String mDriverName; 11. private String mDeviceRoot; 12. Vector mDevices = null; 13. 14. public Vector getDevices() { 15. if (mDevices == null) { 16. mDevices = new Vector(); 17. File dev = new File("/dev"); 18. File[] files = dev.listFiles(); 19. int i; 20. for (i = 0; i < files.length; i++) { 21. if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) { 22. Log.d(TAG, "Found new device: " + files[i]); 23. mDevices.add(files[i]); 24. } 25. } 26. } 27. return mDevices; 28. } 29. 30. public String getName() { 31. return mDriverName; 32. } 33. } 34. 35. Vector getDrivers() throws IOException { 36. if (mDrivers == null) { 37. mDrivers = new Vector(); 38. LineNumberReader r = new LineNumberReader(new FileReader( 39. "/proc/tty/drivers")); 40. String l; 41. while ((l = r.readLine()) != null) { 42. // Issue 3: 43. // Since driver name may contain spaces, we do not extract 44. // driver name with split() 1. String drivername = l.substring(0, 0x15).trim(); 2. String[] w = l.split(" +"); 3. if ((w.length >= 5) && (w[w.length - 1].equals("serial"))) { 4. Log.d(TAG, "Found new driver " + drivername + " on " 5. + w[w.length - 4]); 6. mDrivers.add(new Driver(drivername, w[w.length - 4])); 7. } 8. } 9. r.close(); 10. } 11. return mDrivers; 12. } 13. 14. public String[] getAllDevices() { 15. Vector devices = new Vector(); 16. // Parse each driver 17. Iterator itdriv; 18. try { 19. itdriv = getDrivers().iterator(); 20. while (itdriv.hasNext()) { 21. Driver driver = itdriv.next(); 22. Iterator itdev = driver.getDevices().iterator(); 23. while (itdev.hasNext()) { 24. String device = itdev.next().getName(); 25. String value = String.format("%s (%s)", device, 26. driver.getName()); 27. devices.add(value); 28. } 29. } 30. } catch (IOException e) { 31. e.printStackTrace(); 32. } 33. return devices.toArray(new String[devices.size()]); 34. } 35. 36. public String[] getAllDevicesPath() { 37. Vector devices = new Vector(); 38. // Parse each driver 39. Iterator itdriv; 40. try { 41. itdriv = getDrivers().iterator(); 42. while (itdriv.hasNext()) { 43. Driver driver = itdriv.next(); 44. Iterator itdev = driver.getDevices().iterator(); 1. while (itdev.hasNext()) { 2. String device = itdev.next().getAbsolutePath(); 3. devices.add(device); 4. } 5. } 6. } catch (IOException e) { 7. e.printStackTrace(); 8. } 9. return devices.toArray(new String[devices.size()]); 10. } 11.} 12. 上面这个类在“android-serialport-api串口工具测试随笔”中有详细的说明,我就不多说了。3)新建SerialPort类,这个类主要用来加载SO文件,通过JNI的方式打开关闭串口[java] viewplaincopy1.package org.winplus.serial.utils; 2. 3.import java.io.File; 4.import java.io.FileDescriptor; 5.import java.io.FileInputStream; 6.import java.io.FileOutputStream; 7.import java.io.IOException; 8.import java.io.InputStream; 9.import java.io.OutputStream; 10. 11.import android.util.Log; 12. 13.public class SerialPort { 14. private static final String TAG = "SerialPort"; 15. 16. /* 17. * Do not remove or rename the field mFd: it is used by native method 18. * close(); 19. */ 20. private FileDescriptor mFd; 21. private FileInputStream mFileInputStream; 22. private FileOutputStream mFileOutputStream; 23. 1. public SerialPort(File device, int baudrate, int flags) 2. throws SecurityException, IOException { 3. 4. /* Check access permission */ 5. if (!device.canRead() || !device.canWrite()) { 6. try { 7. /* Missing read/write permission, trying to chmod the file */ 8. Process su; 9. su = Runtime.getRuntime().exec("/system/bin/su"); 10. String cmd = "chmod 666 " + device.getAbsolutePath() + " " 11. + "exit "; 12. su.getOutputStream().write(cmd.getBytes()); 13. if ((su.waitFor() != 0) || !device.canRead() 14. || !device.canWrite()) { 15. throw new SecurityException(); 16. } 17. } catch (Exception e) { 18. e.printStackTrace(); 19. throw new SecurityException(); 20. } 21. } 22. 23. mFd = open(device.getAbsolutePath(), baudrate, flags); 24. if (mFd == null) { 25. Log.e(TAG, "native open returns null"); 26. throw new IOException(); 27. } 28. mFileInputStream = new FileInputStream(mFd); 29. mFileOutputStream = new FileOutputStream(mFd); 30. } 31. 32. // Getters and setters 33. public InputStream getInputStream() { 34. return mFileInputStream; 35. } 36. 37. public OutputStream getOutputStream() { 38. return mFileOutputStream; 39. } 40. 41. // JNI 42. private native static FileDescriptor open(String path, int baudrate, 43. int flags); 44. 1. public native void close(); 2. 3. static { 4. System.loadLibrary("serial_port"); 5. } 6.} 7. 4)新建一个Application继承android.app.Application,用来对串口进行初始化和关闭串口[java] viewplaincopy1.package org.winplus.serial; 2. 3.import java.io.File; 4.import java.io.IOException; 5.import java.security.InvalidParameterException; 6. 7.import org.winplus.serial.utils.SerialPort; 8.import org.winplus.serial.utils.SerialPortFinder; 9. 10.import android.content.SharedPreferences; 11. 12.public class Application extends android.app.Application { 13. public SerialPortFinder mSerialPortFinder = new SerialPortFinder(); 14. private SerialPort mSerialPort = null; 15. 16. public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException { 17. if (mSerialPort == null) { 18. /* Read serial port parameters */ 19. SharedPreferences sp = getSharedPreferences("android_serialport_api.sample_preferences", MODE_PRIVATE); 20. String path = sp.getString("DEVICE", ""); 21. int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1")); 22. 23. /* Check parameters */ 24. if ( (path.length() == 0) || (baudrate == -1)) { 25. throw new InvalidParameterException(); 26. } 27. 28. /* Open the serial port */ 29. mSerialPort = new SerialPort(new File(path), baudrate, 0); 30. } 31. return mSerialPort; 1. } 2. 3. public void closeSerialPort() { 4. if (mSerialPort != null) { 5. mSerialPort.close(); 6. mSerialPort = null; 7. } 8. } 9.} 10. 5)新建一个继承抽象的Activity类,主要用于读取串口的信息[java] viewplaincopy1.package org.winplus.serial; 2. 3.import java.io.IOException; 4.import java.io.InputStream; 5.import java.io.OutputStream; 6.import java.security.InvalidParameterException; 7. 8.import org.winplus.serial.utils.SerialPort; 9. 10.import android.app.Activity; 11.import android.app.AlertDialog; 12.import android.content.DialogInterface; 13.import android.content.DialogInterface.OnClickListener; 14.import android.os.Bundle; 15. 16.public abstract class SerialPortActivity extends Activity { 17. protected Application mApplication; 18. protected SerialPort mSerialPort; 19. protected OutputStream mOutputStream; 20. private InputStream mInputStream; 21. private ReadThread mReadThread; 22. 23. private class ReadThread extends Thread { 24. 25. @Override 26. public void run() { 27. super.run(); 28. while (!isInterrupted()) { 29. int size; 1. try { 2. byte[] buffer = new byte[64]; 3. if (mInputStream == null) 4. return; 5. 6. /** 7. * 这里的read要尤其注意,它会一直等待数据,等到天荒地老,海枯石烂。如果要判断是否接受完成,只有设置结束标识,或作其他特殊的处理。 8. */ 9. size = mInputStream.read(buffer); 10. if (size > 0) { 11. onDataReceived(buffer, size); 12. } 13. } catch (IOException e) { 14. e.printStackTrace(); 15. return; 16. } 17. } 18. } 19. } 20. 21. private void DisplayError(int resourceId) { 22. AlertDialog.Builder b = new AlertDialog.Builder(this); 23. b.setTitle("Error"); 24. b.setMessage(resourceId); 25. b.setPositiveButton("OK", new OnClickListener() { 26. public void onClick(DialogInterface dialog, int which) { 27. SerialPortActivity.this.finish(); 28. } 29. }); 30. b.show(); 31. } 32. 33. @Override 34. protected void onCreate(Bundle savedInstanceState) { 35. super.onCreate(savedInstanceState); 36. mApplication = (Application) getApplication(); 37. try { 38. mSerialPort = mApplication.getSerialPort(); 39. mOutputStream = mSerialPort.getOutputStream(); 40. mInputStream = mSerialPort.getInputStream(); 41. 42. /* Create a receiving thread */ 43. mReadThread = new ReadThread(); 1. mReadThread.start(); 2. } catch (SecurityException e) { 3. DisplayError(R.string.error_security); 4. } catch (IOException e) { 5. DisplayError(R.string.error_unknown); 6. } catch (InvalidParameterException e) { 7. DisplayError(R.string.error_configuration); 8. } 9. } 10. 11. protected abstract void onDataReceived(final byte[] buffer, final int size); 12. 13. @Override 14. protected void onDestroy() { 15. if (mReadThread != null) 16. mReadThread.interrupt(); 17. mApplication.closeSerialPort(); 18. mSerialPort = null; 19. super.onDestroy(); 20. } 21.} 22. 6)编写string.xml以及baudrates.xml文件 在string.xml文件中添加:[html] viewplaincopy1. Please configure your serial port first. 2. You do not have read/write permission to the serial port. 3. The serial port can not be opened for an unknown reason. 4. 在baudrates.xml文件中添加[html] viewplaincopy1. 2. 1. 2. 3. 50 4. 75 5. 110 6. 134 7. 150 8. 200 9. 300 10. 600 11. 1200 12. 1800 13. 2400 14. 4800 15. 9600 16. 19200 17. 38400 18. 57600 19. 115200 20. 230400 21. 460800 22. 500000 23. 576000 24. 921600 25. 1000000 26. 1152000 27. 1500000 28. 2000000 29. 2500000 30. 3000000 31. 3500000 32. 4000000 33. 34. 35. 50 36. 75 37. 110 38. 134 39. 150 40. 200 41. 300 42. 600 43. 1200 44. 1800 1. 2400 2. 4800 3. 9600 4. 19200 5. 38400 6. 57600 7. 115200 8. 230400 9. 460800 10. 500000 11. 576000 12. 921600 13. 1000000 14. 1152000 15. 1500000 16. 2000000 17. 2500000 18. 3000000 19. 3500000 20. 4000000 21. 22. 23. 24. 7)开始编写界面了:在main.xml布局文件中添加两个编辑框,一个用来发送命令,一个用来接收命令:[html] viewplaincopy1. 2. 6. 7. 2. 3. 4. 10. 11. 12. 13. 8)SerialDemoActivity类的实现:[java] viewplaincopy1.package org.winplus.serial; 2. 3.import java.io.IOException; 4. 5.import android.os.Bundle; 6.import android.view.KeyEvent; 7.import android.widget.EditText; 8.import android.widget.TextView; 9.import android.widget.TextView.OnEditorActionListener; 10. 11.public class SerialDemoActivity extends SerialPortActivity{ 12. EditText mReception; 13. 14. @Override 15. protected void onCreate(Bundle savedInstanceState) { 16. super.onCreate(savedInstanceState); 17. setContentView(R.layout.main); 18. 19.// setTitle("Loopback test"); 20. mReception = (EditText) findViewById(R.id.EditTextReception); 21. 22. EditText Emission = (EditText) findViewById(R.id.EditTextEmission); 23. Emission.setOnEditorActionListener(new OnEditorActionListener() { 24. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 1. int i; 2. CharSequence t = v.getText(); 3. char[] text = new char[t.length()]; 4. for (i=0; i 写到这里,代码基本上写完了。下面就是要实现JNI层的功能了,要实现JNI,必须首先生成头文件,头文件的生成方式也很简单,我们编译工程,在终端输入javahorg.winplus.serial.utils.SerialPort则会生成头文件:org_winplus_serial_utils_SerialPort.h,这个头文件的名字可以随意命名。我们将它命名为:SerialPort.h拷贝到新建的目录jni中,新建SerialPort.c文件,这两个文件的代码就不贴出来了。直接到上传的代码中看吧。 (四)串口的应用,可实现扫描头,指纹识别等外围USB转串口的特色应用。
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处