|
技术讲串口编程只要模仿 资料区的 TQ210android SerailPort.zip就可以(http://www.armbbs.net/forum.php?mod=viewthread&tid=15967&extra=page%3D1%26filter%3Dtypeid%26typeid%3D178),按此思路,下载,抽取核心代码,始终无法运行,每次运行到SerialPort的静态方法中时就会崩溃,这个静态方法就一条语句System.loadLibrary("serial"); 我以为没有libserial.so(/system/lib中确实没有),故也将相应的文件通过u盘copy到了/system/lib目录下。可还是不行,控制台显示错误为:root@android:/ # init: untracked pid 3601 exited
request_suspend_state: sleep (0->3) at 160406618688 (1970-01-02 03:00:45.249258686 UTC)
==ft5x0x_ts_suspend=
request_suspend_state: wakeup (3->0) at 413461212720 (1970-01-02 03:04:58.303852718 UTC)
==ft5x0x_ts_resume=
cpufreq_interactive_input_connect: connect to ft5x06_ts
以下是主要的程序语句, searialPort来自SerialPort.zip
SerialPort sp = new SerialPort(f, 115200);
OutputStream out = sp.getOutputStream();
String dataString = "123456789";
out.write( dataString.getBytes() );
out.close();
SerialPort类定义:
public class SerialPort {
private static final String TAG = "SerialPort";
/*
* Do not remove or rename the field mFd: it is used by native method close();
*/
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate) throws SecurityException, IOException {
/* 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);
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
// Getters and setters
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");
}
}
参考帖子(http://www.armbbs.net/forum.php? ... &extra=page%3D2)后发现,直接通过以下代码就可以发送,不过这与demo中的实现方式有什么区别? 只发现下面的代码是没有波特率的,没有JNI。
File file = new File(/dev/ttySAC0");
OutputStream oStream = new FileOutputStream( file );
String dataString = "123456789";
oStream.write( dataString.getBytes() );
oStream.close();
请大侠帮忙解决一下,先谢谢了
|
|