#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <unistd.h>
int openCom(char *comBuf)
{
char comName[64] = {0};
snprintf(comName,sizeof(comName),"/dev/tty%s",comBuf);
int fd = open(comName,O_RDWR);
if(fd < 0)
{
perror("openm:");
printf("open %s error\n",comName);
return -1;
}
fcntl(fd, F_SETFL, 0);
struct termios opts;
tcgetattr(fd, &opts);
cfsetispeed(&opts, B9600);
cfsetospeed(&opts, B9600);
opts.c_cflag |= CLOCAL|CREAD;
opts.c_cflag &= ~PARENB;
opts.c_cflag &= ~CSTOPB;
opts.c_cflag |= CS8;
opts.c_cflag &= ~CRTSCTS;
opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
opts.c_oflag &= ~OPOST;
tcsetattr(fd, TCSANOW, &opts);
}
int main(int argc,char **argv)
{
int fd;
if(argc < 2)
{
fd = openCom("");
}
else
{
fd = openCom(argv[1]);
}
if(fd < 0)
{
return 0;
}
while(1)
{
sleep(1);
write(fd,"123\n",4);
}
}