knaffe

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe  发布于  2016-07-04 10:12:52
采纳率 0%
3个问答
3566

HI3520D UART0 无法发送数据--附上源码

 

我用超级终端和串口调试助手,都无法接收到数据,但是open,write操作都正常,能返回正常值。请各位指点。本人初学。
我使用串口目的就是想让串口发送一串字符指令出来用作控制外设(GSM)
附上我的代码,使用的是UART0

-----------------------------------------------------------------
#include     <stdio.h>      /*标准输入输出定义*/
#include     <stdlib.h>     /*标准函数库定义*/
#include     <unistd.h>     /*Unix标准函数定义*/
#include     <sys/types.h>  /**/
#include     <sys/stat.h>   /**/
#include     <fcntl.h>      /*文件控制定义*/
#include     <termios.h>    /*PPSIX终端控制定义*/
#include     <errno.h>      /*错误号定义*/

/***@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(fd, TCIOFLUSH); //清空终端未完成的输入/输出请求及数据, TCIOFLUSH:清除所有正在发生的I/O数据
        cfsetispeed(&Opt, speed_arr[i]);//获取和设置Linux终端属性,线路控制,获取和设置波特率。
        cfsetospeed(&Opt, speed_arr[i]);//speed_arr[i]:需要设置的输出波特率
        status = tcsetattr(fd, TCSANOW, &Opt);//设置终端参数,不等数据传输完毕就立即改变属性
        if  (status != 0)
            perror("tcsetattr fd1");
         return;
    }
   tcflush(fd,TCIOFLUSH);//清空终端未完成的输入/输出请求及数据, TCIOFLUSH:清除所有正在发生的I/O数据
   }
}
/**
*@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(0);
  }
  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 (0);
    }
  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 (0);
        }
  /* 设置停止位*/   
  switch (stopbits)
      {
      case 1:
          options.c_cflag &= ~CSTOPB;
        break;
    case 2:
        options.c_cflag |= CSTOPB;
        break;
    default:
        fprintf(stderr,"Unsupported stop bits\n");
        return (0);
    }
  /* Set input parity option */
  if (parity != 'n')
          options.c_iflag |= INPCK;
    options.c_cc[VTIME] = 150; // 15 seconds
    options.c_cc[VMIN] = 0;

  tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  if (tcsetattr(fd,TCSANOW,&options) != 0)
      {
          perror("SetupSerial 3");
        return (0);
    }
  return (1);
 }
/**
*@breif 打开串口
*/
int OpenDev(char *Dev)
{
    int    fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
    if (-1 == fd)
        { 
            perror("Can't Open Serial Port");
            return -1;
        }
    else
    return fd;

}
/**
*@breif     main()
*/
int main(int argc, char **argv)
{
    int fd;
    int nread;
    char buff[2]={1,2};
    char *dev ="/dev/ttyAMA0";
    printf("ready\n");
    fd = OpenDev(dev);
    if (fd>0)
    {
        printf("open uart success!");
        set_speed(fd,9600);
    }
    else
        {
        printf("Can't Open Serial Port!\n");
        exit(0);
        }
  if (set_Parity(fd,8,1,'N')== 0)
  {
    printf("Set Parity Error\n");
    exit(1);
  }
  for(int i=0;i<100;i++)
      {
           if(write(fd,buff,2)!=-1)
            printf("%d\n",i);

      }
    close(fd);
    printf("The End!\n");
    exit(0);
}
我来回答
回答19个
时间排序
认可量排序

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 10:18:10
认可0
这是hilinux里头dev目录

ngswfx

1个粉丝

55

问答

1

专栏

40

资料

ngswfx 2016-07-04 10:48:47
认可0
本帖最后由 ngswfx 于 2016-7-4 10:55 编辑

[quote][url=forum.php?mod=redirect&goto=findpost&pid=33099&ptid=11835]knaffe 发表于 2016-7-4 10:18[/url]
这是hilinux里头dev目录[/quote]

总体流程看代码应该是对的,问题不大。


你的串口0可是在被控制终端用着呢,而且波特率是115200,在uboot环境下,就已经设定了。

///////////你不用动这些参数设置,直接open ttyAMA0后,write就可以看到控制终端上有输出了。

我测试了一下:
直接用:

char str[]="adfasdfqwertqwetdfgsdfg";

if(fUart1==0)
                fUart1=open("/dev/ttyAMA0",O_RDWR);
        int nWriteLen=0;
        if(fUart1)
                nWriteLen=write(fUart1,str, strlen(str));

//////////////终端上就会有输出。


你如果想让串口调试助手收到程序,你就写个死循环,1秒钟发一个包出来,让他一直发数据,否则100个包很快就发完了,你还没来得及换线,就结束了。

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 11:30:03
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33101&ptid=11835]ngswfx 发表于 2016-7-4 10:48[/url]
总体流程看代码应该是对的,问题不大。


[/quote]

我还是没接收到信息。。但是我可以从电脑终端写信息给板子,但是就是读不到板子写给我的

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 17:12:47
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33101&ptid=11835]ngswfx 发表于 2016-7-4 10:48[/url]
总体流程看代码应该是对的,问题不大。


