?? serial.c
字號:
/* Copyright (C) 1999-2001 Quality Quorum, Inc. Copyright (C) 2002 Chris Liechti and Steve Underwood. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. QQI can be contacted as qqi@world.std.com Implementation of serial connection used on target side. Exported Data: None Imported Data: None Static Data: serial_xXXX - static data representing status of serial Global Functions: serial_xXXX - see definitions in serial_xXXX Static Functions: None $Id: serial.c,v 1.2 2003/12/11 12:42:23 XHKJAMES Exp $ */#ifdef HAVE_CONFIG_H#include "config.h"#endif /* HAVE_CONFIG_H */#include <assert.h>#include <stdint.h>#if defined(WIN32)#include <windows.h>#else#include <stdio.h>#include <string.h>#include <termios.h>#include <errno.h>#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif /* HAVE_FCNTL_H */#ifdef HAVE_UNISTD_H#include <unistd.h>#include <sys/types.h>#endif /* HAVE_UNISTD_H */#ifdef HAVE_SYS_TIME_H#include <sys/time.h>#else#include <time.h>#endif /* HAVE_SYS_TIME_H */#include <sys/socket.h>#include <netinet/in.h>#include <netinet/tcp.h>#endif#include "serial.h"#if !defined(FALSE)#define FALSE 0#endif#if !defined(TRUE)#define TRUE (!FALSE)#endif#define SERIAL_BUFF_SIZE 8192
#define MAX_PORTS 32
static int initialised = FALSE;
typedef struct{ /* Serial device file descriptor */#if defined(WIN32) HANDLE handle;#else int fd;#endif /* Input buffer */ char serial_in_buf[SERIAL_BUFF_SIZE]; /* Pointer to the current char in buffer */ char *serial_in_ptr; /* Number of characters in buffer */ int serial_in_count;} port_data_t;static port_data_t ports[MAX_PORTS] ={ {#if defined(WIN32) INVALID_HANDLE_VALUE,#else -1,#endif { 0 }, NULL, 0 }};/* Check check whether baud is an acceptable rate */int serial_check_baud(int baud){ switch (baud) { case 50: case 75: case 110: case 134: case 150: case 1800: case 2400: case 4800: case 9600: case 19200: case 38400: case 57600: case 115200:#if defined(B230400) case 230400:#endif#if defined(B460800) case 460800:#endif#if defined(B921600) case 921600:#endif return TRUE; } return FALSE;}/* Open serial connection */int serial_open(char *name, int baud, int parity, int bits){#if defined(WIN32) DCB dcb; BOOL res; COMMTIMEOUTS ctm; int port; assert(name != NULL);
if (!initialised)
{
for (port = 0; port < MAX_PORTS; port++) { ports[port].handle = INVALID_HANDLE_VALUE; ports[port].serial_in_buf[0] = '\0'; ports[port].serial_in_ptr = ports[port].serial_in_buf; ports[port].serial_in_count = 0; }
initialised = TRUE;
} for (port = 0; port < MAX_PORTS; port++) { if (ports[port].handle == INVALID_HANDLE_VALUE) break; } if (port >= MAX_PORTS) return -1; /* Open port with exclusive access and no security attributes */ ports[port].handle = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (ports[port].handle == INVALID_HANDLE_VALUE) return -2; ports[port].serial_in_ptr = ports[port].serial_in_buf; /* Set raw mode */ if (!(res = GetCommState(ports[port].handle, &dcb))) { CloseHandle(ports[port].handle); ports[port].handle = INVALID_HANDLE_VALUE; return -3; } dcb.BaudRate = baud; dcb.fBinary = TRUE; dcb.fParity = (parity) ? TRUE : FALSE; dcb.fOutxCtsFlow = FALSE; dcb.fOutxDsrFlow = FALSE; dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fDsrSensitivity = FALSE; dcb.fOutX = FALSE; dcb.fInX = FALSE; dcb.fErrorChar = FALSE; dcb.fNull = FALSE; dcb.fRtsControl = RTS_CONTROL_ENABLE; dcb.fAbortOnError = FALSE; dcb.ByteSize = bits;
switch (parity)
{
case 0: dcb.Parity = NOPARITY;
break; case 1: dcb.Parity = ODDPARITY;
break; case 2: dcb.Parity = EVENPARITY;
break;
} dcb.StopBits = ONESTOPBIT; if (!(res = SetCommState(ports[port].handle, &dcb))) { CloseHandle(ports[port].handle); ports[port].handle = INVALID_HANDLE_VALUE; return -4; } /* Reset timeouts */ ctm.ReadIntervalTimeout = 20; /* Allow 20ms between characters */ ctm.ReadTotalTimeoutConstant = 0; /* This one will be changed later */ ctm.ReadTotalTimeoutMultiplier = 0; ctm.WriteTotalTimeoutConstant = 0; ctm.WriteTotalTimeoutMultiplier = 0; if (!(res = SetCommTimeouts(ports[port].handle, &ctm))) { assert(0); CloseHandle(ports[port].handle); ports[port].handle = INVALID_HANDLE_VALUE; return -5; }#else int ret; int port; struct termios termios; assert(name != NULL); if (!initialised)
{
for (port = 0; port < MAX_PORTS; port++) { ports[port].fd = -1; ports[port].serial_in_buf[0] = '\0'; ports[port].serial_in_ptr = ports[port].serial_in_buf; ports[port].serial_in_count = 0; }
initialised = TRUE;
} for (port = 0; port < MAX_PORTS; port++) { if (ports[port].fd < 0) break; } if (port >= MAX_PORTS) return -1; if (strchr (name, ':') != NULL) { /* We do not support TCP connections right now */ return -1; } if ((ports[port].fd = open(name, O_RDWR)) < 0) { ports[port].fd = -1; return -1; } /* Set raw mode */ do { ret = tcgetattr(ports[port].fd, &termios); } while (ret < 0 && errno == EINTR); if (ret < 0) { close(ports[port].fd); ports[port].fd = -1; return -1; } termios.c_iflag = 0; termios.c_oflag = 0; termios.c_lflag = 0; termios.c_cflag = CLOCAL | CREAD | CS8; termios.c_cc[VMIN] = 0; termios.c_cc[VTIME] = 0; switch (baud) { case 50: ret = cfsetospeed(&termios, B50); break; case 75: ret = cfsetospeed(&termios, B75); break; case 110: ret = cfsetospeed(&termios, B110); break; case 134: ret = cfsetospeed(&termios, B134); break; case 150: ret = cfsetospeed(&termios, B150); break; case 200: ret = cfsetospeed(&termios, B200); break; case 1800: ret = cfsetospeed(&termios, B1800); break; case 2400: ret = cfsetospeed(&termios, B2400); break; case 4800: ret = cfsetospeed(&termios, B4800); break; case 9600: ret = cfsetospeed(&termios, B9600); break; case 19200: ret = cfsetospeed(&termios, B19200); break; case 38400: ret = cfsetospeed(&termios, B38400); break; case 57600: ret = cfsetospeed(&termios, B57600); break; case 115200:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -