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

📄 xw.c

📁 串口向mysql发送文件程序。客户端返回。 简单的程序示范
💻 C
字号:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "mysql.h"

 

#define BAUDRATE B9600
#define DEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1

int insertdb(int d1, int d2, int d3, int d4) {
 MYSQL connect;
 int res, no1, no2, sd1, sd2;
 char *query = "INSERT INTO mydata  ( stime, sno1, sno2, sdata1, sdata2 ) VALUES ( '%s', %d, %d, %x, %x)";
 char *sql, *st1;
 struct tm *ptr;
 time_t lt;
 
 no1 = d1;
 no2 = d2;
 sd1 = d3;
 sd2 = d4;
 lt = time(NULL);
 ptr = localtime(&lt);
 st1 = (char *)asctime(ptr);
 st1[strlen(st1) -1 ] = '\0';
 sql = (char *)malloc(255*sizeof(char));
 sprintf(sql, query, st1, no1, no2, sd1, sd2);
 
/* debug here
 printf("%c : ", st1[strlen(st1)]);
 printf("%s : %d :",sql, strlen(st1));
 return EXIT_SUCCESS;
*/
 
 mysql_init(&connect);
 
 if(mysql_real_connect(&connect, "localhost", "root", "root", "mytest", 0, NULL, 0)) {
  printf("connect success!\n");

  res = mysql_query(&connect, sql);

  if(!res) {
   printf("insert success!\n");
  } else {
   fprintf(stderr, "insert error %d: %s\n", mysql_errno(&connect), mysql_error(&connect));
   return EXIT_FAILURE;
  }

  mysql_close(&connect);
 } else {
  fprintf(stderr, "connect fail!\n");
  return EXIT_FAILURE;
 }

 return EXIT_SUCCESS;
}

int main(void) {
 int fd, res_w, res_r, i, j, k;
 struct termios oldtio,newtio;
 char inbuf[255];
 char cbuf[4];
 int buf[4];

 res_w = 0;
 res_r = 0;
 
 fd = open(DEVICE, O_RDWR | O_NOCTTY ); // | O_NDELAY);
 if(fd < 0) {
  perror(DEVICE);
  exit(-1);
 }

 tcgetattr(fd, &oldtio);

bzero(&newtio,sizeof(struct termios));

newtio.c_cflag|= (CLOCAL | CREAD);
newtio.c_cflag|=BAUDRATE;
newtio.c_cflag&=~CSTOPB;
newtio.c_cflag&=~PARENB;
newtio.c_cflag&=~CSIZE;
newtio.c_cflag|=CS8;
newtio.c_cflag&=~CRTSCTS;

newtio.c_lflag=0;

newtio.c_oflag=0;

newtio.c_cc[VMIN]=4;
newtio.c_cc[VTIME]=0;

newtio.c_iflag&=~(IXON|IXOFF|IXANY); 

cfsetispeed(&newtio, BAUDRATE);
cfsetospeed(&newtio, BAUDRATE);

tcsetattr(fd, TCSANOW, &newtio);

tcflush(fd, TCIFLUSH);

         cbuf[0] = 0x00; 
//  cbuf[1] = 0x00;

  j = 0;

for(k = 0; k < 4; k++) {
 switch (j) {
  case 0: 
  default:
   cbuf[1] = 0x00;
   j = 2;
   break;
  case 2:
   cbuf[1] = 0x02;
   j = 0;
   break;
 }
   
 res_w = write(fd, cbuf, 2);

/* debug here 
printf("cbuf : %x %x \n", cbuf[0], cbuf[1]);
printf("buf : %x : %x : %x : %x \n", inbuf[0], inbuf[1], inbuf[2], inbuf[3]);
*/
 res_r = read(fd, &inbuf, 255);
 
 if(res_r != -1) {
  for(i = 0; i < res_r; i++) {
   buf[i] = (int)inbuf[i];
   buf[i] = buf[i] & 0xff;
  // printf(" %x ", buf[i]);   
  }
  printf("\n");
  if(insertdb(buf[0], buf[1], buf[2], buf[3]))
   printf("insert into db success!");
 }
 else {
  perror("read fail");
  exit(-1);
 }// if end here
}// for end here 

 tcsetattr(fd, TCSANOW, &oldtio);

 close(fd);
 exit(0);
} // main end here

⌨️ 快捷键说明

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