CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 4428|回复: 0

uart 操作

[复制链接]
发表于 2014-6-14 09:22:07 | 显示全部楼层 |阅读模式
本帖最后由 seeing 于 2014-6-14 09:44 编辑

請問 Cubian 的 uart 可以操作嗎?我修改了 /boot 中的 script.bin 後已經可以看到 ttyS1 的裝置,也寫了程式測試通訊的部份,不過似乎有問題,資料送出 55 01 12 00 00 00 02 6A,卻是收到 55 7F BB FF FF FB 2B 00,不曉得是哪裡設置有問題?

uart4
baudrate:9600, n, 8, 1
環境:uart4 直接接 pc 的 com1

[script.fex]
uart_used = 1
uart_port = 4
uart_type = 2
uart_tx = portG10<4><1><default><default>
uart_rx = portG11<4><1><default><default>

cubian 程式如下:

  1. #include <stdio.h> // standard input / output functions
  2. #include <string.h> // string function definitions
  3. #include <unistd.h> // UNIX standard function definitions
  4. #include <fcntl.h> // File control definitions
  5. #include <errno.h> // Error number definitions
  6. #include <termios.h> // POSIX terminal control definitionss
  7. #include <time.h>   // time calls


  8. int open_port(void)
  9. {
  10.         int fd; // file description for the serial port
  11.         
  12.         fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
  13.         
  14.         if(fd == -1) // if open is unsucessful
  15.         {
  16.                 printf("open_port: Unable to open /dev/ttyS0. \n");
  17.         }
  18.         else
  19.         {
  20.                 fcntl(fd, F_SETFL, 0);
  21.                 printf("port is open.\n");
  22.         }
  23.         
  24.         return(fd);
  25. } //open_port

  26. int configure_port(int fd)      // configure the port
  27. {
  28.         struct termios port_settings;      // structure to store the port settings in

  29.         cfsetispeed(&port_settings, B9600);    // set baud rates
  30.         cfsetospeed(&port_settings, B9600);

  31.         port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
  32.         port_settings.c_cflag &= ~CSTOPB;
  33.         port_settings.c_cflag &= ~CSIZE;
  34.         port_settings.c_cflag |= CS8;
  35.       
  36.         fcntl(fd, F_SETFL, FNDELAY);
  37.         
  38.         tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port
  39.         return(fd);

  40. } //configure_port

  41. int query_modem(int fd)   // query modem with an AT command
  42. {
  43.         char n;
  44.         fd_set rdfs;
  45.         struct timeval timeout;
  46.         
  47.         // initialise the timeout structure
  48.         timeout.tv_sec = 10; // ten second timeout
  49.         timeout.tv_usec = 0;
  50.         
  51.         //Create byte array
  52.         unsigned char send_bytes[8];
  53.         send_bytes[0] = 0x55;
  54.         send_bytes[1] = 0x01;
  55.         send_bytes[2] = 0x12;
  56.         send_bytes[3] = 0x00;
  57.         send_bytes[4] = 0x00;
  58.         send_bytes[5] = 0x00;
  59.         send_bytes[6] = 0x02;
  60.         send_bytes[7] = 0x6A;
  61.    
  62.         write(fd, send_bytes, 8);  //Send data
  63.         printf("Wrote the bytes. \n");

  64.         return 0;
  65.         
  66. } //query_modem

  67. int main(void)
  68. {
  69.         int fd = open_port();
  70.         configure_port(fd);
  71.         query_modem(fd);
  72.         
  73.         return(0);
  74.         
  75. } //main
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-4-28 19:34 , Processed in 0.020320 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部