|
#include <stdlib.h>
#include <stdlib.h>
#include <unsisted.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <erro.h>
#define BAUDRATE B19200
char *dev="/dev/ttys1";
void setup_com(int fd){
struct termios options;
tcgetattr(fd, &options);
/* Set the baud rates to 19200..*/
cfsetispeed(&options, BAUDRATE);
cfsetospeed(&options, BAUDRATE);
/* Enable the receiver and set local mode...*/
options.c_cflag |= (CLOCAL | CREAD);
/* Set c_cflag options.*/
options.c_cflag &= ~PARENB; //无校验
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB; //1个停止位
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; //8个数据位
/* Set c_iflag input options */
options.c_iflag &=~(IXON | IXOFF | IXANY);
options.c_iflag &=~(INLCR | IGNCR | ICRNL);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* Set c_oflag output options */
options.c_oflag &= ~OPOST; //RAW数据模式
/* Set the timeout options */
options.c_cc[VMIN] = 10;
options.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &options); //设置COM参数并立即生效
}
int main(int argc, char **argv)
{
int fd;
int nread;
char buff[512];
char *dev ="/dev/ttyS1";
fd = OpenDev(dev);
if (fd>0)
set_speed(fd,19200);
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
while(1)
{
while((nread = read(fd,buff,512))>0)
{
printf("\nLen %d\n",nread);
buff[nread+1]='\0';
printf("\n%s",buff);
}
}
//close(fd);
//exit(0);
} |
|