本帖最后由 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 程式如下:
- #include <stdio.h> // standard input / output functions
- #include <string.h> // string function definitions
- #include <unistd.h> // UNIX standard function definitions
- #include <fcntl.h> // File control definitions
- #include <errno.h> // Error number definitions
- #include <termios.h> // POSIX terminal control definitionss
- #include <time.h> // time calls
- int open_port(void)
- {
- int fd; // file description for the serial port
-
- fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
-
- if(fd == -1) // if open is unsucessful
- {
- printf("open_port: Unable to open /dev/ttyS0. \n");
- }
- else
- {
- fcntl(fd, F_SETFL, 0);
- printf("port is open.\n");
- }
-
- return(fd);
- } //open_port
- int configure_port(int fd) // configure the port
- {
- struct termios port_settings; // structure to store the port settings in
- cfsetispeed(&port_settings, B9600); // set baud rates
- cfsetospeed(&port_settings, B9600);
- port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
- port_settings.c_cflag &= ~CSTOPB;
- port_settings.c_cflag &= ~CSIZE;
- port_settings.c_cflag |= CS8;
-
- fcntl(fd, F_SETFL, FNDELAY);
-
- tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
- return(fd);
- } //configure_port
- int query_modem(int fd) // query modem with an AT command
- {
- char n;
- fd_set rdfs;
- struct timeval timeout;
-
- // initialise the timeout structure
- timeout.tv_sec = 10; // ten second timeout
- timeout.tv_usec = 0;
-
- //Create byte array
- unsigned char send_bytes[8];
- send_bytes[0] = 0x55;
- send_bytes[1] = 0x01;
- send_bytes[2] = 0x12;
- send_bytes[3] = 0x00;
- send_bytes[4] = 0x00;
- send_bytes[5] = 0x00;
- send_bytes[6] = 0x02;
- send_bytes[7] = 0x6A;
-
- write(fd, send_bytes, 8); //Send data
- printf("Wrote the bytes. \n");
- return 0;
-
- } //query_modem
- int main(void)
- {
- int fd = open_port();
- configure_port(fd);
- query_modem(fd);
-
- return(0);
-
- } //main
复制代码 |