我是linux上嵌入式系统的新手。我从我的friendlyARM KIT,tiny6410上的一个简单的串行通信程序开始,我遇到了这个问题。 这是我的代码:
#include <stdio.h> // standard input / output functions
#include <stdlib.h>
#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 <sys/time.h> // time calls
#include <string.h>
/* Ten file thiet bi cong com
- tren PC: /dev/ttyUSB0 (USB2COM)
- tren KIT: /dev/ttyS0;
*/
int main(int argc, char** argv)
{
char ch;
int fd;// n;
char *DeviceName;
struct termios port_settings; //Cau truc de luu tru cau hinh uart
if(argc>1)
{
DeviceName = (char*)malloc(strlen(argv[1])+1);
strcpy(DeviceName, argv[1]); //Lay tham so nhap vao
//printf("Device Name=%s\n", DeviceName);
}
else
DeviceName = "/dev/ttyUSB0"; //Cong com mac dinh
//Open com port
fd = open(DeviceName, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd<0)
{
printf("Open com port %s failed\n", DeviceName);
return fd;
}
fcntl(fd, F_SETFL, FNDELAY); /* Configure port reading */
//Cau hinh tham so com port
//baudrate 9600, 8N1
cfsetispeed(&port_settings, B9600);
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; //Set no parity
port_settings.c_cflag &= ~CSTOPB; //Set 1 stop bit
port_settings.c_cflag &= ~CSIZE; //Set 8 bit data using mask bit
port_settings.c_cflag |= CS8;
port_settings.c_cflag &= ~CRTSCTS; //No hadware hanshaking
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
while(1)
{
char key = getchar();
ch = key;
write(fd, &ch, 1);
}
close(fd);
return 0;
}
我得到“open com port / dev / ttyUSB0 failed”错误消息。
答案 0 :(得分:0)
您应该尝试通过指定/dev/ttyS0
来运行代码,因为通常主板的串口具有此设备名称,您还必须以root权限运行您的二进制文件