Created
February 18, 2024 00:44
-
-
Save bakueikozo/49c3564188b6b0e8bb7b0e5a26d52365 to your computer and use it in GitHub Desktop.
This file contains 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 <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <termios.h> | |
#include <time.h> | |
#define DEV_NAME "/dev/tty.usbserial-23410" | |
#define BAUD_RATE B9600 | |
#define BUFF_SIZE 4096 | |
void serial_init(int fd) { | |
struct termios tio; | |
memset(&tio, 0, sizeof(tio)); | |
tio.c_cflag = CS8 | CLOCAL | CREAD; | |
tio.c_cc[VTIME] = 100; | |
cfsetispeed(&tio, BAUD_RATE); | |
cfsetospeed(&tio, BAUD_RATE); | |
tcsetattr(fd, TCSANOW, &tio); | |
} | |
int sendRequest(int fd,int music){ | |
char buf[128]; | |
char ret; | |
int r; | |
char eot=0x03; | |
char enq=0x05; | |
char ack=0x06; | |
char nak=0x15; | |
sprintf(buf,"%cA1%06d",0x02,music); | |
printf("Music requqst cmd=%s\n",buf+1); | |
/* | |
write(fd,&enq,1); | |
do { | |
r=read(fd,&ret,1); | |
}while(r==0); | |
if( ret == 0x11 ){ | |
printf("ret dc1\n"); | |
}else{ | |
return -1; | |
}*/ | |
buf[strlen(buf)]=0x03; | |
write(fd,buf,10); | |
do { | |
r=read(fd,&ret,1); | |
}while(r==0); | |
printf(" ret ack or nak : 0x%0x\n",ret); | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
int fd; | |
int i,j; | |
int len; | |
unsigned char buffer[BUFF_SIZE], in_data[BUFF_SIZE]; | |
fd = open(DEV_NAME, O_RDWR | O_NONBLOCK ); | |
if(fd<0) { | |
exit(1); | |
} | |
serial_init(fd); | |
printf("start main loop...\n"); | |
j=0; | |
char packet[128]; | |
int packet_index=0; | |
int inPacket=0; | |
read(fd,buffer,BUFF_SIZE); | |
int cnt=0; | |
int status = 0; | |
while(1) { | |
len = read(fd, buffer, BUFF_SIZE); | |
if(len==0) { | |
continue; | |
} | |
if(len<0) { | |
printf("%s: ERROR\n", argv[0]); | |
perror(""); | |
exit(2); | |
} | |
// printf("%d bytes read.\n",len); | |
for( int n=0;n<len;n++){ | |
int c = buffer[n]; | |
if( c == 0x18 ){ | |
// CAN | |
printf("CAN"); | |
status=0; | |
} | |
if( c == 0x05 ){ | |
char out[24]; | |
if( status == 0 ){ | |
printf("enq"); | |
out[0]=0x11; | |
write(fd,out,1); | |
status=1; | |
}else{ | |
printf("Ignore enq"); | |
} | |
} | |
if( c== 0x06 ){ | |
printf("ack"); | |
status=0; | |
} | |
if( c == 0x15 ){ | |
printf("nak"); | |
status = 0; | |
} | |
if( c == 0x02 ){ | |
// STX | |
packet_index=0; | |
inPacket=1; | |
printf("stx"); | |
} | |
if( inPacket == 1 ){ | |
packet[packet_index]=c; | |
} | |
if( c == 0x03 ){ | |
printf("ETX RECV\n"); | |
printf("packet cmd=%c[0x%x]\n",packet[1],packet[1]); | |
for(int c=0;c<packet_index;c++){ | |
printf("packet[%d]=%x\n",c,packet[c]); | |
} | |
char out[24]; | |
out[0] = 0x06; | |
write(fd,out,1); | |
if( packet[1] == 'W' && packet[2] == 0x30 ){ | |
out[0] = 0x06; | |
out[1] = 0x02; | |
out[2] = 'W'; | |
out[3] = 'A'; | |
out[4] = 0x03; | |
write(fd,out+1,4); | |
} | |
if( packet[1] == 'W' && packet[2] == 0x33 ){ | |
out[0] = 0x06; | |
out[1] = 0x02; | |
out[2] = 'W'; | |
out[3] = 'C'; | |
out[4] = 0x03; | |
write(fd,out+1,4); | |
} | |
if( packet[1] == 0x5a ){ | |
out[1] = 0x02; | |
out[2] = 'W'; | |
out[3] = 'Z'; | |
out[4] = '2'; | |
out[5] = '7'; | |
out[6] = '0'; | |
out[7] = '5'; | |
out[8] = '4'; | |
out[9] = '9'; | |
out[10] = 3; | |
write(fd,out+1,10); | |
// return 0; | |
} | |
inPacket=0; | |
cnt++; | |
break; | |
}else{ | |
packet_index++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment