|
E9 Android 打开串口/dev/ttySAC3,通过升压模块,接到台式机的串口上,在Windows客户端使用串口调试助手打开接受数据。每一次在E9上面调用JNI接口函数OpenDoor发送的数据,在串口调试助手都只是能够接受到十六进制的00,下面是我使用的JNI接口函数。
/*
* Copyright 2009-2011 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h>
#include "SerialPort.h"
#include "android/log.h"
static const char *TAG="serial_port";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
char data[] = "1212121212121212";
static speed_t getBaudrate(jint baudrate)
{
switch(baudrate) {
case 0: return B0;
case 50: return B50;
case 75: return B75;
case 110: return B110;
case 134: return B134;
case 150: return B150;
case 200: return B200;
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 1800: return B1800;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
case 230400: return B230400;
case 460800: return B460800;
case 500000: return B500000;
case 576000: return B576000;
case 921600: return B921600;
case 1000000: return B1000000;
case 1152000: return B1152000;
case 1500000: return B1500000;
case 2000000: return B2000000;
case 2500000: return B2500000;
case 3000000: return B3000000;
case 3500000: return B3500000;
case 4000000: return B4000000;
default: return -1;
}
}
// 串口操作句柄
int fd = -1;
/*
* Class: android_serialport_SerialPort
* Method: open
* Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
*/
int Java_com_example_androidserialportapi_SerialPort_open (JNIEnv *env, jobject thiz, jstring path, jint baudrate, jint flags)
{
speed_t speed;
jobject mFileDescriptor;
/* Check arguments */
{
speed = getBaudrate(9600);
if (speed == -1) {
/* TODO: throw an exception */
LOGE("Invalid baudrate");
return -1;
}
}
/* Opening device */
{
jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
//LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR|O_NOCTTY|O_NDELAY);
fd = open(path_utf, O_RDWR|O_NOCTTY|O_NDELAY);
LOGD("open() fd = %d", fd);
(*env)->ReleaseStringUTFChars(env, path, path_utf);
if (fd == -1)
{
/* Throw an exception */
LOGE("Cannot open port");
/* TODO: throw an exception */
return -1;
}
}
/* Configure device */
{
/*恢复串口为阻塞状态*/
if(fcntl(fd, F_SETFL, 0)<0)
LOGE("fcntl failed!\n");
else
LOGE("fcntl=%d\n",fcntl(fd, F_SETFL,0));
struct termios cfg;
LOGD("Configuring serial port");
if (tcgetattr(fd, &cfg))
{
LOGE("tcgetattr() failed");
close(fd);
/* TODO: throw an exception */
return -1;
}
bzero( &cfg, sizeof( cfg ) );
cfsetispeed(&cfg, speed);
cfsetospeed(&cfg, speed);
/*步骤一,设置字符大小*/
cfg.c_cflag |= CLOCAL | CREAD;
cfg.c_cflag &= ~CSIZE;
cfg.c_cflag &= ~PARENB; // 没有奇偶校验
cfg.c_cflag |= CS8; // 设置数据位为8
cfg.c_cflag &= ~CSTOPB; // 将停止位设置为一个比特
/*设置等待时间和最小接收字符*/
cfg.c_cc[VTIME] = 10;
cfg.c_cc[VMIN] = 0;
/*处理未接收字符*/
tcflush(fd,TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &cfg))
{
LOGE("tcsetattr() failed");
close(fd);
/* TODO: throw an exception */
return -1;
}
}
return 0;
}
/*
* Class: cedric_serial_SerialPort
* Method: close
* Signature: ()V
*/
void Java_com_example_androidserialportapi_SerialPort_close (JNIEnv *env, jobject thiz)
{
if( fd != -1 )
close(fd);
}
// 打开继电器
void Java_com_example_androidserialportapi_SerialPort_OpenDoor (JNIEnv *env, jobject thiz)
{
if( fd != -1 )
{
write( fd, data, strlen(data) );
}
}
// 关闭继电器
void Java_com_example_androidserialportapi_SerialPort_CloseDoor (JNIEnv *env, jobject thiz)
{
if( fd != -1 )
{
write( fd, data, strlen(data) );
}
}
麻烦大神帮忙看一下出什么问题了
|
|