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

📄 serial_connect.c

📁 主要用于无线传感网络的编写的书籍.对于初学者有着很大的用处
💻 C
字号:
#include <stdio.h>   /* Standard input/output definitions */#include <string.h>  /* String function definitions */#include <unistd.h>  /* UNIX standard function definitions */#include <fcntl.h>   /* File control definitions */#include <errno.h>   /* Error number definitions */#include <termios.h> /* POSIX terminal control definitions */#include "../../debug/debug.h"   /* includes definitions for dbprintf */#include "serial_connect.h"/* see serial_connect.h for description */int g_serial_connect_port_open = SERIAL_PORT_CLOSED;int openSerialPort(char *serial_port_filepath) {  struct termios options;  int serial_port_fd;  if (g_serial_connect_port_open != SERIAL_PORT_CLOSED) {    return g_serial_connect_port_open;  }  if ((serial_port_fd = open(serial_port_filepath, O_RDWR | O_NOCTTY | O_NDELAY)) == -1) {    dbg(DBG_PHYSICAL, CRITICAL, "FATAL ERROR: open_port: Unable to open %s\n", serial_port_filepath);    serial_port_fd = -1;    return -1;  }  fcntl(serial_port_fd, F_SETFL, 0);  tcgetattr(serial_port_fd, &options); // get the current options for the port  cfsetispeed(&options, B57600);  // set input baud rate to 57600  cfsetospeed(&options, B57600);  // set output baud rate to 57600  options.c_cflag |= (CLOCAL | CREAD); // enable the receiver and set local mode  /* setting character size, not a straighforward thing */  options.c_cflag &= ~CSIZE; /* Mask the character size bits */  options.c_cflag |= CS8;    /* Select 8 data bits */  /* setting up parity: also not straightforward: options available are:     No parity (8N1):      options.c_cflag &= ~PARENB     options.c_cflag &= ~CSTOPB     options.c_cflag &= ~CSIZE;     options.c_cflag |= CS8;             Even parity (7E1):      options.c_cflag |= PARENB     options.c_cflag &= ~PARODD     options.c_cflag &= ~CSTOPB     options.c_cflag &= ~CSIZE;     options.c_cflag |= CS7;               Odd parity (7O1):      options.c_cflag |= PARENB     options.c_cflag |= PARODD     options.c_cflag &= ~CSTOPB     options.c_cflag &= ~CSIZE;     options.c_cflag |= CS7;               Space parity is setup the same as no parity (7S1):      options.c_cflag &= ~PARENB     options.c_cflag &= ~CSTOPB     options.c_cflag &= ~CSIZE;     options.c_cflag |= CS8;     I want no parity for MICA2 */  options.c_cflag &= ~PARENB;  options.c_cflag &= ~CSTOPB;  options.c_cflag &= ~CSIZE;  options.c_cflag |= CS8;#ifdef CNEW_RTSCTS  options.c_cflag &= ~CNEW_RTSCTS; // disable hardware flow control (to enable, remove the negation and change to |=)#endif  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);  // make sure no processing is done on data: instead of "canonical" I want "raw"  /* Control input processing: the c_iflag     INPCK Enable parity check      IGNPAR Ignore parity errors      PARMRK Mark parity errors      ISTRIP Strip parity bits      IXON Enable software flow control (outgoing)      IXOFF Enable software flow control (incoming)      IXANY Allow any character to start flow again      IGNBRK Ignore break condition      BRKINT Send a SIGINT when a break condition is detected      INLCR Map NL to CR      IGNCR Ignore CR      ICRNL Map CR to NL      IUCLC Map uppercase to lowercase      IMAXBEL Echo BEL on input line too long  */  options.c_iflag &= ~(INPCK | IXON | IXOFF | ICRNL | INLCR | IMAXBEL | IGNBRK | ISTRIP);  options.c_iflag |= (IGNPAR | IXANY | IGNCR);  options.c_oflag &= ~OPOST; // for raw output, mask OPOST bit and all other flags (there are many) are ignored  /* NOTE: c_cc character array defines the bytecodes for various control characters: we don't care about them since we     masked the OPOST bit above */  /* timeouts: NOTE: Timeouts are ignored in canonical input mode or when the NDELAY option is set on the file via open or fcntl.     The VMIN and VTIME elements of the c_cc character array, are important since they     control how timeouts and minimum number of chracters work.     VMIN = minimum number of characters to read: if 0, then VTIME is taken as maximum time per character     VTIME = time to wait for first character if VMIN != 0, otherwise it specifies the time (in tenths of seconds) to wait for              incoming characters (set it to 0 if you want to block indefinitely)  */  options.c_cc[VMIN] = 0;  options.c_cc[VTIME] = BYTETIMEOUT;  /* Set the new options for the port...     TCSANOW: moke changes NOW, regardless of Tx or Rx state of port     TCSADRAIN: Wait until all transmission has finished to make changes     TCSAFLUSH: flush the input and output buffers, then make the change */  tcsetattr(serial_port_fd, TCSAFLUSH, &options);  g_serial_connect_port_open = serial_port_fd;  tcflush(serial_port_fd, TCIOFLUSH);  return serial_port_fd;}int closeSerialPort(int serial_port_fd) {  if (g_serial_connect_port_open == serial_port_fd && g_serial_connect_port_open != SERIAL_PORT_CLOSED) {    close(serial_port_fd);    return SUCCESS;  }  return FAILURE;}int flushInputPort(int serial_port_fd) {  tcflush(serial_port_fd, TCIFLUSH);  return SUCCESS;}int flushOutputPort(int serial_port_fd) {  tcflush(serial_port_fd, TCOFLUSH);  return SUCCESS;}

⌨️ 快捷键说明

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