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

📄 485_send_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 "OURS_DEF.h"#define DEVICE_TTYS "/dev/ttyS1"#define MY_BAUD_RATE B9600int func_485_send(int fd){	ssize_t ret;	char *send_buf="abcdefgh";		//Give the message to be send.		while (1){	        ret = write(fd,send_buf,strlen(send_buf));        	if (ret == -1) {                	printf ("write device %s error\n", DEVICE_TTYS);	                return -1;        	}		sleep(1);		printf("sending....................................\n");			}} // end func_485_send//------------------------------------- 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_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,0xffff);	// Configure it to "send mode"		ret=ioctl(fd,READ_XGPIO_OUT,0);                printf(" XGPIO_OUT is %x\n", ret);	}//------------------------------------- main ----------------------------------------------------------------int main(void){        int fd;        printf("\n 485 SEND 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_send(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 + -