[/quote]

可以加个QQ请教你吗? 我的QQ是  673009062

zhao_178910

0个粉丝

4

问答

0

专栏

0

资料

zhao_178910 2016-07-04 17:27:45
认可0
没用过你的那款芯,不过串口0 一般是控制终端吧,你试一下别的串口

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 17:31:48
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33161&ptid=11835]zhao_178910 发表于 2016-7-4 17:27[/url]
没用过你的那款芯,不过串口0 一般是控制终端吧,你试一下别的串口[/quote]

这个可以试试,但是找不到其他串口的引脚,貌似找不到板子的原理图

ngswfx

1个粉丝

55

问答

1

专栏

40

资料

ngswfx 2016-07-04 18:21:13
认可0
本帖最后由 ngswfx 于 2016-7-4 18:23 编辑

[quote][url=forum.php?mod=redirect&goto=findpost&pid=33157&ptid=11835]knaffe 发表于 2016-7-4 17:12[/url]
可以加个QQ请教你吗? 我的QQ是  673009062[/quote]

QQ已经加了。你就直接不去设置波特率和校验位,打开你些的那个程序demo,直接open uart0 ,然后狂发数据,while循环,把控制终端关闭,PC打开串口调试助手,把波特率设置为115200,应该能收到东西才对。


////////这些都调试通过了,再修改demo程序,考虑接收的事情,不过通常接收串口这种数据,需要建立一个线程,不停访问uart0的数据,如果读取到东西,通过回调输出到应用层处理就可以了。

ngswfx

1个粉丝

55

问答

1

专栏

40

资料

ngswfx 2016-07-04 18:35:29
认可0
本帖最后由 ngswfx 于 2016-7-4 18:38 编辑

我试了你些的代码,能用呀.


只要一执行,控制台就失控了

就是会失控的,符合逻辑,控制台软件用的交互波特率是115200,一旦修改为9600发数据,就是这样的.

ngswfx

1个粉丝

55

问答

1

专栏

40

资料

ngswfx 2016-07-04 18:37:06
认可0
如果关闭,不去设置波特率等等,直接写就是对的,控制台收到数据了:

# ./UartTest                                                                    
ready                                                                           
open uart success!0                                                           
1                                                                             
2                                                                             
3                                                                             
4                                                                             
5                                                                             
6                                                                             
7                                                                             
8                                                                             
9                                                                             
10                                                                           
11                                                                           
12                                                                           
13                                                                           
14                                                                           
15                                                                           
16                                                                           
17                                                                           
18                                                                           
19                                                                           
20                                                                           
21                                                                           
22                                                                           
23                                                                           
24                                                                           
25                                                                           
26                                                                           
27                                                                           
28                                                                           
29                                                                           
30                                                                           
31                                                                           
32                                                                           
33                                                                           
34                                                                           
35                                                                           
36                                                                           
37                                                                           
38                                                                           
39                                                                           
40                                                                           
41                                                                           
42                                                                           
43                                                                           
44                                                                           
45                                                                           
46                                                                           
47                                                                           
48                                                                           
49                                                                           
50                                                                           
51                                                                           
52                                                                           
53                                                                           
54                                                                           
55                                                                           
56                                                                           
57                                                                           
58                                                                           
59                                                                           
60                                                                           
61                                                                           
62                                                                           
63                                                                           
64                                                                           
65                                                                           
66                                                                           
67                                                                           
68                                                                           
69                                                                           
70                                                                           
71                                                                           
72                                                                           
73                                                                           
74                                                                           
75                                                                           
76                                                                           
77                                                                           
78                                                                           
79                                                                           
80                                                                           
81                                                                           
82                                                                           
83                                                                           
84                                                                           
85                                                                           
86                                                                           
87                                                                           
88                                                                           
89                                                                           
90                                                                           
91                                                                           
92                                                                           
93                                                                           
94                                                                           
95                                                                           
96                                                                           
97                                                                           
98                                                                           
99                                                                           
The End!   

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 18:50:17
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33170&ptid=11835]ngswfx 发表于 2016-7-4 18:35[/url]
我试了你些的代码,能用呀.


[/quote]

我的代码不能用....无法打印输出。我不知道是不是硬件的问题了

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 18:51:09
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33170&ptid=11835]ngswfx 发表于 2016-7-4 18:35[/url]
我试了你些的代码,能用呀.


[/quote]

控制台失控时什么意思?

ngswfx

1个粉丝

55

问答

1

专栏

40

资料

ngswfx 2016-07-04 18:51:52
认可0
本帖最后由 ngswfx 于 2016-7-4 18:57 编辑

[quote][url=forum.php?mod=redirect&goto=findpost&pid=33176&ptid=11835]knaffe 发表于 2016-7-4 18:51[/url]
控制台失控时什么意思?[/quote]

就是啥都不显示,按键盘没反映了,PC机和板子通信彻底不同步了.

我是用的minicom软件和开发板通过串口0通信的

///////////////你把你的代码for循环改成while循环:
int i=0;
        while(1)
        {
                usleep(1000*1000);
                char buffEx[32];
                printf("buff:test :%d \n",i++);
                write(fd,buffEx,2);

        }

///////////////////////////////然后再测试,PC机串口调试助手应该能收到数据.

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 19:00:18
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33177&ptid=11835]ngswfx 发表于 2016-7-4 18:51[/url]
就是啥都不显示,按键盘没反映了,PC机和板子通信彻底不同步了.

我是用的minicom软件和开发板通过串口 ...[/quote]

失控的话,对我的应用应该没什么影响的。我让板子监控识别而已。我现在的情况是,程序不设置波特率,直接open和write、read都没反应。我在串口调试助手发送数据,板子上会打印出读到的数量,但是打印buf就不行。程序中用write写,函数返回值正常,但是串口调试助手无法接收到数据。一个都没有,我用了几个串口调制助手都不行

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 19:06:07
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33177&ptid=11835]ngswfx 发表于 2016-7-4 18:51[/url]
就是啥都不显示,按键盘没反映了,PC机和板子通信彻底不同步了.

我是用的minicom软件和开发板通过串口 ...[/quote]

刚刚修改了你给我的代码,电脑端无显示...终端不断刷新buff test i

ngswfx

1个粉丝

55

问答

1

专栏

40

资料

ngswfx 2016-07-04 19:12:56
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33179&ptid=11835]knaffe 发表于 2016-7-4 19:06[/url]
刚刚修改了你给我的代码,电脑端无显示...终端不断刷新buff test i[/quote]

你的终端软件是什么软件?

/////////////搞得我云里雾里的。

////////////PC方面,串口都是独占的,你要是开了串口终端,其他串口类程序,端口就打不开了。

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-07-04 21:24:21
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=33181&ptid=11835]ngswfx 发表于 2016-7-4 19:12[/url]
你的终端软件是什么软件?

/////////////搞得我云里雾里的。
[/quote]

我的板子通过Teln控制,那么串口1没有被占用吧?可以用来发普通的数据吧?

但是我现在电脑的串口调试助手无法收到串口的消息,如何确定串口已经打开没问题?如何解决电脑终端无法读取串口数据?

lejianz

0个粉丝

1

问答

0

专栏

1

资料

lejianz 2016-07-05 23:06:20
认可0
根据芯片,查找芯片的引脚,用软件不停发送数据, 找个示波器,看一下TX引脚,有波形再去用软件测试。

ngswfx

1个粉丝

55

问答

1

专栏

40

资料

ngswfx 2016-07-05 23:16:51
认可0
本帖最后由 ngswfx 于 2016-7-6 10:01 编辑

[quote][url=forum.php?mod=redirect&goto=findpost&pid=33264&ptid=11835]lejianz 发表于 2016-7-5 23:06[/url]
根据芯片,查找芯片的引脚,用软件不停发送数据, 找个示波器,看一下TX引脚,有波形再去用软件测试。[/quote]

他平时用的telnet调试,串口线路可能有些问题。


///////////////只要使用串口方式的调试终端能正常工作,估计应该可以收发数据正常了。
////因为板子上只有uart0,也不用配置寄存器,应该比较好弄,只要把串口链路弄通即可。

/////////////我现在怀疑是不是板子上为了能支持telnet,修改了什么东西。

/////////////////////////////////////////////////////////////////////////////////////////////我以前也没搞过telnet,专门发帖求助,自己也尝试了一下开启telnet后,发现对Uart0串口调试没有影响(实现机制里面并没有对交互数据进行阻断)

applepen

0个粉丝

11

问答

0

专栏

11

资料

applepen 2017-03-15 10:33:00
认可0
本帖最后由 applepen 于 2017-3-15 10:34 编辑

int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;

if  ( tcgetattr( fd,&options)  !=  0)
  {
          perror("SetupSerial 1");
          return(0);
  }

  options.c_iflag = 0;
  options.c_oflag = 0;
.....

/* 设置停止位*/   
  switch (stopbits)
          {
          case 1:
                  options.c_cflag &= ~CSTOPB;
                break;
        case 2:
                options.c_cflag |= CSTOPB;
                break;
        default:
                fprintf(stderr,"Unsupported stop bits\n");
                return (0);
        }
//  if (parity != 'n')
//                  options.c_iflag |= INPCK;
//    options.c_cc[VTIME] = 1; // 15 seconds   150
//    options.c_cc[VMIN] = 1;        //0

    options.c_cflag &= ~CRTSCTS;        //不使用流控制

    options.c_lflag = 0;
    options.c_cc[VTIME]    = 10;
    options.c_cc[VMIN]     = 1;

  tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  if (tcsetattr(fd,TCSANOW,&options) != 0)
          {
                  perror("SetupSerial 3");
                return (0);
        }
  return (1);
}


char *dev ="/dev/ttys1";
QQ11315935
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

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

Markdown 语法

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

举报类型

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

详细说明

易百纳技术社区