linux 驱动中调用串口,在android系统中的linux层调用串口驱动读取数据-版主一定要帮助看一下Android,include,LINUX,根目录,新建文件夹 - Powered by D...

这几天我在android的linux层做一个串口通信程序(主要是想摆脱NDK的约束,用C来完成所用功能,就像在LInux下一样),调试始终有问题(将程序放到开发板上,然后通过串口与计算机相连,使用串口调试程序来测试,结果在串口调试助手中要么显示乱码,要么就没有数据)

下面是我的设计步骤:

1、进入android源码的根目录,在/exteral下新建文件夹SerialComm,在SerialComm文件夹下新建源文件SerialComm.c和Android.mk。

源文件内容:

SerialComm.c :

#include

#include

#include

#include

#include

#define FALSE -1

#define TRUE 0

/**

*@brief  设置串口通信速率

*@param  fd     类型 int  打开串口的文件句柄

*@param  speed  类型 int  串口速度

*@return  void

*/

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300, 38400,

19200,  9600, 4800, 2400, 1200,  300, };

void set_speed(int fd, int speed){

int   i;

int   status;

struct termios   Opt;

tcgetattr(fd, &Opt);

for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)

{

if  (speed == name_arr[i])

{

/**

* tcflush函数刷清(抛弃)输入缓存(终端驱动程序已接收到,但用户程序尚未读)或输出缓存(用户程序已经写,但尚未发送)。queue参数应是下列三个常数之一:

* TCIFLUSH刷清输入队列。

* TCOFLUSH刷清输出队列。

* TCIOFLUSH刷清输入、输出队列。

*/

tcflush(fd, TCIOFLUSH);//设置前flush

cfsetispeed(&Opt, speed_arr[i]);

cfsetospeed(&Opt, speed_arr[i]);

//通过tcsetattr函数把新的属性设置到串口上。

//tcsetattr(串口描述符,立即使用或者其他标示,指向termios的指针)

status = tcsetattr(fd, TCSANOW, &Opt);

if  (status != 0)

{

perror("tcsetattr fd1");

return;

}

tcflush(fd,TCIOFLUSH);  //设置后flush

}

}

}

/**

*@brief   设置串口数据位,停止位和效验位

*@param  fd     类型  int  打开的串口文件句柄

*@param  databits 类型  int 数据位   取值 为 7 或者8

*@param  stopbits 类型  int 停止位   取值为 1 或者2

*@param  parity  类型  int  效验类型 取值为N,E,O,,S

*/

int set_Parity(int fd, int databits, int stopbits, int parity)

{

struct termios options;

if (tcgetattr(fd, &options) != 0)

{

perror("SetupSerial 1");

return (FALSE);

}

options.c_cflag &= ~CSIZE;

switch (databits)

/*设置数据位数*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size\n");

return (FALSE);

}

switch (parity)

{

case 'n':

case 'N':

options.c_cflag &= ~PARENB; /* Clear parity enable */

options.c_iflag &= ~INPCK; /* Enable parity checking */

break;

case 'o':

case 'O':

options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/

options.c_iflag |= INPCK; /* Disnable parity checking */

break;

case 'e':

case 'E':

options.c_cflag |= PARENB; /* Enable parity */

options.c_cflag &= ~PARODD; /* 转换为偶效验*/

options.c_iflag |= INPCK; /* Disnable parity checking */

break;

case 'S':

case 's': /*as no parity*/

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;

break;

default:

fprintf(stderr,"Unsupported parity\n");

return (FALSE);

}

/* 设置停止位*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits\n");

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd, TCIFLUSH);

options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/

options.c_cc[VMIN] = 0; /* Update the options and do it NOW */

if (tcsetattr(fd, TCSANOW, &options) != 0)

{

perror("SetupSerial 3");

return (FALSE);

}

return (TRUE);

}

int main(int argc,char* argv[])

{

//open serial with block

int fd=open("/dev/s3c2410_serial1",O_RDWR);

if(fd==-1)

{

perror("open error");

return 0;

}

printf("serial1 is be open fd=%d\n",fd);

set_speed(fd,4800);

if(set_Parity(fd,8,1,'N')==FALSE)

{

printf("Failed to set serial properiy");

close(fd);

return 0;

}

int iTmp=write(fd,"hello",strlen("hello"));

printf("iTmp=%d\n",iTmp);

char chTmp[50];

iTmp=read(fd,chTmp,50);

printf("chTmp:%s\n",chTmp);

close(fd);

return 0;

}

