串口小项目 - 声控刷抖音

项目准备: 

 orangepi02

语言 模块: SU-03T

电脑


接线:


语言模块 - orangepi
VCC - 5V
GND -  GND
B7(RX)--RX-5

orangepi  手机 通过usb 连接

实现思路图:

语言模块接收到语言信息,发送到 H616 去处理,H616再控制手机实现语言刷抖音的功能


====================== 
插入手机后,使用 指令:  dmesg  -- 查看信息:

 
//手机的内核(安卓的底层实现)也是linux实现的

先下载adb:  sudo apt-get install adb

再使用adb下的命令得到手机编号:

adb  -- Android 调试桥 (Android Debug Bridge )

adb devices

adb shell --  需要手机 的授权 “USB调试”


 

项目代码:

//同步在资源中

muUart.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>


#include<pthread.h>
#include "uartTool.h"

int fd;

void *readSerial()
{
	char cmd;
	while(1){
		cmd=myserialGetchar(fd);
		printf("GET->0x%c\n",cmd);
      
    switch(cmd){
        case 'N':
		puts("next");	
        system("adb shell input swipe 540 1300 540 500 100");
		break;	
	
	
        case 'P':
		puts("pre");	
        system("adb shell input swipe 540 500 540 1300 100");
        break;	
	
	
        case 'Z':
		puts("zan");	
        system("adb shell \"seq 2 | while read i;do input tap 350 1050 & input tap 350 1050 &sleep 0.03;done;\"");

        break;	
	
	
        case 'Q':
		puts("quit");	
        system("adb shell input keyevent 26");
        break;	
	
	}


	}

}


void *sendSerial()
{

	char buf[32];
	while(1){
		memset(buf,'\0',sizeof(buf));
		scanf("%s",buf);
		serialsendString(fd,buf);  
	}


}

int main(int argc,char **argv)
{
	char  deviceName[32]={'\0'};
	pthread_t readt;


	if(argc<2){
		printf("uage:%s /dev/ttyS?\n",argv[0]);

		return -1;
	}

	strcpy(deviceName,argv[1]);
	if((fd=myserialOpen(deviceName,115200))==-1){
		printf("open %s error\n",deviceName);
		return -1;
	}

	pthread_create(&readt,NULL,readSerial ,NULL);

	while(1)
	{
		sleep(10);
	}

	return 0;
}

uartTool.c


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "wiringSerial.h"

char myserialGetchar (const int fd)
{
  char x ;

  if (read (fd, &x, 1) != 1)
    return -1 ;

  return x;
}


int myserialOpen (const char *device, const int baud)
{

	struct termios options ;
	speed_t myBaud ;
	int 	status, fd ;

	 switch (baud)
    {
    
case    9600:	myBaud =    B9600 ; break ;
	 case  115200:	myBaud =  B115200 ; break ;
	}

	 if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
	   return -1 ;

	 fcntl (fd, F_SETFL, O_RDWR) ;
	 
	 // Get and modify current options:
	 
	   tcgetattr (fd, &options) ;
	 
		 cfmakeraw	 (&options) ;
		 cfsetispeed (&options, myBaud) ;
		 cfsetospeed (&options, myBaud) ;
	 
		 options.c_cflag |= (CLOCAL | CREAD) ;
		 options.c_cflag &= ~PARENB ;
		 options.c_cflag &= ~CSTOPB ;
		 options.c_cflag &= ~CSIZE ;
		 options.c_cflag |= CS8 ;
		 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
		 options.c_oflag &= ~OPOST ;
	 
		 options.c_cc [VMIN]  =   0 ;
		 options.c_cc [VTIME] = 100 ;	 // Ten seconds (100 deciseconds)
	 
	   tcsetattr (fd, TCSANOW, &options) ;
	 
	   ioctl (fd, TIOCMGET, &status);
	 
	   status |= TIOCM_DTR ;
	   status |= TIOCM_RTS ;
	 
	   ioctl (fd, TIOCMSET, &status);
	 
	   usleep (10000) ;  // 10mS
	 
	   return fd ;



}


void serialsendString (const int fd, const char* s)
{
  int ret;
  ret=write(fd,s,strlen(s));
  if(ret<0)
  	printf("Serial Puts Error\n");
}

int serialgetString (const int fd, char* buffer)
{
  int n_read;
  n_read=read(fd,buffer,32);
  return n_read;

}





uartTool.h

int myserialOpen (const char *device, const int baud);
void serialsendString (const int fd, const char* s);
int serialgetString (const int fd, char* buffer);


char myserialGetchar (const int fd);


操作步骤: 

1.按接线要求 接好线 (注意我们代码接的是串口5 ,波特率115200)

2.编译: gcc myUart.c uartTool.c -lpthread

3. 运行 ./a.out /dev/ttyS5  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值