All pastes #10677 Raw Edit

Astro73

public cpp v1 · immutable
#10677 ·published 2005-04-30 18:37 UTC
rendered paste body
/*******************************************************************************  Program : IFI Linux PicLoader (ifi-picloader)  Author  : Ryan Waliany (ryan@scv.net)--------------------------------------------------------------------------------  Copyright: (C) 2004 by Ryan Waliany  Notes:     Please distribute and make better.--------------------------------------------------------------------------------  Filename    : serial.cpp  Description : Serial communications*******************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/#include <iostream>#include <fstream>#include <vector>#include <string>#include <fcntl.h>#if UNIX#include <unistd.h>#endif#include <stdio.h>#if UNIX#include <termios.h>#include <sys/ioctl.h>#elseif WIN32#include <io.h>#include <windows.h>#endif#include <time.h>#include <errno.h>#include "serial.h"using namespace std;void closetty(TTY_HANDLE fd){#if WIN32	CloseHandle(fd);#elseif UNIX   _close(fd);#elseif MAC_OSX	//TODO: Write#endif}// Returns the amount of data readunsigned long readtty(TTY_HANDLE fd, void* buffer, unsigned long length){#if WIN32   DWORD read;   // Minus one because we needed space for the null   if (ReadFile(fd, buffer, length, &read, NULL))   {      return read;   }   else   {      return -1;   }#elseif UNIX   int data = read(fd, buffer, length);   if (data >= 0)   {      return data;   }   else   {      return -1;   }#elseif MAC_OSX	//TODO: Write#endif}// Returns the amount writtenunsigned long writetty(TTY_HANDLE fd, void *data, unsigned long length){#if WIN32   DWORD wrote;   if (!WriteFile(fd, txt, length, &wrote, NULL))#elseif UNIX   size_t   if ((write(fd, txt, length)) < 0)#endif   {      return false;   }   return true;#if MAC_OSX	//TODO: Write#endif}TTY_HANDLE opentty(unsigned int tty){	TTY_HANDLE fd;#if WIN32     // C O M # # #\0 	char *port =   "\0\0\0\0\0\0\0"; // COM###\0#elseiff UNIX    // / d e v / t t y S # # #\0 	char *port = "\0\0\0\0\0\0\0\0\0\0\0\0\0"; // /dev/ttyS###\0#elseif MAC_OSX	//TODO: Write#endif#if WIN32	sprintf(port, "COM%u", PortNum);#elseif UNIX	sprintf(port, "/dev/ttyS%u", PortNum);#elseif MAC_OSX	//TODO: Write#endif	#if WIN32	fd = CreateFile(port,                    GENERIC_READ | GENERIC_WRITE,                    0,                    NULL,                    OPEN_EXISTING,                    NULL,                    NULL);    // We need to set up the port    DCB config;    // Get current config    BOOL fSuccess = GetCommState(fd, &config);    // Set up struct    config.BaudRate = 115200;    config.fBinary = TRUE;    config.fParity = FALSE;    config.fDtrControl = DTR_CONTROL_DISABLE;    config.fDsrSensitivity = FALSE;    config.fInX = FALSE;    config.fOutX = FALSE;    config.fErrorChar = FALSE;    config.fNull = FALSE;    config.fRtsControl = RTS_CONTROL_DISABLE;    config.fAbortOnError = FALSE;    config.ByteSize = 8;    config.Parity = NOPARITY;    config.StopBits = ONESTOPBIT;    // Set config    SetCommState(fd, &config);#elseif UNIX   struct termios tios;   fd = open(tty, O_RDWR | O_NOCTTY);   if (fd == -1)   {      return -1;   }   bzero(&tios, sizeof(termios));   tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;   tios.c_iflag = IGNPAR;   tios.c_oflag = 0;   tios.c_lflag = 0;   tios.c_cc[VINTR] = 0;   tios.c_cc[VQUIT] = 0;   tios.c_cc[VERASE] = 0;   tios.c_cc[VKILL] = 0;   tios.c_cc[VEOF] = 0;   tios.c_cc[VTIME] = 0;   tios.c_cc[VSWTC] = 0;   tios.c_cc[VSTART] = 0;   tios.c_cc[VSTOP] = 0;   tios.c_cc[VSUSP] = 0;   tios.c_cc[VEOL] = 0;   tios.c_cc[VREPRINT] = 0;   tios.c_cc[VDISCARD] = 0;   tios.c_cc[VWERASE] = 0;   tios.c_cc[VLNEXT] = 0;   tios.c_cc[VEOL2] = 0;   tios.c_cc[VMIN] = 0;   tcflush(fd, TCIFLUSH);   tcsetattr(fd, TCSANOW, &tios);#elseif MAC_OSX	//TODO: Write#endif   return fd;}