int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR | O_NOCTTY); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
int main(int argc, char **argv)
{
int uart_fd;
int nread, nwrite;
int num = 0;
char rev_char[128];
char rev_buf[128];
char *buff="hello! this is a serial test program,if you can see this,you success!\n";
char *dev = "/dev/ttyAMA2"; //串口二
uart_fd = OpenDev(dev); //打开设备
set_speed(uart_fd, 9600); //设置波特率
if (set_Parity(uart_fd,8,1,'n') == FALSE) //数据位,停止位,校验位
{
printf("Set Parity Error\n");
exit (0);
}
while(nread = read(uart_fd, rev_char, 1)) //读返回值
{
rev_buf[num++] = rev_char[0];
if (rev_char[0] == '\n')
{
rev_buf[num] = '\0';
num = 0;
printf("%s", rev_buf);
}
printf("%s", rev_buf);
if((nwrite = write(uart_fd,buff,strlen(buff)))>0)
{
printf("%s\n",buff);
}
}
if (nread == 0)
printf("No DATE!\n");
}
close(uart_fd);
}