Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2159690
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2015-09-18 11:15:47

1. AT的shell
  1. #define LOG_TAG "SPITEST"
  2. //#define LOG_NDEBUG 0

  3. #include <fcntl.h>
  4. #include <sys/prctl.h>
  5. #include <sys/wait.h>
  6. #include <binder/IPCThreadState.h>
  7. #include <binder/ProcessState.h>
  8. #include <binder/IServiceManager.h>
  9. #include <cutils/properties.h>
  10. #include <utils/Log.h>

  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "SPIData.h"
  14. #include "NetClient.h"

  15. #include <termios.h>
  16. #include <unistd.h>

  17. using namespace android;

  18. char recv_buf[1024];
  19. int fd = -1;
  20. static void *readerLoop(void *arg)
  21. {
  22.     int ret;
  23.     while(1)
  24.     {
  25.         memset(recv_buf, 0, sizeof(recv_buf));
  26.         ret = read(fd, recv_buf, sizeof(recv_buf));
  27.         if(ret < 0)
  28.             printf("read error\n");

  29.         printf("%s", recv_buf);
  30.     }
  31.     return NULL;
  32. }

  33. int main(int argc, char*argv[])
  34. {
  35.     struct termios termOptions;
  36.     int ret,i;
  37.     pthread_t s_tid_reader;
  38.     pthread_attr_t attr;
  39.     char send_buf[1024];
  40.     char prev_buf[1024];
  41.     //char send_buf[1024]="AT+COPN=?";
  42.     //char send_buf[1024]="AT+CRSM=?";
  43.     //char send_buf[1024]="AT+CGMI";
  44.     //char send_buf[1024]="ATE\n";
  45.     //char send_buf[1024]="ATI\n";
  46.     fd=open("/dev/ttyMT3", O_RDWR);

  47.     // fcntl(fd, F_SETFL, 0);
  48.     //1. 串口的配置
  49.     // Get the current options:
  50.     tcgetattr(fd, &termOptions);
  51.   
  52.     // Set 8bit data, No parity, stop 1 bit (8N1):
  53.     termOptions.c_cflag &= ~PARENB; //no parity
  54.     termOptions.c_cflag &= ~CSTOPB; //1 stop bit
  55.     termOptions.c_cflag &= ~CSIZE;
  56.     termOptions.c_cflag |= CS8 | CLOCAL | CREAD;
  57.     termOptions.c_cflag &= ~CRTSCTS;
  58.     // Raw mode
  59.        termOptions.c_iflag &= ~(INLCR | ICRNL | IXON | IXOFF | IXANY);
  60.     termOptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*raw input*/
  61.     termOptions.c_oflag &= ~OPOST; /*raw output*/
  62.     termOptions.c_oflag |= ONLCR; /*raw output*/

  63.     tcflush(fd,TCIFLUSH);//clear input buffer
  64.     termOptions.c_cc[VTIME] = 10; /* inter-character timer unused, wait 1s, if no data, return */
  65.     termOptions.c_cc[VMIN] = 0; /* blocking read until 0 character arrives */

  66.     cfsetispeed(&termOptions, B115200); /*set baudrate to 115200, which is 3332 default bd*/
  67.     cfsetospeed(&termOptions, B115200);
  68.     tcsetattr(fd, TCSANOW, &termOptions);

  69.     //read thread
  70.     pthread_attr_init (&attr);
  71.     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  72.     ret = pthread_create(&s_tid_reader, &attr, readerLoop, &attr);    //3.接收并不与发送同步所以这儿需要加一个线程来不断的读取
  73.     if (ret < 0) {
  74.         perror ("pthread_create");
  75.         return -1;
  76.     }

  77.     while(fgets(send_buf,sizeof(send_buf),stdin) != NULL)
  78.     {
  79.         int len = strlen(send_buf);
  80.         //for(i=0; i<len; i++)
  81.         // printf("%d=0x%x\n", i, send_buf[i]);
  82.         //printf("\n");
  83.         if( (0x1b==send_buf[0]) && (0x5b==send_buf[1]) && (0x41==send_buf[2]))
  84.         {
  85.             //up arrow pressed
  86.             ret = write(fd, prev_buf, strlen(prev_buf));
  87.         }else if(0x0a==send_buf[0]) {
  88.             memset(send_buf, 0, sizeof(send_buf));
  89.         }else {
  90.             send_buf[len-1] = '\r';                //2.指令必须以\r\n结束,否则gprs模块不认这条指令
  91.             send_buf[len] = '\0';                  //据说可以通过配置串口来自动发送\r\n,但是没有成功
  92. #if 0                                              //有知道的告诉一下,谢谢!!!!
  93.             send_buf[len-1] = '\r';
  94.             send_buf[len] = '\n';
  95.             send_buf[len+1]='\0';
  96. #endif
  97.             ret = write(fd, send_buf, strlen(send_buf));
  98.             if(ret < 0)
  99.                 printf("write error\n");
  100.             memset(prev_buf, 0, sizeof(prev_buf));
  101.             memcpy(prev_buf, send_buf, strlen(send_buf));
  102.             memset(send_buf, 0, sizeof(send_buf));
  103.         }
  104.     #if 0
  105.         memset(recv_buf, 0, sizeof(recv_buf));
  106.         ret = read(fd, recv_buf, sizeof(recv_buf));
  107.         if(ret < 0)
  108.             printf("read error\n");

  109.         printf("%s", recv_buf);
  110.     #endif
  111.     }
  112.     return 0;
  113. }
这儿有三点需要注意:
    a. 串口的配置,如何设置8n1
    b. 发送的字符串必须是以 \r\n结束,要不gprs模块不认这个指令
    c. 加了一个read的线程,是因为接收并不与发送同步
2.
Android.mk
  1. LOCAL_PATH:= $(call my-dir)

  2. include $(CLEAR_VARS)

  3. LOCAL_SRC_FILES:= \
  4.     main_spi.cpp


  5. LOCAL_SHARED_LIBRARIES := \
  6.     libutils \
  7.     libcutils \
  8.     liblog \
  9.     libhardware_legacy \
  10.     libbinder

  11. LOCAL_C_INCLUDES := $(JNI_H_INCLUDE) \
  12.     frameworks/base/services/input
  13. LOCAL_MODULE_TAGS := optional

  14. LOCAL_MODULE:= spi_server

  15. include $(BUILD_EXECUTABLE)

3.
编译命令
  1. ./mk -t mm ./frameworks/ireadygo/server/native
  2. adb remount
  3. adb push out/target/product/mt92_wet_jb9/system/lib/libgprs_ireadygo.so /system/lib/
  4. adb push out/target/product/mt92_wet_jb9/system/bin/spi_gprs /system/bin/spi_gprs
  5. adb shell sync





阅读(1320) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~