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

📄 serial_connect.h

📁 主要用于无线传感网络的编写的书籍.对于初学者有着很大的用处
💻 H
字号:
#ifndef __SERIAL_CONNECT__#define __SERIAL_CONNECT__#include "../../types/types.h"#define BYTETIMEOUT 10 // tenths of seconds#ifndef SUCCESS#define SUCCESS 1#endif#ifndef FAILURE#define FAILURE 0#endif#define SERIAL_PORT_CLOSED -1/************************************************************************ * RULES: * 1.  You must call closeSerialPort with only the file descriptor  *     returned by openSerialPort. * 2.  If you call openSerialPort twice without calling closeSerialPort, *     you will get the same, already open, serial port desciptor back *     (i.e. it will not open two streams to the same port) *     even if you pass the function a new pathname.  In other words, *     there is a global flag that is set/reset by openSerialPort *     and closeSerialPort that does not keep track of the pathname. *     NOTE: this may be modified in the future * 3.  If you try to call closeSerialPort with anything other than  *     the open serial port file descriptor as a parameter, then *     it will instantly return failure. * 4.  Examples of how to read/write from/to the serial port are *     listed at the end of this file. ************************************************************************/// returns a useable file descriptor on success, -1 on errorint openSerialPort(char *serial_port_filepath);// returns SUCCESS on success, FAILURE on failureint closeSerialPort(int serial_port_file_descriptor);// flushes everything from input and output buffersint flushInputPort(int serial_port_file_descriptor);int flushOutputPort(int serial_port_file_descriptor);/******************************************************************************* * HOW TO USE SERIAL_CONNECT: * * 1.  First, get a file descriptor: *  * fd = openSerialPort(); *  * 2.  To write to the serial port: *  * ret_val = write(fd, "ATZ\r", num_bytes_to_write); * if (ret_val < 0) { *   fprintf(stderr, "write() of %d bytes failed!\n", num_bytes_to_write); * } *  * 3.  To read from serial port: * num_bytes_read = read(fd, &buffer, num_bytes_to_read); * if (num_bytes_read != num_bytes_to_read) { *   fprintf(stderr, "read() of %d bytes failed!\n", num_bytes_to_read); * } * NOTE: for mica2 motes, always read only one byte at a time *******************************************************************************/#endif

⌨️ 快捷键说明

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