Chinaunix

标题: 请教:Linux串口通信的接收程序的一个问题(有源代码) [打印本页]

作者: ql2300237    时间: 2004-05-14 07:18
标题: 请教:Linux串口通信的接收程序的一个问题(有源代码)
硬件上只接了GND,TDX,RDX三根线.运行以下程序时,一直接收不到数据,是否哪儿写错了.但可以发送出数据
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/stat.h>;
#include <fcntl.h>;
#include <errno.h>;
#include <string.h>;
#include <termios.h>;

int main()
{ int fd,nbyte,nread,i,j;
struct termios opt;
char buffer[1024]="*hello,xianren!*";
char buffer1[]="00000000000000000";

fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);//open com
if(fd==-1)perror("Unable to open /dev/ttyS0!";
//printf("fd=%d\n",fd);

tcgetattr(fd,&opt);
cfsetispeed(&opt,B9600);//baud rate
cfsetospeed(&opt,B9600);
opt.c_cflag|=(CLOCAL|CREAD);
tcsetattr(fd,TCSANOW,&opt);

opt.c_cflag &=~PARENB;//8N1
opt.c_cflag &=~CSTOPB;
opt.c_cflag &=~CSIZE;
opt.c_cflag |=CS8;

//opt.c_cflag &=~CNEW_RTSCTS; //remove hardware control
opt.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG); //origingal input
opt.c_oflag &=~OPOST; //original output
opt.c_cc[VMIN]=0;
opt.c_cc[VTIME]=10; //wait for 1s

printf("Send and receive begin:\n";
for(i=0;i<16;i++)
{
//nbyte=write(fd,&buffer,1);
//printf("%d data sended:%c \t",nbyte,buffer);

nread=read(fd,&buffer1,1);
//while(nread<=0);
printf("%d data received:%c\n",nread,buffer1);
}
buffer1[16]='\0';
printf("Send and Receive end!\n data received is:%s\n",buffer1);


//nbyte=write(fd,buffer,1);
//printf("%d data sended:%s\n",nbyte,buffer);

close(fd);
return(0);
}
作者: windflowers1976    时间: 2004-05-14 07:49
标题: 请教:Linux串口通信的接收程序的一个问题(有源代码)
先找个MINICOM测试下你作的串口测试头.
作者: henngy    时间: 2004-05-14 09:52
标题: 请教:Linux串口通信的接收程序的一个问题(有源代码)
你的设置有问题,下面是我写的测试RS485端口的读指令:
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/stat.h>;
#include <fcntl.h>;
#include <termios.h>;
#include <errno.h>;

main()
{
        int fd;
        struct termios Opt;
        int nwrite;
        int nread;
        char buff[64];
        char buffer[64];
        int i;
        int ST;

        fd = open("/dev/ttyS1", O_RDWR);
        if (-1 == fd) {
                perror("ORT_ERROR!";
        }
        else
                printf("The port is open\n";

        tcgetattr(fd, &Opt);
        cfsetispeed(&Opt, B9600);
        cfsetospeed(&Opt, B9600);
        tcsetattr(fd, TCSANOW, &Opt);
        if (0 != tcgetattr(fd, &Opt)) {
                perror("SetupSerial 1";
                exit(1);
        }
        Opt.c_cflag &= ~PARENB;
        Opt.c_cflag &= ~CSTOPB;
        Opt.c_cflag &= ~CSIZE;
        Opt.c_cflag |= CS8;
        tcflush(fd,TCIFLUSH);
        Opt.c_cc[VTIME] = 150;
        Opt.c_cc[VMIN] = 0;
        Opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
        Opt.c_oflag &= ~OPOST;

        if (0 != tcsetattr(fd, TCSANOW, &Opt)) {
                perror("SetupSerial 3";
                exit(1);
        }
        printf("while begin.......\n";
        while(1) {
                sleep(1);
                printf("read begin.......\n";
                nread = read(fd, buffer, 3);
                if (nread <= 0)
                        perror("nread";
                if (nread >; 0) {
                        for(i = 0; i < 3; i++) {
                                printf("%#x\n", buffer);
                        }
                }
        }
}


其中端口设置:1开始位,8数据位,没有奇偶位,波特率9600
作者: henngy    时间: 2004-05-14 09:54
标题: 请教:Linux串口通信的接收程序的一个问题(有源代码)
复制原因,格式不怎么清楚!
作者: windflowers1976    时间: 2004-05-14 10:46
标题: 请教:Linux串口通信的接收程序的一个问题(有源代码)
形成回路的串口后,没发送何来接收?
先write fd 然后再 recv fd.
作者: ql2300237    时间: 2004-05-18 18:49
标题: 请教:Linux串口通信的接收程序的一个问题(有源代码)
谢谢各位的建议!
我现在程序可以接收了
作者: windflowers1976    时间: 2004-05-18 19:58
标题: 请教:Linux串口通信的接收程序的一个问题(有源代码)
还有 不要这么一大堆代码,自己作些小封装,C/C++都可以,
SERIAL OPEN CLOSE WRITE READ INIT ... ...




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2