1
我試圖連接具有RS232接口的熱敏打印機。Linux串行端口應用程序不工作
打印機在與膩子,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;
}
解釋 「不能夠使它發揮作用」 ...錯誤?消息? – m0skit0
請澄清「無法使其工作」的句子。我看到你忘記關閉()你的描述符。這可能是數據尚未發送到打印機的原因。 –
其實該程序正在工作。問題是我沒有啓用VMware中運行該程序的串口。 – androidFan