Code:
// Compile GCC -o serial_wiring -l rt serial_wiring6.c -lwiringPi
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
//WiringPi include für SEN/STA Erkennung
#include <wiringPi.h>
//Konfig RASPI USART
#define BAUDRATE B9600 // 9600 Baud definieren für 8E1 I-Bus
char MODEMDEVICE[]= "/dev/ttyAMA0"; // Schnittstelle wählen
//int fd; // File descriptor
char eingang[255]="";
char d[1]="";
int anzahl=0;
int laenge=0;
int logg=0;
int fd; // File descriptor
int sender;
int lautst=95;
long int time_back;
long int time_ok;
struct termios newtio={};
int bytes_avaiable;
int rx_buffer_cnt = 0;
#include <sys/ioctl.h> //IO
unsigned char initmpd() //Schnittstelle parametrieren und öffnen
{
char befehl[30]="";
char s[2]="";
lautst=95;
system("mpc play"); //InitScript für mpd
sleep(1);
system("mpc play > /tmp/current"); // ID-Tags von aktuellem Titel ablegen
sleep(1);
system("mpc lsplaylists > /tmp/lsplaylists"); //Playlist zwischenspeichern
sleep(1);
FILE *f;
char Text[300]="";
char Text1[70]="";
char Text2[7]="volume";
f = fopen("/tmp/lsplaylists","r");
anzahl=0; //Anzahl Einträge der Senderliste zählen
if(f!=NULL){
fgets(Text, sizeof(Text), f);
if(strlen(Text)<2){
fclose(f);
return;
}else{
anzahl=1;
while( fgets(Text, sizeof(Text), f) !=0 ){
if(strlen(Text)>2){
anzahl++;
}
if(anzahl>199){ //Nicht mehr wie 200
break;
}
}
}
fclose(f);
}
}
int init() //Schnittstelle parametrieren und öffnen für I-Bus 9600 8E1
{
//O_RDONLY, O_WRONLY or O_RDWR -
//O_NDELAY (geht weiter, wenn keine Daten da sind und gibt "-1" zurueck)
// man 2 open fuer mehr Infos - see "man 2 open" for more info
// O_NOCTTY No ControllTeleType
fd = open(MODEMDEVICE, O_RDONLY | O_NOCTTY);
if (fd < 0){
printf("Fehler beim oeffnen von %s\n", MODEMDEVICE);
exit(-1);
}
memset(&newtio, 0, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD | PARENB; //setzt Einstellungen vom UART auf 8E1
newtio.c_iflag = IGNPAR | IGNCR | IXON ;
newtio.c_oflag = 0;
newtio.c_lflag = 0;//ICANON; /* set input mode (non-canonical, no echo, ...) */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 chars received old 1 */
newtio.c_cc[VSTART] = 0; /* Ctrl-q startet Datenübertragung */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s stoppt Datenübertragung*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
return fd;
}
int main(int argc, char** argv) //Programmstart
{
//new
int pin = 7; //GPIO 4
int value = 0; // Deklaration Eingangswert GPIO4
printf ("Control Raspberry Pi via BMW Headunit E46 wiringPi\n") ;
if (wiringPiSetup () == -1)
exit (1) ;
//Fehlerrückgabe wiringPi
pinMode(pin,INPUT); //wiringPi GPIO4 Pin7 deklarieren
//char c;
init(); //Schnittstelle UART parametrieren und öffnen
initmpd(); //MPC starten
sleep(3); //nix tun
while (1)
{
//value = digitalRead (pin);
//printf("Status SEN/STA %d \n", bytes_avaiable );
//printf("SEN/STA: %d\n", value); // BUSRUHE?
//printf("RX_Länge %d",rx_length);
// Filedescriptor für BMW Radiotasten
if (fd != -1)
{
unsigned char rx_buffer[9]; //8 Bytes aus UART Puffer holen
//unsigned char i_bus_buffer[25]; // String zum zusammenführen von 4 rx_buffer
//i_bus_buffer Nullen
rx_buffer_cnt = 0;
for (rx_buffer_cnt; rx_buffer_cnt < 9; rx_buffer_cnt++)
{
rx_buffer[rx_buffer_cnt] = 0;
}
int rx_length = read(fd, (void*)rx_buffer, 8); //Filestream, buffer to store in, number of bytes to read (max)
if (rx_length < 0)
{
//An error occured
printf("UART RX error\n %i", rx_length);
}
else if (rx_length == 0)
{
//No data waiting
//I-Bus Puffer Nullen
rx_buffer_cnt = 0;
for (rx_buffer_cnt; rx_buffer_cnt < 9; rx_buffer_cnt++)
{
rx_buffer[rx_buffer_cnt] = 0;
}
}
else
{
//Bytes empfangen --> Darstellung
// int i = 0; // Zähler für Stringverknüpfer
// int j = 0; // + 8 nehmen um rx_buffer nach inten zu kriegen
// for(i=0; i<1; i++){
// i_bus_buffer [0 + j] = rx_buffer [0];
// i_bus_buffer [1 + j] = rx_buffer [1];
// i_bus_buffer [2 + j] = rx_buffer [2];
// i_bus_buffer [3 + j] = rx_buffer [3];
// i_bus_buffer [4 + j] = rx_buffer [4];
// i_bus_buffer [5 + j] = rx_buffer [5];
// i_bus_buffer [6 + j] = rx_buffer [6];
// i_bus_buffer [7 + j] = rx_buffer [7];
// j = j + 8; // für nächste Runde um 8 verschieben
//mach nix
//
//}
// Integer Arry mit 8 Charaktären
unsigned int rx_buffer_int [9];
rx_buffer_int [0] = (int)rx_buffer [0];
rx_buffer_int [1] = (int)rx_buffer [1];
rx_buffer_int [2] = (int)rx_buffer [2];
rx_buffer_int [3] = (int)rx_buffer [3];
rx_buffer_int [4] = (int)rx_buffer [4];
rx_buffer_int [5] = (int)rx_buffer [5];
rx_buffer_int [6] = (int)rx_buffer [6];
rx_buffer_int [7] = (int)rx_buffer [7];
rx_buffer_int [8] = (int)rx_buffer [8];
if (rx_buffer_int[0] == 104 && rx_buffer_int[1] == 5 && rx_buffer_int[2] == 24 && rx_buffer_int[3] == 56 && rx_buffer_int[4] == 10 && rx_buffer_int[5] == 0 && rx_buffer_int[6] == 71)
{
//An error occured
system("mpc next");
}
if (rx_buffer_int[0] == 104 && rx_buffer_int[1] == 5 && rx_buffer_int[2] == 24 && rx_buffer_int[3] == 56 && rx_buffer_int[4] == 10 && rx_buffer_int[5] == 1 && rx_buffer_int[6] == 70)
{
//An error occured
system("mpc prev");
}
//char dest='a';
//int c=(int)dest;
printf("%i bytes read : %X %X %X %X %X %X %X %X %X \n", rx_length,rx_buffer[0],rx_buffer[1],rx_buffer[2],rx_buffer[3],rx_buffer[4],rx_buffer[5],rx_buffer[6],rx_buffer[7],rx_buffer[8]);
printf("%i bytes read : %i %i %i %i %i %i %i %i %i \n", rx_length,rx_buffer_int[0],rx_buffer_int[1],rx_buffer_int[2],rx_buffer_int[3],rx_buffer_int[4],rx_buffer_int[5],rx_buffer_int[6],rx_buffer_int[7],rx_buffer_int[8]);
//printf("%i bytes read : %s\n", rx_length, rx_buffer);
//I-Bus Puffer Nullen
rx_buffer_cnt = 0;
for (rx_buffer_cnt; rx_buffer_cnt < 9; rx_buffer_cnt++)
{
rx_buffer[rx_buffer_cnt] = 0;
}
//printf("%i bytes read : %X %X %X %X %X %X %X %X %X %X \n", rx_length, rx_buffer[0],rx_buffer[1],rx_buffer[2],rx_buffer[3],rx_buffer[4],rx_buffer[5],rx_buffer[6],rx_buffer[7],rx_buffer[8],rx_buffer[9]);
}
}
}
close (fd);
return 0;
}
Und zum Schluss mein
Lesezeichen