Parcourir la source

Add ability to poen regular files in wiringSerial

pull/158/head
Jim Parziale il y a 2 ans
Parent
révision
d3c6ecda3d
3 fichiers modifiés avec 165 ajouts et 84 suppressions
  1. +69
    -0
      wiringPi/tSerial.c
  2. +7
    -7
      wiringPi/wiringPi.c
  3. +89
    -77
      wiringPi/wiringSerial.c

+ 69
- 0
wiringPi/tSerial.c Voir le fichier

@@ -0,0 +1,69 @@
// ****************************************************************************
// tSerial.c
//
// Build: gcc tSerial.c wiringSerial.o -o tSerial
// ****************************************************************************

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "wiringSerial.h"

// @DEBUG
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// @DEBUG

// ****************************************************************************

#define HERE() do { printf ("%s(%d).%s\r\n", __FILE__, __LINE__, __FUNCTION__); } while(0)
#define DPRINTF(fmt, args...) do { \
printf ("%s(%d).%s ", __FILE__, __LINE__, __FUNCTION__); \
printf(fmt, ## args); \
fflush(stdout); \
} while (0)

// ****************************************************************************

int main(int argc, char *argv[])
{
// ----------------------------------------------------

printf ("\r\n\r\n");
HERE();

// ----------------------------------------------------

HERE();
#if 1
const char devName[] = "/dev/tty";
int fd = serialOpen(devName, 115200);
#else
const char devName[] = "test.txt";
int fd = serialOpen(devName, -1);
#endif
DPRINTF("fd = %d\r\n", fd);
if (fd == -1)
{
perror("open");
DPRINTF("ERROR: fd = %d\r\n", fd);
}

serialPrintf(fd, "%s(%d).%s HELLO THERE!\r\n", __FILE__, __LINE__, __FUNCTION__);

serialFlush(fd);
serialClose(fd);

// ----------------------------------------------------

HERE();
printf ("\r\n");

return EXIT_SUCCESS;
}

// ****************************************************************************

+ 7
- 7
wiringPi/wiringPi.c Voir le fichier

@@ -1395,7 +1395,7 @@ void pullUpDnControl (int pin, int pud)

if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
{
/**/ if (wiringPiMode == WPI_MODE_PINS)
if (wiringPiMode == WPI_MODE_PINS)
pin = pinToGpio[pin];
else if (wiringPiMode == WPI_MODE_PHYS)
pin = physToGpio[pin];
@@ -1412,8 +1412,8 @@ void pullUpDnControl (int pin, int pud)

switch (pud)
{
case PUD_OFF: pull = 0; break;
case PUD_UP: pull = 1; break;
case PUD_OFF: pull = 0; break;
case PUD_UP: pull = 1; break;
case PUD_DOWN: pull = 2; break;
default: return; /* An illegal value */
}
@@ -1426,11 +1426,11 @@ void pullUpDnControl (int pin, int pud)
else
{
// legacy pull up/down method
*(gpio + GPPUD) = pud & 3; delayMicroseconds (5);
*(gpio + gpioToPUDCLK[pin]) = 1 << (pin & 31); delayMicroseconds (5);
*(gpio + GPPUD) = pud & 3; delayMicroseconds (5);
*(gpio + gpioToPUDCLK[pin]) = 1 << (pin & 31); delayMicroseconds (5);

*(gpio + GPPUD) = 0; delayMicroseconds (5);
*(gpio + gpioToPUDCLK[pin]) = 0; delayMicroseconds (5);
*(gpio + GPPUD) = 0; delayMicroseconds (5);
*(gpio + gpioToPUDCLK[pin]) = 0; delayMicroseconds (5);
}
}
else // Extension module


+ 89
- 77
wiringPi/wiringSerial.c Voir le fichier

@@ -27,99 +27,111 @@
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "wiringSerial.h"

/*
* serialOpen:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
* Open and initialise the serial port, setting port parameters
*********************************************************************************
*/

