k8s搭建mysql主从
时间: 2023-05-31 10:04:49 浏览: 143
//以下为串口通讯方式的控制程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#define FINGERPRINT_STARTCODE 0xEF01 //起始码
#define FINGERPRINT_COMMANDPACKET 0x01 //指令包
#define FINGERPRINT_DATA_PACKET 0x02 //数据包
#define FINGERPRINT_ACKPACKET 0x07 //应答包
#define FINGERPRINT_ENDDATAPACKET 0x08 //结束数据包
#define FINGERPRINT_TIMEOUT 1000 //超时时间
int fd = -1; //串口文件描述符
void serialInit(char *port) //串口初始化
{
struct termios options;
fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
perror("open");
return;
}
tcgetattr(fd, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR | ICRNL;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
}
int serialSend(unsigned char *data, int len) //串口发送数据
{
int ret = -1;
ret = write(fd, data, len);
if (ret < 0)
{
perror("write");
return -1;
}
return ret;
}
int serialRecv(unsigned char *data, int len) //串口接收数据
{
int ret = -1;
fd_set readfds;
struct timeval timeout;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
timeout.tv_sec = 0;
timeout.tv_usec = FINGERPRINT_TIMEOUT;
ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
if (ret == -1)
{
perror("select");
return -1;
}
else if (ret == 0)
{
printf("select timeout\n");
return -1;
}
else
{
ret = read(fd, data, len);
if (ret < 0)
{
perror("read");
return -1;
}
return ret;
}
}
int fingerprintVerify() //指纹验证
{
unsigned char cmd[12] = {0}; //指令
unsigned char recvBuf[12] = {0}; //接收缓存
int ret = -1; //返回值
int i = 0; //计数器
int sum = 0; //校验和
cmd[0] = (FINGERPRINT_STARTCODE >> 8) & 0xff; //起始码高位
cmd[1] = FINGERPRINT_STARTCODE & 0xff; //起始码低位
cmd[2] = 0x00; //设备地址
cmd[3] = 0x03; //数据长度
cmd[4] = FINGERPRINT_COMMANDPACKET; //指令类型
cmd[5] = 0x01; //指令代码
cmd[6] = 0x00; //参数1
cmd[7] = 0x00; //参数2
sum = cmd[4] + cmd[5] + cmd[6] + cmd[7]; //计算校验和
cmd[8] = (sum >> 8) & 0xff; //校验和高位
cmd[9] = sum & 0xff; //校验和低位
cmd[10] = (FINGERPRINT_ENDDATAPACKET >> 8) & 0xff; //结束码高位
cmd[11] = FINGERPRINT_ENDDATAPACKET & 0xff; //结束码低位
ret = serialSend(cmd, sizeof(cmd)); //发送指令
if (ret < 0)
{
printf("serial send failed\n");
return -1;
}
ret = serialRecv(recvBuf, sizeof(recvBuf)); //接收应答
if (ret < 0)
{
printf("serial recv failed\n");
return -1;
}
if (recvBuf[0] != ((FINGERPRINT_STARTCODE >> 8) & 0xff) || recvBuf[1] != (FINGERPRINT_STARTCODE & 0xff)) //判断起始码
{
printf("start code error\n");
return -1;
}
if (recvBuf[4] != FINGERPRINT_ACKPACKET) //判断是否为应答包
{
printf("not ack packet\n");
return -1;
}
if (recvBuf[5] != 0x01) //判断指令代码
{
printf("verification failed\n");
return -1;
}
return 0;
}
int main()
{
serialInit("/dev/ttyUSB0"); //初始化串口
if (fingerprintVerify() == 0) //指纹验证
{
printf("verification succeed\n");
}
else
{
printf("verification failed\n");
}
close(fd); //关闭串口
return 0;
}
//以下为USB通讯方式的控制程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
#define FINGERPRINT_STARTCODE 0xEF01 //起始码
#define FINGERPRINT_COMMANDPACKET 0x01 //指令包
#define FINGERPRINT_DATA_PACKET 0x02 //数据包
#define FINGERPRINT_ACKPACKET 0x07 //应答包
#define FINGERPRINT_ENDDATAPACKET 0x08 //结束数据包
#define FINGERPRINT_TIMEOUT 1000 //超时时间
libusb_device_handle *dev_handle = NULL; //USB设备句柄
int usbInit() //USB初始化
{
int ret = -1;
libusb_init(NULL);
dev_handle = libusb_open_device_with_vid_pid(NULL, 0x0403, 0x6001); //根据设备的VID和PID打开设备
if (dev_handle == NULL)
{
printf("open device failed\n");
libusb_exit(NULL);
return -1;
}
ret = libusb_claim_interface(dev_handle, 0); //申请接口0
if (ret < 0)
{
printf("claim interface failed\n");
libusb_close(dev_handle);
libusb_exit(NULL);
return -1;
}
return 0;
}
int usbSend(unsigned char *data, int len) //USB发送数据
{
int ret = -1;
ret = libusb_bulk_transfer(dev_handle, 0x02, data, len, &len, FINGERPRINT_TIMEOUT); //使用bulk传输方式发送数据
if (ret < 0)
{
printf("send data failed\n");
return -1;
}
return len;
}
int usbRecv(unsigned char *data, int len) //USB接收数据
{
int ret = -1;
ret = libusb_bulk_transfer(dev_handle, 0x81, data, len, &len, FINGERPRINT_TIMEOUT); //使用bulk传输方式接收数据
if (ret < 0)
{
printf("recv data failed\n");
return -1;
}
return len;
}
int fingerprintVerify() //指纹验证
{
unsigned char cmd[12] = {0}; //指令
unsigned char recvBuf[12] = {0}; //接收缓存
int ret = -1; //返回值
int i = 0; //计数器
int sum = 0; //校验和
cmd[0] = (FINGERPRINT_STARTCODE >> 8) & 0xff; //起始码高位
cmd[1] = FINGERPRINT_STARTCODE & 0xff; //起始码低位
cmd[2] = 0x00; //设备地址
cmd[3] = 0x03; //数据长度
cmd[4] = FINGERPRINT_COMMANDPACKET; //指令类型
cmd[5] = 0x01; //指令代码
cmd[6] = 0x00; //参数1
cmd[7] = 0x00; //参数2
sum = cmd[4] + cmd[5] + cmd[6] + cmd[7]; //计算校验和
cmd[8] = (sum >> 8) & 0xff; //校验和高位
cmd[9] = sum & 0xff; //校验和低位
cmd[10] = (FINGERPRINT_ENDDATAPACKET >> 8) & 0xff; //结束码高位
cmd[11] = FINGERPRINT_ENDDATAPACKET & 0xff; //结束码低位
ret = usbSend(cmd, sizeof(cmd)); //发送指令
if (ret < 0)
{
printf("usb send failed\n");
return -1;
}
ret = usbRecv(recvBuf, sizeof(recvBuf)); //接收应答
if (ret < 0)
{
printf("usb recv failed\n");
return -1;
}
if (recvBuf[0] != ((FINGERPRINT_STARTCODE >> 8) & 0xff) || recvBuf[1] != (FINGERPRINT_STARTCODE & 0xff)) //判断起始码
{
printf("start code error\n");
return -1;
}
if (recvBuf[4] != FINGERPRINT_ACKPACKET) //判断是否为应答包
{
printf("not ack packet\n");
return -1;
}
if (recvBuf[5] != 0x01) //判断指令代码
{
printf("verification failed\n");
return -1;
}
return 0;
}
int main()
{
usbInit(); //初始化USB
if (fingerprintVerify() == 0) //指纹验证
{
printf("verification succeed\n");
}
else
{
printf("verification failed\n");
}
libusb_release_interface(dev_handle, 0); //释放接口0
libusb_close(dev_handle); //关闭设备
libusb_exit(NULL); //退出libusb
return 0;
}
//以下为TTL通讯方式的控制程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiringPi.h>
#define FINGERPRINT_STARTCODE 0xEF01 //起始码
#define FINGERPRINT_COMMANDPACKET 0x01 //指令包
#define FINGERPRINT_DATA_PACKET 0x02 //数据包
#define FINGERPRINT_ACKPACKET 0x07 //应答包
#define FINGERPRINT_ENDDATAPACKET 0x08 //结束数据包
#define FINGERPRINT_TIMEOUT 1000 //超时时间
const int PIN_TXD = 15; //发送引脚
const int PIN_RXD = 16; //接收引脚
void ttlInit() //TTL初始化
{
wiringPiSetup();
pinMode(PIN_TXD, OUTPUT);
pinMode(PIN_RXD, INPUT);
}
int ttlSend(unsigned char *data, int len) //TTL发送数据
{
int i = 0;
int j = 0;
int sum = 0;
digitalWrite(PIN_TXD, LOW);
delay(10);
for (i = 0; i < len; i++) //循环发送每个字节
{
for (j = 0; j < 8; j++) //循环发送每个位
{
digitalWrite(PIN_TXD, (data[i] >> j) & 0x01); //发送数据位
delayMicroseconds(100);
}
digitalWrite(PIN_TXD, HIGH); //发送停止位
delayMicroseconds(100);
}
return len;
}
int ttlRecv(unsigned char *data, int len) //TTL接收数据
{
int i = 0;
int j = 0;
int ret = -1;
int sum = 0;
for (i = 0; i < len; i++) //循环接收每个字节
{
for (j = 0; j < 8; j++) //循环接收每个位
{
while (digitalRead(PIN_RXD) == LOW); //等待接收到起始位
delayMicroseconds(50);
data[i] |= digitalRead(PIN_RXD) << j; //接收数据位
}
while (digitalRead(PIN_RXD) == HIGH); //等待接收到停止位
delayMicroseconds(50);
}
return len;
}
int fingerprintVerify() //指纹验证
{
unsigned char cmd[12] = {0}; //指令
unsigned char recvBuf[12] = {0}; //接收缓存
int ret = -1; //返回值
int i = 0; //计数器
int sum = 0; //校验和
cmd[0] = (FINGERPRINT_STARTCODE >> 8) & 0xff; //起始码高位
cmd[1] = FINGERPRINT_STARTCODE & 0xff; //起始码低位
cmd[2] = 0x00; //设备地址
cmd[3] = 0x03; //数据长度
cmd[4] = FINGERPRINT_COMMANDPACKET; //指令类型
cmd[5] = 0x01; //指令代码
cmd[6] = 0x00; //参数1
cmd[7] = 0x00; //参数2
sum = cmd[4] + cmd[5] + cmd[6] + cmd[7]; //计算校验和
cmd[8] = (sum >> 8) & 0xff; //校验和高位
cmd[9] = sum & 0xff; //校验和低位
cmd[10] = (FINGERPRINT_ENDDATAPACKET >> 8) & 0xff; //结束码高位
cmd[11] = FINGERPRINT_ENDDATAPACKET & 0xff; //结束码低位
ret = ttlSend(cmd, sizeof(cmd)); //发送指令
if (ret < 0)
{
printf("ttl send failed\n");
return -1;
}
ret = ttlRecv(recvBuf, sizeof(recvBuf)); //接收应答
if (ret < 0)
{
printf("ttl recv failed\n");
return -1;
}
if (recvBuf[0] != ((FINGERPRINT_STARTCODE >> 8) & 0xff) || recvBuf[1] != (FINGERPRINT_STARTCODE & 0xff)) //判断起始码
{
printf("start code error\n");
return -1;
}
if (recvBuf[4] != FINGERPRINT_ACKPACKET) //判断是否为应答包
{
printf("not ack packet\n");
return -1;
}
if (recvBuf[5] != 0x01) //判断指令代码
{
printf("verification failed\n");
return -1;
}
return 0;
}
int main()
{
ttlInit(); //初始化TTL
if (fingerprintVerify() == 0) //指纹验证
{
printf("verification succeed\n");
}
else
{
printf("verification failed\n");
}
return 0;
}
阅读全文