Takin

Takin

1个粉丝

41

问答

0

专栏

0

资料

Takin  发布于  2016-09-01 14:09:58
采纳率 0%
41个问答
3662

uart2接收的问题

 

请教下在用海思uart接收数据时,为什么每次pc端输入发送之后,海思uart不能接收,
而当pc端发送了ctr+d指令后海思端就能接收到数据,
测波型发现海思uart rx之前就已收到了


接收部分如下:
printf("Reading...\n");  
    while(1) {  
        res = read(fd, buf, 255);  
  
        if(res==0)         continue;  
        buf[res]=0;  
  
        printf("%s", buf);  
         
        if (buf[0] == 0x0d)        printf("\n");  
         
        if (buf[0] == '@') break;  
    }  
好像是海思要收到一个信号,之后read才能读取数据
不知道哪个地方要设置才不会这样

我来回答
回答8个
时间排序
认可量排序

Takin

1个粉丝

41

问答

0

专栏

0

资料

Takin 2016-09-01 14:14:05
认可0
[code]#include   
  
#include   
#include   
#include   
#include   
#define BAUDRATE        B115200  
#define UART_DEVICE     "/dev/ttyS3"  
  
#define FALSE  -1  
#define TRUE   0  
////////////////////////////////////////////////////////////////////////////////  
/**
*@brief  设置串口通信速率
*@param  fd     类型 int  打开串口的文件句柄
*@param  speed  类型 int  串口速度
*@return  void
*/  
int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,  
                   B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };  
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200,  300,   
                  115200, 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) {      
      tcflush(fd, TCIOFLUSH);      
      cfsetispeed(&Opt, speed_arr);   
      cfsetospeed(&Opt, speed_arr);     
      status = tcsetattr(fd, TCSANOW, &Opt);   
      if  (status != 0) {         
        perror("tcsetattr fd1");   
        return;      
      }      
      tcflush(fd,TCIOFLUSH);     
    }   
  }  
}  
////////////////////////////////////////////////////////////////////////////////  
/**
*@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);   
    }   
    options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/  
    options.c_oflag  &= ~OPOST;   /*Output*/  
    return (TRUE);   
}  
////////////////////////////////////////////////////////////////////////////////  
int main(int argc, char *argv[])  
{  
  
    int    fd, c=0, res;  
  
    char  buf[256];  
  
    printf("Start...\n");  
    fd = open(UART_DEVICE, O_RDWR);  
  
    if (fd < 0) {  
        perror(UART_DEVICE);  
        exit(1);  
    }  
  
    printf("Open...\n");  
    set_speed(fd,115200);  
    if (set_Parity(fd,8,1,'N') == FALSE)  {  
        printf("Set Parity Error\n");  
        exit (0);  
    }  
  
    printf("Reading...\n");  
    while(1) {  
        res = read(fd, buf, 255);  
  
        if(res==0)  
            continue;  
        buf[res]=0;  
  
        printf("%s", buf);  
         
        if (buf[0] == 0x0d)  
            printf("\n");  
         
        if (buf[0] == '@') break;  
    }  
  
    printf("Close...\n");  
    close(fd);  
  
    return 0;  
}   [/code]

Takin

1个粉丝

41

问答

0

专栏

0

资料

Takin 2016-09-01 14:33:43
认可0
参考
[url]http://wenku.baidu.com/view/dd685c8b76eeaeaad1f330e8.html[/url]

baboe

0个粉丝

8

问答

0

专栏

1

资料

baboe 2016-09-01 16:27:29
认可0
可能是 printf 缓冲的问题

两个方式
1.
第154行,改多送一个\n
printf("%s\n", buf);

2.
第154行后加一行
fflush(stdout);

Takin

1个粉丝

41

问答

0

专栏

0

资料

Takin 2016-09-03 22:41:29
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=37027&ptid=12496]baboe 发表于 2016-9-1 16:27[/url]
可能是 printf 缓冲的问题

两个方式
[/quote]

谢了,是我流控设置的问题,不过还有个疑问是,之前在hi3535 上是ok 的换成3516cv200就不行,还不知原因

kennybluezy

0个粉丝

16

问答

0

专栏

0

资料

kennybluezy 2016-11-24 18:34:41
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=37166&ptid=12496]xjl_hi 发表于 2016-9-3 22:41[/url]
谢了,是我流控设置的问题,不过还有个疑问是,之前在hi3535 上是ok 的换成3516cv200就不行,还不知原因[/quote]

求支持,用文中代码,同样能写不能读。。。

13915426184

0个粉丝

15

问答

0

专栏

0

资料

13915426184 2017-09-08 11:24:56
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=37015&ptid=12496]xjl_hi 发表于 2016-9-1 14:14[/url]
[/quote]

非常感谢,这个程序至少设置uart状态很好

13915426184

0个粉丝

15

问答

0

专栏

0

资料

13915426184 2017-09-12 15:31:02
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=37027&ptid=12496]baboe 发表于 2016-9-1 16:27[/url]
可能是 printf 缓冲的问题

两个方式
[/quote]

高手啊,确实就是这样的

hero

0个粉丝

1

问答

0

专栏

0

资料

hero 2017-09-12 16:53:21
认可0
:victory::victory::victory::victory::victory:
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区