serial在c语言,serial.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "serial.h"

#include "debug.h"

#define SERIAL_PORTING_DEBUG

int uart_setopt(int sfd,int NBits, unsigned char NEvent, int NSpeed, int NStop)

{

struct termios newtio;

struct termios oldtio;

if(tcgetattr(sfd,&oldtio) != 0)

{

SYLOGE("SetupSerial 1\n");

return FAIL;

}

bzero(&newtio,sizeof(newtio));

newtio.c_cflag |= CLOCAL |CREAD;

newtio.c_cflag &= ~CSIZE;

switch(NBits)

{

case 7:

newtio.c_cflag |= CS7;

break;

case 8:

newtio.c_cflag |= CS8;

break;

}

switch(NEvent)

{

case 'O':

newtio.c_cflag |= PARENB;

newtio.c_cflag |= PARODD;

newtio.c_iflag |= (INPCK | ISTRIP);

break;

case 'E':

newtio.c_iflag |= (INPCK |ISTRIP);

newtio.c_cflag |= PARENB;

newtio.c_cflag &= ~PARODD;

break;

case 'N':

newtio.c_cflag &= ~PARENB;

break;

}

switch(NSpeed)

{

case 2400:

cfsetispeed(&newtio,B2400);

cfsetospeed(&newtio,B2400);

break;

case 4800:

cfsetispeed(&newtio,B4800);

cfsetospeed(&newtio,B4800);

break;

case 9600:

cfsetispeed(&newtio,B9600);

cfsetospeed(&newtio,B9600);

break;

case 19200:

cfsetispeed(&newtio,B19200);

cfsetospeed(&newtio,B19200);

break;

case 115200:

cfsetispeed(&newtio,B115200);

cfsetospeed(&newtio,B115200);

break;

case 460800:

cfsetispeed(&newtio,B460800);

cfsetospeed(&newtio,B460800);

break;

default:

cfsetispeed(&newtio,B9600);

cfsetospeed(&newtio,B9600);

break;

}

if(NStop == 1)

{

newtio.c_cflag &= ~CSTOPB;

}

else if(NStop ==2)

{

newtio.c_cflag |= CSTOPB;

}

newtio.c_cc[VTIME] = 0;

newtio.c_cc[VMIN] = 0;

tcflush(sfd,TCIOFLUSH);

if((tcsetattr(sfd,TCSANOW,&newtio)) != 0)

{

SYLOGE("com set error\n");

return FAIL;

}

//SERIAL_DEBUG(IRIS_PF_INFO,"set done!\n");

return SUCCSSE;

}

int uart_devinit(int* sfd,const char* name,int NBits, char NEvent,int NSpeed,int NStop)

{

int i;

*sfd= open(name,O_RDWR | O_NOCTTY | O_NDELAY);

if(*sfd == INVALL_DEV)

{

SYLOGE("open tty fail\n");

return FAIL;

}

fcntl(INVALL_DEV, F_SETFL, 0);//

if(uart_setopt(*sfd,NBits,NEvent,NSpeed,NStop) < 0)

{

SYLOGE("set_opt error\n");

return FAIL;

}

return SUCCSSE;

}

int uart_devread(int sfd,unsigned char* readb,int maxlen, int timeout)

{

int nread = 0;

int time = 0;

int len = 0;

int i=0;

struct timeval start,stop,diff;

int gettimes = 0;

gettimeofday(&start,0);

if(sfd == INVALL_DEV)

{

SYLOGE("tty fail\n");

return FAIL;

}

while(1)

{

gettimeofday(&stop,0);

tim_subtract(&diff,&start,&stop);

if(diff.tv_sec*1000000 + diff.tv_usec > 1000000 * timeout)

{

return TIMEOUT;

}

nread = read(sfd,readb+len,maxlen-len);

if(nread<0)

{

SYLOGE("device is error\n");

return FAIL;

}

else

{

if(nread == (maxlen-len))

{

return SUCCSSE;

}

else if(nread == 0)

{

usleep(20000);

continue;

}

else

{

len += nread;

//ALOGE("get %d left:%d:%d\n",len,maxlen-len,nread);

continue;

}

}

}

return FAIL;

}

int uart_devread_ms(int sfd,unsigned char* readb,int maxlen, int timeout)

