1、 */* Use ionice wrapper to improve serial performance: $ sudo ionice -c 1 -n 0 ./bin/bacserv 12345*/* handle returned from open() */static int RS485_Handle = -1;/* baudrate settings are defined in , which is included by RS485 from B&B Electronics USOPTL4 */static char *RS485_Port_Name = /dev/ttyUSB0
2、;/*系统默认是通过USB转485的,根据需要设置,若你的开发板用485接口,则用static char *RS485_Port_Name = /dev/ttyS0代替 */* some terminal I/O have RS-485 specific functionality */#ifndef RS485MOD#define RS485MOD 0#endif/* serial I/O settings */static struct termios RS485_oldtio;/* Ring buffer for incoming bytes, in order to speed up
3、the receiving. */static FIFO_BUFFER Rx_FIFO;/* buffer size needs to be a power of 2 */static uint8_t Rx_Buffer4096;#define _POSIX_SOURCE 1 /* POSIX compliant source */* DESCRIPTION: Configures the interface name* RETURN: none* ALGORITHM:* NOTES:*/void RS485_Set_Interface( char *ifname) /* note: expe
4、cts a constant char, or char from the heap */ if (ifname) RS485_Port_Name = ifname; Returns the interface nameconst char *RS485_Interface( void) return RS485_Port_Name;/* Returns the baud rate that we are currently running at*/uint32_t RS485_Get_Baud_Rate( uint32_t baud = 0; switch (RS485_Baud) case
5、 B0: baud = 0; break; case B50: baud = 50; case B75: baud = 75; case B110: baud = 110; case B134: baud = 134; case B150: baud = 150; case B200: baud = 200; case B300: baud = 300; case B600: baud = 600; case B1200:baud = 1200; case B1800: baud = 1800; case B2400: baud = 2400; case B4800: baud = 4800;
6、 case B9600:baud = 9600; case B19200: baud = 19200; case B38400: baud = 38400; case B57600: baud = 57600; case B115200: baud = 115200; case B230400: baud = 230400; default: baud = 9600; return baud; Sets the baud rate for the chip USARTbool RS485_Set_Baud_Rate( uint32_t baud) bool valid = true; swit
7、ch (baud) case 0: RS485_Baud = B0; case 50: RS485_Baud = B50; case 75: RS485_Baud = B75; case 110: RS485_Baud = B110; case 134: RS485_Baud = B134; case 150: RS485_Baud = B150; case 200: RS485_Baud = B200;break; case 300: RS485_Baud = B300; case 600: RS485_Baud = B600; case 1200: RS485_Baud = B1200;
8、case 1800: RS485_Baud = B1800; case 2400: RS485_Baud = B2400; case 4800: RS485_Baud = B4800; case 9600: RS485_Baud = B9600; case 19200: RS485_Baud = B19200; case 38400: RS485_Baud = B38400; case 57600: RS485_Baud = B57600; case 115200: RS485_Baud = B115200; case 230400: RS485_Baud = B230400; valid =
9、 false; if (valid) /* FIXME: store the baud rate */ return valid; Transmit a frame on the wirevoid RS485_Send_Frame( volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes) /* number of bytes of data (up
10、 to 501) */ uint32_t turnaround_time = Tturnaround * 1000; uint32_t baud = RS485_Get_Baud_Rate(); ssize_t written = 0; int greska; /* sleeping for turnaround time is necessary to give other devices time to change from sending to receiving state. */ usleep(turnaround_time / baud);/至少要等待Tturnaround时间才
11、启动RS485缓冲器发送 /* On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and errno set appropriately. If count is zero and the file descriptor refers to a regular file, 0 will be returned without causing any other effect. For a special file
12、, the results are not portable. written = write(RS485_Handle, buffer, nbytes);/RS485_Handle初始时是为-1,当发出open操作时应该是个正整数,而write函数的原型是(fd,buf,n) greska = errno; if (written SilenceTimerReset(); return; Get a byte of receive dataRS485_Check_UART_Data函数分别检查ReceiveError、DataAvailable。然后选择端口从UART中读数据void RS4
13、85_Check_UART_Data( volatile struct mstp_port_struct_t *mstp_port) fd_set input; struct timeval waiter; uint8_t buf2048; int n; if (mstp_port-ReceiveError = true) /* do nothing but wait for state machine to clear the error */ /* burning time, so wait a longer time */ waiter.tv_sec = 0; waiter.tv_use
14、c = 5000; else if (mstp_port-DataAvailable = false) /* wait for state machine to read from the DataRegister */ if (FIFO_Count(&Rx_FIFO) 0) /* data is available */DataRegister = FIFO_Get(&Rx_FIFO);DataAvailable = true; /* FIFO is giving data - dont wait very long */ waiter.tv_usec = 10; /* FIFO is em
15、pty - wait a longer time */ /* grab bytes and stuff them into the FIFO every time */ FD_ZERO(&input); FD_SET(RS485_Handle, & n = select(RS485_Handle + 1, &input, NULL, NULL, &waiter); if (n if (FD_ISSET(RS485_Handle, &input) n = read(RS485_Handle, buf, sizeof(buf); FIFO_Add(&Rx_FIFO, &buf0, n);void
16、RS485_Cleanup( /* restore the old port settings */ tcsetattr(RS485_Handle, TCSANOW, &RS485_oldtio);/tcgetattr用于获取终端的相关参数,而tcsetattr函数用于设置终端参数 close(RS485_Handle);void RS485_Initialize( struct termios newtio;RS485: Initializing %s, RS485_Port_Name); Open device for reading and writing. Blocking mode - more CPU ef
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1