【发布时间】:2014-02-11 22:10:31
【问题描述】:
我正在尝试连接具有 RS232 接口的热敏打印机。
打印机在使用 putty、terraterm 和 minicom 时工作正常。
但我无法在 linux 中使用 C 程序使其工作。
以下是程序:
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#define _POSIX_SOURCE 1 /* POSIX compliant source */
struct termios options;
int main(void /*int argc,char *argv[]*/)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
printf("Success\n");
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 9600...
*/
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
options.c_iflag |= (IXON | IXOFF | IXANY);
/*
* Set the new options for the port...
*/
tcsetattr(fd, TCSANOW, &options);
write(fd, "abcd", 4);
}
return 0;
}
【问题讨论】:
-
解释“无法使其工作”... 错误?消息?
-
请澄清“无法使其工作”这句话。我看到你忘了 close() 你的描述符。这可能是数据未发送到打印机的原因。
-
实际上程序现在正在运行。问题是我没有在运行程序的 VMware 中启用串行端口。
-
投票结束,因为没有其他问题需要回答。
-
你是如何让它与 minicom 一起工作的?
标签: linux serial-port embedded-linux