Last active
April 25, 2022 14:13
-
-
Save penk/560193fcb1df323054268a9125b15c51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <time.h> | |
#include <wiringSerial.h> | |
#define UART_NODE "/dev/ttyS0" | |
enum KEY_TYPE { | |
SHORT_PRESS = 1, | |
MIDDLE_PRESS = 2, | |
LONG_PRESS = 3, | |
} KEY_TYPE; | |
int checkData(char *buf, int messageLength) | |
{ | |
int isDataValid = 0; | |
int checksum = buf[messageLength]; | |
int calculation = 0; | |
for (int i = 0; i < messageLength; i++) { | |
calculation += buf[i]; | |
} | |
isDataValid = (calculation == checksum); | |
return isDataValid; | |
} | |
void combineAck(char *ackMessage, char *payload, int messageLength) | |
{ | |
ackMessage[0] = 0xA5; | |
ackMessage[1] = 0x5A; | |
ackMessage[2] = messageLength; | |
for (int i = 0; i < messageLength; i++) { | |
ackMessage[3 + i] = payload[i]; | |
} | |
ackMessage[4 + messageLength] = 0; | |
for (int j = 0; j < 4 + messageLength; j++) { | |
ackMessage[4 + messageLength] += ackMessage[j]; | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
int fd; | |
int charpos, payload_len; | |
char recvBuf[64]; | |
char sendBuf[64]; | |
char payLoad[32]; | |
char gchar = 0; | |
int mVoltage; | |
int mChargeStatus; | |
// enum KEY_TYPE mKeyType; | |
int mKeyType; | |
time_t now; | |
if (argc != 2) { | |
printf("Usage: sudo ./readall_mcu /dev/ttyS0\n"); | |
return -1; | |
} | |
if ((fd = serialOpen(argv[1], 115200)) < 0) { | |
printf("open serial error\n"); | |
} | |
while (1) { | |
int recv_count = serialDataAvail(fd); | |
//int recv_count = read(fd, recvBuf, 64); | |
if (recv_count > 0) { | |
for (int i = 0; i < recv_count; i++) { | |
// time(&now); | |
gchar = serialGetchar(fd); | |
printf("%02x ", gchar); | |
if (0x5A == gchar) { | |
charpos = 1; | |
} else if (0xA5 == gchar && charpos == 1) { | |
printf("\n[IPC] "); | |
charpos = 2; | |
} else if (charpos == 2) { | |
if (0x01 == gchar) { | |
printf("[KEY] "); | |
} else if (0x02 == gchar) { | |
printf("[BAT] "); | |
} else if (0x03 == gchar) { | |
printf("[CHG] "); | |
} else if (0x04 == gchar) { | |
printf("[VER] "); | |
} else if (0x05 == gchar) { | |
printf("[CUT] "); | |
} else { | |
printf("[UNKNOWN] "); | |
} | |
charpos = 3; | |
} else if (charpos == 3) { | |
payload_len = gchar; | |
charpos = 4; | |
} else if (charpos == 4) { | |
payload_len = | |
(gchar << 8) + payload_len; | |
printf("[LEN %d] ", payload_len); | |
charpos = 5; | |
} else if (charpos == 5) { | |
payload_len--; | |
printf("[%d] ", payload_len); | |
if (payload_len == 0) | |
charpos = 6; | |
} else if (charpos == 6) { | |
printf("[SUM]\n"); | |
charpos = 0; | |
} | |
} | |
serialFlush(fd); | |
} | |
} | |
serialClose(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment