1. AT的shell
-
#define LOG_TAG "SPITEST"
-
//#define LOG_NDEBUG 0
-
-
#include <fcntl.h>
-
#include <sys/prctl.h>
-
#include <sys/wait.h>
-
#include <binder/IPCThreadState.h>
-
#include <binder/ProcessState.h>
-
#include <binder/IServiceManager.h>
-
#include <cutils/properties.h>
-
#include <utils/Log.h>
-
-
#include <stdio.h>
-
#include <string.h>
-
#include "SPIData.h"
-
#include "NetClient.h"
-
-
#include <termios.h>
-
#include <unistd.h>
-
-
using namespace android;
-
-
char recv_buf[1024];
-
int fd = -1;
-
static void *readerLoop(void *arg)
-
{
-
int ret;
-
while(1)
-
{
-
memset(recv_buf, 0, sizeof(recv_buf));
-
ret = read(fd, recv_buf, sizeof(recv_buf));
-
if(ret < 0)
-
printf("read error\n");
-
-
printf("%s", recv_buf);
-
}
-
return NULL;
-
}
-
-
int main(int argc, char*argv[])
-
{
-
struct termios termOptions;
-
int ret,i;
-
pthread_t s_tid_reader;
-
pthread_attr_t attr;
-
char send_buf[1024];
-
char prev_buf[1024];
-
//char send_buf[1024]="AT+COPN=?";
-
//char send_buf[1024]="AT+CRSM=?";
-
//char send_buf[1024]="AT+CGMI";
-
//char send_buf[1024]="ATE\n";
-
//char send_buf[1024]="ATI\n";
-
fd=open("/dev/ttyMT3", O_RDWR);
-
-
// fcntl(fd, F_SETFL, 0);
-
//1. 串口的配置
-
// Get the current options:
-
tcgetattr(fd, &termOptions);
-
-
// Set 8bit data, No parity, stop 1 bit (8N1):
-
termOptions.c_cflag &= ~PARENB; //no parity
-
termOptions.c_cflag &= ~CSTOPB; //1 stop bit
-
termOptions.c_cflag &= ~CSIZE;
-
termOptions.c_cflag |= CS8 | CLOCAL | CREAD;
-
termOptions.c_cflag &= ~CRTSCTS;
-
// Raw mode
-
termOptions.c_iflag &= ~(INLCR | ICRNL | IXON | IXOFF | IXANY);
-
termOptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*raw input*/
-
termOptions.c_oflag &= ~OPOST; /*raw output*/
-
termOptions.c_oflag |= ONLCR; /*raw output*/
-
-
tcflush(fd,TCIFLUSH);//clear input buffer
-
termOptions.c_cc[VTIME] = 10; /* inter-character timer unused, wait 1s, if no data, return */
-
termOptions.c_cc[VMIN] = 0; /* blocking read until 0 character arrives */
-
-
cfsetispeed(&termOptions, B115200); /*set baudrate to 115200, which is 3332 default bd*/
-
cfsetospeed(&termOptions, B115200);
-
tcsetattr(fd, TCSANOW, &termOptions);
-
-
//read thread
-
pthread_attr_init (&attr);
-
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
ret = pthread_create(&s_tid_reader, &attr, readerLoop, &attr); //3.接收并不与发送同步所以这儿需要加一个线程来不断的读取
-
if (ret < 0) {
-
perror ("pthread_create");
-
return -1;
-
}
-
-
while(fgets(send_buf,sizeof(send_buf),stdin) != NULL)
-
{
-
int len = strlen(send_buf);
-
//for(i=0; i<len; i++)
-
// printf("%d=0x%x\n", i, send_buf[i]);
-
//printf("\n");
-
if( (0x1b==send_buf[0]) && (0x5b==send_buf[1]) && (0x41==send_buf[2]))
-
{
-
//up arrow pressed
-
ret = write(fd, prev_buf, strlen(prev_buf));
-
}else if(0x0a==send_buf[0]) {
-
memset(send_buf, 0, sizeof(send_buf));
-
}else {
-
send_buf[len-1] = '\r'; //2.指令必须以\r\n结束,否则gprs模块不认这条指令
-
send_buf[len] = '\0'; //据说可以通过配置串口来自动发送\r\n,但是没有成功
-
#if 0 //有知道的告诉一下,谢谢!!!!
-
send_buf[len-1] = '\r';
-
send_buf[len] = '\n';
-
send_buf[len+1]='\0';
-
#endif
-
ret = write(fd, send_buf, strlen(send_buf));
-
if(ret < 0)
-
printf("write error\n");
-
memset(prev_buf, 0, sizeof(prev_buf));
-
memcpy(prev_buf, send_buf, strlen(send_buf));
-
memset(send_buf, 0, sizeof(send_buf));
-
}
-
#if 0
-
memset(recv_buf, 0, sizeof(recv_buf));
-
ret = read(fd, recv_buf, sizeof(recv_buf));
-
if(ret < 0)
-
printf("read error\n");
-
-
printf("%s", recv_buf);
-
#endif
-
}
-
return 0;
-
}
这儿有三点需要注意:
a. 串口的配置,如何设置8n1
b. 发送的字符串必须是以 \r\n结束,要不gprs模块不认这个指令
c. 加了一个read的线程,是因为接收并不与发送同步
2.
Android.mk
-
LOCAL_PATH:= $(call my-dir)
-
-
include $(CLEAR_VARS)
-
-
LOCAL_SRC_FILES:= \
-
main_spi.cpp
-
-
-
LOCAL_SHARED_LIBRARIES := \
-
libutils \
-
libcutils \
-
liblog \
-
libhardware_legacy \
-
libbinder
-
-
LOCAL_C_INCLUDES := $(JNI_H_INCLUDE) \
-
frameworks/base/services/input
-
LOCAL_MODULE_TAGS := optional
-
-
LOCAL_MODULE:= spi_server
-
-
include $(BUILD_EXECUTABLE)
3.
编译命令
-
./mk -t mm ./frameworks/ireadygo/server/native
-
adb remount
-
adb push out/target/product/mt92_wet_jb9/system/lib/libgprs_ireadygo.so /system/lib/
-
adb push out/target/product/mt92_wet_jb9/system/bin/spi_gprs /system/bin/spi_gprs
-
adb shell sync
阅读(1320) | 评论(0) | 转发(0) |