⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 485_receive_data.c

📁 linux 485欻串口编程示例
💻 C
字号:
// this is a test about Full 485 // hardware : MAX488#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>      // open() close()#include <unistd.h>     // read() write()#include <termios.h>    // set baud rate#include <fcntl.h>#include <sys/select.h>#include <sys/time.h>#include <sys/types.h>#include "OURS_DEF.h"#define DEVICE_TTYS "/dev/ttyS1"#define MY_BAUD_RATE B9600#define RECEIVE_BUF_WAIT_1S 1#define RECEIVE_BUF_WAIT_2S 2#define RECEIVE_BUF_WAIT_3S 3#define RECEIVE_BUF_WAIT_4S 4#define RECEIVE_BUF_WAIT_5S 5//------------------------------------- read datas from ttyS ------------------------------------------------/*!!! Notice:	If you want to see something on the screen, another program must be running at another machine.that machine must be connect to this machine with net wire or other wire.*/// succese return 1// error   return 0int read_datas_ttyS(int fd, char *rcv_buf,int rcv_wait){        int retval;        fd_set rfds;        struct timeval tv;        int ret,pos;        tv.tv_sec = rcv_wait;           tv.tv_usec = 0;        pos = 0; // point to rceeive buf        while (1)        {                FD_ZERO(&rfds);                FD_SET(fd, &rfds);                retval = select(fd+1 , &rfds, NULL, NULL, &tv);                if (retval == -1)                {                        perror("select()");                        break;                }                else if (retval)                {// pan duan shi fou hai you shu ju                        ret = read(fd, rcv_buf+pos, 2048);                        pos += ret;                        if (rcv_buf[pos-2] == '\r' && rcv_buf[pos-1] == '\n')                        {                                FD_ZERO(&rfds);                                FD_SET(fd, &rfds);                                retval = select(fd+1 , &rfds, NULL, NULL, &tv);                                if (!retval) break;// no datas, break                        }                }                else                {                 //       printf("No data\n");                        break;                }        }        return 1;} // end read_datas_ttySvoid init_CPLD()        {                int ret;                int fd;                fd=open("/dev/CPLD",O_RDWR);		//CPLD is a device that should be insmod before this program.                if (fd<0)                {                        printf("Open device CPLD error\n");                }                else                {                        printf("Open device CPLD success\n");                }                //getchar();                ret=ioctl(fd,READ_CPLD_CTL,0);                printf(" CPLD_CTL is %x\n", ret);                ioctl(fd,WRITE_CPLD_CTL,0x20);                ret=ioctl(fd,READ_CPLD_CTL,0);                printf(" CPLD_CTL is %x\n", ret);                ret=ioctl(fd,READ_XGPIO_OUT,0);                printf(" XGPIO_OUT is %x\n", ret);                ioctl(fd,WRITE_XGPIO_OUT,0x00);		//Configure it to "receive mode".                ret=ioctl(fd,READ_XGPIO_OUT,0);                printf(" XGPIO_OUT is %x\n", ret);        }int func_485_receive(int fd){		ssize_t ret;	char rcv_buf[1024];	int i;        char *send_buf="nice";	bzero(rcv_buf,sizeof(rcv_buf));		for (i=0;i<10;i++) {		ret = write(fd,send_buf,strlen(send_buf));                if (ret == -1) {                        printf ("write device %s error\n", DEVICE_TTYS);                        return -1;                }		if (read_datas_ttyS(fd,rcv_buf,RECEIVE_BUF_WAIT_1S)) {			printf ("%s\n",rcv_buf);		}		else {			printf ("read error\n");		}	}		return 0;} // end func_485_receive//------------------------------------- init seriel port  ---------------------------------------------------void init_ttyS(int fd){        struct termios options;        bzero(&options, sizeof(options));       	// clear options        cfsetispeed(&options,MY_BAUD_RATE);            	// setup baud rate        cfsetospeed(&options,MY_BAUD_RATE);        options.c_cflag |= ( CS8 | CLOCAL | CREAD);        options.c_iflag = IGNPAR;        tcflush(fd, TCIFLUSH);        tcsetattr(fd, TCSANOW, &options);}//end init_ttyS//------------------------------------- main ----------------------------------------------------------------int main(void){        int fd;        printf("\n 485 RECEIVE DATAS \n\n");        // open seriel port        fd = open(DEVICE_TTYS, O_RDWR);        if (fd == -1) {                printf("open device %s error\n",DEVICE_TTYS);        }        else {                init_ttyS(fd);  	// init device		init_CPLD();		// Configure the CPLD.                func_485_receive(fd);  	// 485 send datas functions                // close ttyS0                if (close(fd)!=0) printf("close device %s error",DEVICE_TTYS);        }        return 0;}// end main

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -