精通嵌入式linux编程语言,有精通嵌入式linux和c语言的大神吗?求帮助

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

发送程序如下:

#include

#include

#include /* 文件操作 */

#include /* ioctl()函数 */

#include /* file controle */

#include /*PPSIX terminal controle */

#include /* error */

#include

#include

#ifndef TRUE

#define TRUE 1

#endif

#ifndef FALSE

#define FALSE 0

#endif

/************ set baud ********************/

void set_speed(int fd, int speed)

{

int status;

struct termios Opt;

tcgetattr(fd, &Opt); // get attribute of serial port

tcflush(fd, TCIOFLUSH);

cfsetispeed(&Opt, speed);

cfsetospeed(&Opt, speed);

status = tcsetattr(fd, TCSANOW, &Opt); // set attribute

if (status != 0)

{

perror("tcsetattr fd");

return;

}

tcflush(fd,TCIOFLUSH);

}

/*********** set data format: databits,stopbits and parity **********/

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

{

struct termios opt;

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

{

perror("SetupSerial 1");

return(FALSE);

}

opt.c_cflag &= ~CSIZE;

switch (databits)

{

case 5:

opt.c_cflag |= CS5;

break;

case 6:

opt.c_cflag |= CS6;

break;

case 7:

opt.c_cflag |= CS7;

break;

case 8:

opt.c_cflag |= CS8;

break;

default:

printf("Unsupported data size\n");

return (FALSE);

}

switch (parity)

{

case 'n':

case 'N':

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

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

break;

case 'o':

case 'O':

opt.c_cflag |= (PARODD | PARENB); /* parity checking */

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

break;

case 'e':

case 'E':

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

opt.c_cflag &= ~PARODD; /* */

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

break;

default:

printf("Unsupported parity\n");

return (FALSE);

}

switch (stopbits)

{

case 1:

opt.c_cflag &= ~CSTOPB;

break;

case 2:

opt.c_cflag |= CSTOPB;

break;

default:

printf("Unsupported stop bits\n");

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

opt.c_iflag |= INPCK;

opt.c_cc[VTIME] = 100; // 10 seconds

opt.c_cc[VMIN] = 0;

tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */

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

{

perror("SetupSerial 3");

return (FALSE);

}

return (TRUE);

}

int main()

{

int fd;

int n=-1;

char recbuff[100]={};

fd = open("/dev/ttyS1", O_RDWR); /* 打开设备 */

if(fd == -1)

{

printf("\nCan't open ttyS1 device!\n");

exit(-1);

}

set_speed(fd,B115200); // set baud 115200

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

{

printf("Data format Error!\n");

exit(1);

}

printf("Please Enter 10 Bytes\n");

memset(recbuff,0,sizeof(recbuff));

while(1)

{

if(fgets(recbuff,sizeof(recbuff),stdin)==NULL)

{

printf("error");

exit(1);

}

write(fd,recbuff,10);

}

close(fd);

return 0;

}

/**************************************************************************

** End Of File

**************************************************************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值