Kompiliavimo eilutė
gcc -o test3.o -lmysqlclient -I/usr/local/include -L/usr/local/lib -lwiringPi test2.c
Programos kodas:
/*****Configures an Raspberry Pi as an SPI master and demonstrates bidirectional communication with an***/ #include <mysql/mysql.h> //used by mysql #include //used by mysql #include #include <sys/ioctl.h> #include <linux/spi/spidev.h> #include #include #include //used by string //#include /*******Declare Global Variables*****************************/ unsigned short int spiTxRx(char txDat); int fd; unsigned char AR_command[]={0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //AR_command antras baitas nurodo siunciamos komandos ilgi be pirmo ir antro baitu jis uzpildomas siunciant seka unsigned char AR_Komanda[512]; unsigned int AR_Komanda_Size; unsigned char AR_command1[] = {0x20, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12}; unsigned char spi_bitsPerWord; unsigned char spi_mode; struct connection_details{char *server;char *user;char *password;char *database;}; MYSQL* mysql_connection_setup(struct connection_details mysql_details){// first of all create a mysql instance and initialize the variables within MYSQL *connection = mysql_init(NULL); // connect to the database with the details attached. if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) { printf("Conection error : %s\n", mysql_error(connection));exit(1); } return connection; } MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query){ if (mysql_query(connection, sql_query)) {// send the query to the database printf("MySQL query error : %s\n", mysql_error(connection)); exit(1); } return mysql_use_result(connection); } struct connection_details mysqlD; MYSQL *conn; // the connection MYSQL_RES *res; // the results MYSQL_ROW row; // the results row (line by line) /********************************************************** Main Setup SPI Open file spidev0.0 (chip enable 0) for read/write access with the file descriptor "fd" Configure transfer speed(1MHz) ***********************************************************/ unsigned short int resu[512]; int main (void){ mysqlD.server = "localhost"; // where the mysql database is mysqlD.user = "vejojegaines"; // the root user of mysql mysqlD.password = "vejojegaines"; // the password of the root user in mysql mysqlD.database = "vejojegaines"; // the databse to pick conn = mysql_connection_setup(mysqlD); // connect to the mysql database unsigned char result[20]; unsigned char buffH[1]; unsigned char buffL[1]; fd = open("/dev/spidev0.0", O_RDWR); unsigned int speed =100000; ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); unsigned short int i; unsigned short int j=0; unsigned short int jmax=30; unsigned short int TS=100; unsigned short int TS_Prev=100; unsigned short int *TSa2Point; unsigned char qu[255]; char *lai; unsigned int retx; unsigned int iii; unsigned int iiii; unsigned short int DisableWork=0; unsigned short int DisableWorkPrevious=0; unsigned char Komanda; void KomandosVykdymas(); void LokalusDarbas(); unsigned int id; char *pChar; while (1) { res = mysql_perform_query(conn, "select id, Komanda, Atlikta from komandos where Atlikta=0 order by ModData asc limit 1"); while ((row = mysql_fetch_row(res))!=NULL) { for (iiii=0; iiii<strlen(row[1]); iiii=iiii+2) { AR_Komanda[iiii/2]=hex_char_to_bin(row[1][iiii])*16+hex_char_to_bin(row[1][iiii+1]); } AR_Komanda_Size=strlen(row[1])/2; if (atoi(row[2])==0) {DisableWork=1;} else {DisableWork=0;} if (DisableWork!=DisableWorkPrevious) { //printf("Darbo rezimas: %d\n", DisableWork); DisableWorkPrevious=DisableWork; } id=atoi(row[0]); } mysql_free_result(res); if (DisableWork==1) { KomandosVykdymas(); sprintf(qu, "update komandos set Atlikta=1 where id=%d", id); res = mysql_perform_query(conn, qu); mysql_free_result(res); DisableWork=0; } else { //LokalusDarbas(); } LokalusDarbas(); //testavimui usleep(1000000);//sekundes uzdelsimas tarp duomenu uzklausu } exit(1); } void KomandosVykdymas() { unsigned int i, pakartojimas; printf("(ilgis=%d) vykdymas\n", AR_Komanda_Size); SR_packet(AR_Komanda, AR_Komanda_Size); while (SR_packet(AR_Komanda, AR_Komanda_Size)==0){ for (i=0; i<AR_Komanda_Size; i++) {printf("%02X ", AR_Komanda[i]);} printf("Kartojimas Komandos (%d baitu ilgis) vykdymas (%u kartas)\n", AR_Komanda_Size, pakartojimas); if (pakartojimas++>100) { printf("Pasiektas limitas kartojimui: %d. Vykdymas nesekmingas\n"); break; } } } void LokalusDarbas() { unsigned int A0, A1, A2, A3, A4, A5, TS1, TS2, VGRA, VGRB, GenHzA, GenHzB; unsigned int i, pakartojimas; unsigned char qu[255]; while (SR_packet(AR_command, sizeof(AR_command))==0){ for (i=0; i<sizeof(AR_command); i++) {printf("%02X ", AR_command[i]);} SR_packet(AR_command, sizeof(AR_command)); printf("Kartojimas Komandos (%d baitu ilgis) vykdymas (%u kartas)\n", sizeof(AR_command), pakartojimas); if (pakartojimas++>100) { printf("Pasiektas limitas kartojimui (LokalusDarbas): %d. Vykdymas nesekmingas\n"); return ; break; } } printf("Uzklausa(lokali):"); for (i=0; i<sizeof(AR_command); i++) {printf("%02X ", AR_command[i]);} printf("Rezultatas:"); for (i=0; i<sizeof(AR_command); i++) {printf("%02X ", resu[i]);} printf("\n "); A0=resu[3] + resu[4]*256; A1=resu[5] + resu[6]*256; A2=resu[7] + resu[8]*256; A3=resu[9] + resu[10]*256; A4=resu[11] + resu[12]*256; A5=resu[13] + resu[14]*256; VGRA=resu[15] + resu[16]*256; VGRB=resu[17] + resu[18]*256; GenHzA=resu[19] + resu[20]*256; GenHzB=resu[21] + resu[22]*256; TS1=resu[23] + resu[24]*256; TS2=resu[25] + resu[26]*256; //TSa2Point=&TSa2[0]; //TS=AtmestiKrastinesReikmes(TSa2Point, jmax); //int A0min=0xFFFF;int A0max=0;for (j=0; j<jmax; j++) {if (A0a[j]>A0max) A0max=A0a[j];if (A0a[j]<A0min) A0min=A0a[j];} //int A1min=0xFFFF;int A1max=0;for (j=0; j<jmax; j++) {if (A1a[j]>A1max) A1max=A1a[j];if (A1a[j]<A1min) A1min=A1a[j];} //int A2min=0xFFFF;int A2max=0;for (j=0; j<jmax; j++) {if (A2a[j]>A2max) A2max=A2a[j];if (A2a[j]<A2min) A2min=A2a[j];} //int A3min=0xFFFF;int A3max=0;for (j=0; j<jmax; j++) {if (A3a[j]>A3max) A3max=A3a[j];if (A3a[j]<A3min) A3min=A3a[j];} //int A4min=0xFFFF;int A4max=0;for (j=0; j<jmax; j++) {if (A4a[j]>A4max) A4max=A4a[j];if (A4a[j]<A4min) A4min=A4a[j];} //int A5min=0xFFFF;int A5max=0;for (j=0; j<jmax; j++) {if (A5a[j]>A5max) A5max=A5a[j];if (A5a[j]<A5min) A5min=A5a[j];} sprintf(qu, "insert into vejojegaines.spi values (\"\",%i,%i,%i,%i,%i,%i,%i,%i,%i,null)",A0,A1,A2,A3,A4,A5, VGRA+VGRB*256*256, GenHzA+GenHzB*256*256, TS1+TS2*256*256); //printf("qu=%s\n", qu); res=mysql_perform_query(conn, qu); // assign the results return to the MYSQL_RES pointer mysql_free_result(res); //clean up the database result set } int SR_packet(char AR[], int KKiek) { unsigned short int cs, css, i; cs=spiTxRx(0x81);//pradinis sekos baitas cs=0;//pirmas baitas dummy - jis neapskaitomas issiunciamojoje kontrolineje sumoje css=0x81; //pirmas baitas ir pradeda kontrolin▒ siun▒iamos bait▒ sekos sum▒ for (i=0; i<KKiek; i++) { if (i==1) {AR[i]=KKiek-2;}//siunciamu baitu kiekis irasomas i seka, kad priimantis irenginys zinotu ilgi resu[i] = spiTxRx(AR[i]); cs+=resu[i];if(cs>255) {cs=cs-256;} css+=AR[i];if(css>255) {css=css-256;} } resu[i]=spiTxRx(255-css);//kontrolin▒ siunciam▒ bait▒ sekos suma cs+=resu[i++];if(cs>255) {cs=cs-256;} resu[i]=spiTxRx(0x00);//finalinis baitas 'dymmy' skirtas nuskaityti paskutin▒ gaunam▒ bait▒ cs+=resu[i];if(cs>255) {cs=cs-256;} if (cs!=0xff){ printf("Bloga kontroline suma (checksum send) sc=%x\n", cs);return 0; } else { return 1; } } /********************************************************** spiTxRx Transmits one byte via the SPI device, and returns one byte as the result. Establishes a data structure, spi_ioc_transfer as defined by spidev.h and loads the various members to pass the data and configuration parameters to the SPI device via IOCTL Local variables txDat and rxDat are defined and passed by reference. ***********************************************************/ unsigned short int spiTxRx(char txDat){ unsigned char rxDat; struct spi_ioc_transfer spi; memset (&spi, 0, sizeof (spi)); spi.tx_buf = (unsigned long)&txDat; spi.rx_buf = (unsigned long)&rxDat; spi.len = 1; ioctl (fd, SPI_IOC_MESSAGE(1), &spi); return rxDat; } void ToMySQL(char qu[]) { res=mysql_perform_query(conn, qu); // assign the results return to the MYSQL_RES pointer mysql_free_result(res); //clean up the database result set } int AtmestiKrastinesReikmes(unsigned short int *ar_ptr, int jmax) { unsigned short int i; unsigned short int max=0; unsigned short int min=1024; unsigned int res=0; unsigned long int res1=55555555; unsigned short int ct=0; for (i=0;i<jmax; i++) { if (*(ar_ptr+i)>max) max=*(ar_ptr+i); if (*(ar_ptr+i)<min) min=*(ar_ptr+i); //printf("%hu,", *(ar_ptr+i)); } for (i=0;i<jmax; i++) { if (*(ar_ptr+i)!=max && *(ar_ptr+i)!=min) { res+=*(ar_ptr+i); ct++; } } if (ct==0) { //printf(".max:%hu, min: %hu, ct: %hu, res1: %li,--------\n", max, min, ct, max); return max; } else { res1=(double)res/(double)ct; res=res1; //printf(".max:%hu, min: %hu, ct: %hu, res1: %li,--------\n", max, min, ct, res); return res; } } int hex_char_to_bin(unsigned char *c){// TODO handle default / error switch(toupper(c)) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'A': return 10; case 'B': return 11; case 'C': return 12; case 'D': return 13; case 'E': return 14; case 'F': return 15; } }