- 论坛徽章:
- 0
|
那位高手有FREEBSD下串口编程的C程序呢,只是实现COM1写,COM2读的就可以。。。
我用DB9的线将COM1和COM2连接起来,想实现COM1写,然后在COM2把COM1写的值读出来,但现在读一直出错,返回值为-1。
提示 : resource temporarily unavailable.
那位有这这方面的经验,请指教
#include <sys/types.h>;
#include <sys/stat.h>;
#include <fcntl.h>;
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "termios.h"
#include "math.h"
int main()
{
int fd = open_serial_on();
read_serial_on(fd);
close_serial(fd);
}
int open_serial_on(void)
{
char *str;/*serial device[/dev/tty?]*/
int fd;
char *filename;
struct termios option;/*serial struct*/
if((fd = open("/dev/ttyd1", O_RDWR | O_NOCTTY | O_NDELAY))==-1)
{
perror("open_port: Unable to open /dev/ttyS1 - " ;/* Could not open the port*/
}
else
fcntl(fd, F_SETFL, 0);
// fcntl(fd, F_SETFL, FNDELAY);
/* * Get the current option for the port...*/
tcgetattr(fd, &option);
/* * Set the baud rates to 4800...*/
cfsetispeed(&option, B19200);
cfsetospeed(&option, B19200);
/* * Enable the receiver and set local mode...*/
option.c_cflag |= (CLOCAL | CREAD);
/* * Set the new option for the port...*/
// option.c_cflag&=~CRTSCTS;/*close hardware flow control*/
tcsetattr(fd, TCSANOW, &option);
/*No parity (8N1): none parity bit(PARENB),1 stop bit(CSTOPB),none bit mask for data bit(CSIZE), 8 data bit(CS */
option.c_cflag &= ~PARENB;
option.c_cflag &= ~CSTOPB;
option.c_cflag &= ~CSIZE;
option.c_cflag |= CS8;
return(fd);
}
void read_serial(int fd)
{
char cmd[2];
int i;
char *parameter;
char *storefile;
char namesize;
int namesize_int;
int readnsize;/*read data size*/
if(read(fd,&namesize,1)<0)
{
perror("read serial file error\n" ;
exit(-1);
}
namesize_int=(int)((int)namesize-4 ;
storefile=(char *)malloc(namesize_int+1);
printf("namesize_int [%d]\n",namesize_int);
if(read(fd,storefile,namesize_int)<0)
{
perror("read serial file error\n" ;
exit(-1);
}
printf("storefile %s\n",storefile);
parameter=(char *)malloc(SIZE_FILE);
if((readnsize=read(fd,parameter,SIZE_FILE))<0)
{
perror("read serial file error\n" ;
exit(-1);
}
free(parameter);
free(storefile);
}
void close_serial( int fd)
{
close(fd);
} |
|