{

int nread = 0;

int time = 0;

int len = 0;

int i=0;

struct timeval start,stop,diff;

int gettimes = 0;

gettimeofday(&start,0);

if(sfd == INVALL_DEV)

{

SYLOGE("tty fail\n");

return FAIL;

}

while(1)

{

gettimeofday(&stop,0);

tim_subtract(&diff,&start,&stop);

if(diff.tv_sec*1000000 + diff.tv_usec > 1000 * timeout)

{

return TIMEOUT;

}

nread = read(sfd,readb+len,maxlen-len);

if(nread<0)

{

SYLOGE("device is error\n");

return FAIL;

}

else

{

if(nread == (maxlen-len))

{

return SUCCSSE;

}

else if(nread == 0)

{

usleep(20000);

continue;

}

else

{

len += nread;

//ALOGE("get %d left:%d:%d\n",len,maxlen-len,nread);

continue;

}

}

}

return FAIL;

}

int uart_devclose(int sfd)

{

if(sfd == INVALL_DEV)

{

SYLOGE("DevClose a invall fd\n");

return FAIL;

}

close(sfd);

sfd = INVALL_DEV;

return SUCCSSE;

}

int uart_devwrite(int sfd,unsigned char* writeb,int maxlen)

{

int sendlen = 0;

int len = 0;

if(sfd == INVALL_DEV)

{

SYLOGE("DevWrite a invall fd\n");

return FAIL;

}

//tcflush(sfd,TCOFLUSH);

while(1)

{

sendlen = write(sfd,writeb+len,maxlen-len);

len += sendlen;

if(len == maxlen){

break;

}

}

return SUCCSSE;

}

int tim_subtract(struct timeval *result, struct timeval *x, struct timeval *y)

{

if ( x->tv_sec > y->tv_sec )

{

return-1;

}

if ((x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec))

{

return-1;

}

result->tv_sec = ( y->tv_sec-x->tv_sec );

result->tv_usec = ( y->tv_usec-x->tv_usec );

if (result->tv_usec<0)

{

result->tv_sec--;

result->tv_usec += 1000000;

}

return0;

}

void time_udelay(U32 us)

{

struct timeval start,stop,diff;

int gettimes = 0;

gettimeofday(&start,0);

while(1)

{

gettimeofday(&stop,0);

tim_subtract(&diff,&start,&stop);

if(diff.tv_usec >= us)

{

return;

}

}

return;

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: serial.c文件是使用C语言编写的一个文件,主要用于处理串口通信。在很多嵌入式系统或者与外设通信的应用中,使用串口进行数据的传输和接收是很常见的。serial.c文件就是用于实现这一功能的。 该文件主要包含了串口初始化、读取和写入数据等功能的函数。其中,串口初始化函数用于设置串口的波特率、数据位、停止位、校验位等参数,使得串口与外设能够进行正常的数据传输。读取和写入数据的函数用于在程序中进行串口数据的接收和发送操作。 在使用serial.c文件时,通常需要包含相关的库文件,并在程序中进行相应的函数调用。例如,首先需要调用串口初始化函数来设置串口的相关参数,然后可以通过读取数据函数来接收来自外设的数据,最后可以通过写入数据函数来向外设发送数据。 串口通信相比于其他通信方式,具有传输速率快、稳定可靠等优点。因此,在很多需要数据交换或者远程控制的应用中,都会选择使用串口通信。通过使用serial.c文件,我们可以方便地在C语言程序中实现串口通信功能,使得程序能够与外设进行数据的交互。这为我们的应用开发和系统调试提供了很大的便利。 ### 回答2: serial.c文件是一种用于处理串口通信的源代码文件,主要用于在计算机和外部设备之间进行数据传输。 在C语言中,serial.c文件通常定义了一些串口通信的函数和变量,用于配置和控制串口通信的参数和操作。通过这些函数和变量,可以实现与外部设备的数据交换和通信。 在serial.c文件中,常见的函数包括串口的初始化函数、发送数据函数、接收数据函数等。例如,初始化函数用于配置串口通信的波特率、数据位数、停止位等参数;发送数据函数用于将数据发送到外部设备;接收数据函数则用于从外部设备接收数据。 此外,serial.c文件中可能还定义了一些全局变量和数据结构,用于存储和处理串口通信的相关信息。例如,可以定义一个用于存储接收到的数据的缓冲区,或者定义一个用于状态标记的变量。 总之,serial.c文件是一个用于串口通信的C语言源代码文件,其中包含了一些函数和变量,用于配置和控制串口通信的参数和操作。通过对该文件的编写和调用,可以实现与外部设备的数据交换和通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值