Android.mk:

LOCAL_PATH:=$(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS:=optional

LOCAL_SRC_FILES:=SerialComm.c

LOCAL_MODULE:=SerialComm

include $(BUILD_EXECUTABLE)

在android源码的根目录执行:make SerialComm

root@zero-desktop:~/forLinux/android2.3.4_32bit# make SerialComm

============================================

PLATFORM_VERSION_CODENAME=REL

PLATFORM_VERSION=2.3.4

TARGET_PRODUCT=generic

TARGET_BUILD_VARIANT=eng

TARGET_SIMULATOR=

TARGET_BUILD_TYPE=release

TARGET_BUILD_APPS=

TARGET_ARCH=arm

HOST_ARCH=x86

HOST_OS=linux

HOST_BUILD_TYPE=release

BUILD_ID=GRJ06D

============================================

find: `frameworks/base/frameworks/base/docs/html': No such file or directory

find: `out/target/common/docs/gen': No such file or directory

find: `frameworks/base/frameworks/base/docs/html': No such file or directory

find: `out/target/common/docs/gen': No such file or directory

find: `frameworks/base/frameworks/base/docs/html': No such file or directory

find: `out/target/common/docs/gen': No such file or directory

find: `frameworks/base/frameworks/base/docs/html': No such file or directory

find: `out/target/common/docs/gen': No such file or directory

find: `frameworks/base/frameworks/base/docs/html': No such file or directory

find: `out/target/common/docs/gen': No such file or directory

Install: out/host/linux-x86/bin/apriori

Install: out/host/linux-x86/bin/soslim

target Non-prelinked: SerialComm (out/target/product/generic/symbols/system/bin/SerialComm)

Install: out/target/product/generic/system/lib/libdl.so

Install: out/target/product/generic/system/lib/libc.so

Install: out/target/product/generic/system/lib/libstdc++.so

Install: out/target/product/generic/system/lib/libm.so

Install: out/target/product/generic/system/bin/SerialComm

root@zero-desktop:~/forLinux/android2.3.4_32bit#

将SerialComm拷贝到开发板的/root下,连接好串口以及设置好串口调试助手,执行./SerialComm,但是在串口调试助手里面并没有由SerialComm发送过来的数据,同时也不能接收数据。

烦请你帮我看一下问题所在,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux调用RS232驱动可以通过以下步骤进行: 1. 确认串口设备:首先,需要确认系统串口设备。你可以使用以下命令来查看系统串口设备列表: ```shell ls /dev/ttyS* ``` 这将列出系统所有的串口设备,如`/dev/ttyS0`、`/dev/ttyS1`等。 2. 配置串口参数:接下来,需要配置串口的参数,如波特率、数据位、校验位等。可以使用`stty`命令来设置串口参数。例如,下面的命令设置波特率为9600、数据位为8、无校验位、停止位为1: ```shell stty -F /dev/ttyS0 9600 cs8 -cstopb -parity ``` 3. 打开和关闭串口:使用文件操作函数来打开和关闭串口设备。在C语言,可以使用`open()`函数打开串口设备文件,并使用`close()`函数关闭串口设备文件。例如: ```c #include <fcntl.h> #include <unistd.h> int fd; fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); // 使用串口设备进行数据读写 close(fd); ``` 4. 读写数据:通过打开串口设备文件进行数据的读写操作。可以使用`read()`函数从串口读取数据,使用`write()`函数向串口写入数据。例如: ```c #include <fcntl.h> #include <unistd.h> int fd; char buffer[256]; ssize_t n; fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); n = read(fd, buffer, sizeof(buffer)); // 处理读取到的数据 n = write(fd, "Hello", 5); // 写入数据到串口 close(fd); ``` 这是一个简单的示例,实际的代码可能需要根据具体的需求进行更多的配置和处理。同时,需要注意对串口设备的访问权限,可能需要以root或者具有串口访问权限的用户身份来运行程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值