?? 485_send_data.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 + -