管理提醒: 本帖被 qq2440 从 Linux技术交流专区 移动到本区(2009-07-31)
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <stdlib.h>
int main (void)
{
char buff[]="RS232 Communication\n";
int nread;
int fd;
int status;
int nSpeed, nBits, nStop;
char nEvent;
struct termios newtio, oldtio;
fd=open( "/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1==fd)
{
perror("cann't open serial port 0\n");
}
else printf("open com0 ok\n");
if (tcgetattr( fd, &oldtio) !=0){
perror("setupserial 1");
return -1;
}
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CLOCAL|CREAD;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_cflag &=~ PARENB;
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
newtio.c_cflag &= ~CSTOPB;
newtio.c_cc[VTIME]=0;
newtio.c_cc[VMIN]=0;
tcflush(fd, TCIFLUSH);
if((tcsetattr(fd, TCSANOW, &newtio)) !=0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
write(fd,buff,19);
}
生成rs232 bin 文件后
./rs232
然后直接输出RS232 Communication,而不是在超级终端什么也没收到...别外/dev下只有tty,没有tty1,tty2?