int serialOpen (const char *device, const int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
struct termios options;
speed_t myBaud;
int status, fd;
int regularFile = 0;

switch (baud)
{
case 50: myBaud = B50 ; break ;
case 75: myBaud = B75 ; break ;
case 110: myBaud = B110 ; break ;
case 134: myBaud = B134 ; break ;
case 150: myBaud = B150 ; break ;
case 200: myBaud = B200 ; break ;
case 300: myBaud = B300 ; break ;
case 600: myBaud = B600 ; break ;
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 4800: myBaud = B4800 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
case 57600: myBaud = B57600 ; break ;
case 115200: myBaud = B115200 ; break ;
case 230400: myBaud = B230400 ; break ;
case 460800: myBaud = B460800 ; break ;
case 500000: myBaud = B500000 ; break ;
case 576000: myBaud = B576000 ; break ;
case 921600: myBaud = B921600 ; break ;
case 1000000: myBaud = B1000000 ; break ;
case 1152000: myBaud = B1152000 ; break ;
case 1500000: myBaud = B1500000 ; break ;
case 2000000: myBaud = B2000000 ; break ;
case 2500000: myBaud = B2500000 ; break ;
case 3000000: myBaud = B3000000 ; break ;
case 3500000: myBaud = B3500000 ; break ;
case 4000000: myBaud = B4000000 ; break ;
case 50: myBaud = B50; break;
case 75: myBaud = B75; break;
case 110: myBaud = B110; break;
case 134: myBaud = B134; break;
case 150: myBaud = B150; break;
case 200: myBaud = B200; break;
case 300: myBaud = B300; break;
case 600: myBaud = B600; break;
case 1200: myBaud = B1200; break;
case 1800: myBaud = B1800; break;
case 2400: myBaud = B2400; break;
case 4800: myBaud = B4800; break;
case 9600: myBaud = B9600; break;
case 19200: myBaud = B19200; break;
case 38400: myBaud = B38400; break;
case 57600: myBaud = B57600; break;
case 115200: myBaud = B115200; break;
case 230400: myBaud = B230400; break;
case 460800: myBaud = B460800; break;
case 500000: myBaud = B500000; break;
case 576000: myBaud = B576000; break;
case 921600: myBaud = B921600; break;
case 1000000: myBaud = B1000000; break;
case 1152000: myBaud = B1152000; break;
case 1500000: myBaud = B1500000; break;
case 2000000: myBaud = B2000000; break;
case 2500000: myBaud = B2500000; break;
case 3000000: myBaud = B3000000; break;
case 3500000: myBaud = B3500000; break;
case 4000000: myBaud = B4000000; break;

// Undocumented: If baud is -1, treat device as a regular file!
case -1: myBaud = B115200; regularFile = 1; break;

default:
return -2 ;
return -2;
}

if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
if (!regularFile)
{
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1;

fcntl (fd, F_SETFL, O_RDWR) ;
fcntl (fd, F_SETFL, O_RDWR);

// Get and modify current options:
// Get and modify current options:

tcgetattr (fd, &options) ;
tcgetattr (fd, &options);

cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
cfmakeraw (&options);
cfsetispeed (&options, myBaud);
cfsetospeed (&options, myBaud);

options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;

options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
options.c_cc [VMIN] = 0;
options.c_cc [VTIME] = 100; // Ten seconds (100 deciseconds)

tcsetattr (fd, TCSANOW, &options) ;
tcsetattr (fd, TCSANOW, &options);

ioctl (fd, TIOCMGET, &status);
ioctl (fd, TIOCMGET, &status);

status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
status |= TIOCM_DTR;
status |= TIOCM_RTS;

ioctl (fd, TIOCMSET, &status);
ioctl (fd, TIOCMSET, &status);

usleep (10000) ; // 10mS
usleep (10000); // 10mS
}
else
{
// Want to open a regular file
if ((fd = open (device, O_RDWR | O_CREAT, 0666)) == -1)
return -1;
}

return fd ;
return fd;
}


@@ -131,7 +143,7 @@ int serialOpen (const char *device, const int baud)

void serialFlush (const int fd)
{
tcflush (fd, TCIOFLUSH) ;
tcflush (fd, TCIOFLUSH);
}


@@ -143,7 +155,7 @@ void serialFlush (const int fd)

void serialClose (const int fd)
{
close (fd) ;
close (fd);
}


@@ -155,7 +167,7 @@ void serialClose (const int fd)

void serialPutchar (const int fd, const unsigned char c)
{
write (fd, &c, 1) ;
write (fd, &c, 1);
}


@@ -167,7 +179,7 @@ void serialPutchar (const int fd, const unsigned char c)

void serialPuts (const int fd, const char *s)
{
write (fd, s, strlen (s)) ;
write (fd, s, strlen (s));
}

/*
@@ -178,14 +190,14 @@ void serialPuts (const int fd, const char *s)

void serialPrintf (const int fd, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
va_list argp;
char buffer [1024];

va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
va_start (argp, message);
vsnprintf (buffer, 1023, message, argp);
va_end (argp);

serialPuts (fd, buffer) ;
serialPuts (fd, buffer);
}


@@ -197,12 +209,12 @@ void serialPrintf (const int fd, const char *message, ...)

int serialDataAvail (const int fd)
{
int result ;
int result;

if (ioctl (fd, FIONREAD, &result) == -1)
return -1 ;
return -1;

return result ;
return result;
}


@@ -216,10 +228,10 @@ int serialDataAvail (const int fd)

int serialGetchar (const int fd)
{
uint8_t x ;
uint8_t x;

if (read (fd, &x, 1) != 1)
return -1 ;
return -1;

return ((int)x) & 0xFF ;
return ((int)x) & 0xFF;
}

Chargement…
Annuler
Enregistrer