免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1402 | 回复: 0
打印 上一主题 下一主题

帮我看看我的代码,只是实现COM1写,COM2读的就可以。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-10 11:54 |只看该作者 |倒序浏览
那位高手有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);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP