Android 操作串口 (android serial port api)
來源:程序員人生 發布時間:2014-12-10 08:25:28 閱讀次數:5542次
頭幾天公司有通過搭載Android系統的開發板來使用打卡機統計數據的需求,對攻城獅來講就需要在Android平臺上讀寫打卡機的串口,在網上搜索1些東西以后發現了在google code 上的android serial port api可以用,墻了以后拿到源碼發現還有demo,不錯不錯,我這個帖子就通過serial port api的源碼簡單得實現1個讀寫串口,這個固然是在native寫的,如果還有哪些童鞋不清楚android上使用jni和native的話可以跳轉到我的上篇帖子 點我點我
在Android工程中建立1個工具類,該類的作用就是通過調用Jni中聲明的Native方法openSerialPort,來打開參數中path的串口,該串口的具體參數為:baudrate比特率,databits數據位,stopbits停止位,和parity奇偶校驗.如果打開成功的話該方法會返回path串口的文件描寫實例,這樣就能夠獲得到能讀寫該串口的IO流,串口對我們來講就能夠當做1個文件(在linux系統上確切是1個裝備文件).成功就按按著不同的命令或數據協議R/W串口就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();
private static SerialPortUtil mInstance = null;
private StudioJni studioJni = StudioJni.getInstance();
private FileDescriptor mFd;
private FileInputStream mFileInputStream = null;
private FileOutputStream mFileOutputStream = null;
private boolean isRunning = false;
public static SerialPortUtil getInstance(){
if(mInstance == null){
mInstance = new SerialPortUtil();
}
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;
}
}
在Native實現中,就是拿著配置下來的參數來打開串口,配置串口.成功了就返回1個java的文件描寫,失敗返回空.
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 == ⑴) {
/* 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 == ⑴)
{
/* 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(&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/FileDescriptor");
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;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