最精簡的C程序

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/termios.h>
#include <sys/time.h>ios

#ifndef EMBED
#include <sys/select.h>
#endifci

/*****************************************************************************/rem

char *version = "1.0.0";get

/*****************************************************************************/string

/*
 * Define some parity flags, internal use only.
 */
#define PARITY_NONE 0
#define PARITY_EVEN 1
#define PARITY_ODD 2it

/*
 * Default port settings.
 */
int  clocal = 0;
int  hardware = 0;
int  software = 0;
int  passflow = 0;
int  parity = PARITY_NONE;
int  databits = 8;
unsigned int baud = 9600;io

char  *devname;
int  gotdevice = 0;table

/*
 * Working termios settings.
 */
struct termios savetio;ioc


/*
 * Temporary buffer to use when working.
 */
unsigned char buf[512];select

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

/*
 * Baud rate table for baud rate conversions.
 */
typedef struct baudmap {
 unsigned int baud;
 unsigned int flag;
} baudmap_t;


struct baudmap baudtable[] = {
 { 0, B0 },
 { 50, B50 },
 { 75, B75 },
 { 110, B110 },
 { 134, B134 },
 { 150, B150 },
 { 200, B200 },
 { 300, B300 },
 { 600, B600 },
 { 1200, B1200 },
 { 1800, B1800 },
 { 2400, B2400 },
 { 4800, B4800 },
 { 9600, B9600 },
 { 19200, B19200 },
 { 38400, B38400 },
 { 57600, B57600 },
 { 115200, B115200 },
 { 230400, B230400 },
 { 460800, B460800 }
};

#define NRBAUDS  (sizeof(baudtable) / sizeof(struct baudmap))


/*****************************************************************************/
/*
 * Verify that the supplied baud rate is valid.
 */

int baud2flag(unsigned int speed)
{
 int i;

 for (i = 0; (i < NRBAUDS); i++) {
  if (speed == baudtable[i].baud)
   return(baudtable[i].flag);
 }
 return(-1);
}


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

/*
 * Set up remote (connect) port termio settings according to
 * user specification.
 */

int setremotetermios(int fd)
{
 struct termios tio;

 memset(&tio, 0, sizeof(tio));
 tio.c_cflag = CREAD | HUPCL | baud2flag(115200);

 if (clocal)
  tio.c_cflag |= CLOCAL;

 switch (parity) {
 case PARITY_ODD: tio.c_cflag |= PARENB | PARODD; break;
 case PARITY_EVEN: tio.c_cflag |= PARENB; break;
 default:  break;
 }

 tio.c_cflag |= ((databits == 7) ? CS7 : CS8);

 if (software)
  tio.c_iflag |= IXON | IXOFF;
 if (hardware)
  tio.c_cflag |= CRTSCTS;

 tio.c_cc[VMIN] = 1;
 tio.c_cc[VTIME] = 0;

 if (tcsetattr(fd, TCSAFLUSH, &tio) < 0) {
  fprintf(stderr, "ERROR: ioctl(TCSETS) failed\n");
  exit(1);
 }
 return(0);
}

 


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

#define MAX_RECV_BUF    (100)
#define MAX_SEND_BUF    (100)

int main(int argc, char *argv[])
{
 int fd, c;
    char buf[MAX_RECV_BUF], buf1[MAX_SEND_BUF];
    int i;
    fd_set infds;

 /*
  * Check device is real, and open it.
  */
 if ((fd = open("/dev/s3c2410_serial1", O_RDWR)) < 0) {
  fprintf(stderr, "ERROR: failed to open %s, errno=%d\n",
   devname, errno);
  exit(0);
 }

 setremotetermios(fd);

 printf("Connected successfull\r\n");

 while(1)
  {

  sprintf(buf1, "at+cmgf=1");
  i = strlen("at+cmgf=1");

  buf1[i] = 13;

  if(0 > write(fd, buf1, i+1))
   {
   printf("send string:at+cmgf=1 error\r\n");
   }
  printf("send string:at+cmgf=1\r\n");

  memset(buf1, 0, MAX_SEND_BUF);
  sprintf(buf1, "at+cmgs=\"18629268232\"");
  i = strlen("at+cmgs=\"18629268232\"");

  buf1[i] = 13;
  if(0 > write(fd, buf1, i+1))
   {

   printf("send string:at+cmgs=\"18629268232\"\r\n");
   }
  printf("send string:at+cmgs=\"18629268232\"\r\n");

  memset(buf1, 0, MAX_SEND_BUF);
  sprintf(buf1, "Welcome to ToCore education\n");

  i = strlen("Welcome to ToCore education\n");

  buf1[i] = 0x1A;

  write(fd, buf1, (i + 1));
  printf("send string:Welcome to ToCore education\r\n");
  }  
 close(fd);
 printf("Disconnected.\n");
 exit(0);
}

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

相關文章
相關標籤/搜索