Hallo Gemeinde,
auf meinem Linuxsystem versuche ich per C Programm eine Kommunikation mit RS232/V24 zu meinen AVR erreichen. Doch ich habe da ein Problem, dass ich eine Zeichen nicht senden kann. zbB.: 0x0C (12) und noch andere. Ich wollte in RAW mode auf die Schnittstelle schreiben.
Jetzt weis ich leider nicht wo mein Fehler ist ;-)
hier meine jetziger CODE
auf meinem Linuxsystem versuche ich per C Programm eine Kommunikation mit RS232/V24 zu meinen AVR erreichen. Doch ich habe da ein Problem, dass ich eine Zeichen nicht senden kann. zbB.: 0x0C (12) und noch andere. Ich wollte in RAW mode auf die Schnittstelle schreiben.
Jetzt weis ich leider nicht wo mein Fehler ist ;-)
hier meine jetziger CODE
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <string.h>
#include <stdbool.h>
int fd = 0;
int main(int argc, char** argv)
{
struct termios oldtios,options;
int currstat = 0;
if((argc < 2)||(argc > 3))
{
printf("Fehler: Ungültige Parameter-Anzahl.\n");
printf("Aufruf:\n");
printf(" %s <device>\n",argv[0]);
return 1;
}
/* Das Device öffnen */
if((fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
{
printf("Fehler: Device \"%s\" kann nicht geöffnet werden.\n", argv[1]);
return 2;
}
tcgetattr(fd,&oldtios);
fcntl(fd, F_SETFL, 0);
if (tcgetattr(fd, &options) != 0) return(-1);
cfsetispeed(&options, B115200); /* setze 115200 bps */
cfsetospeed(&options, B115200);
options.c_cflag &= ~PARENB; /* kein Paritybit */
options.c_cflag &= ~CSTOPB; /* 1 Stopbit */
options.c_cflag &= ~CSIZE; /* 8 Datenbits */
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= (CLOCAL | CREAD); /* CD-Signal ignorieren */
/* Kein Echo, keine Steuerzeichen, keine Interrupts */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | ECHOCTL);
options.c_oflag &= ~OPOST; /* setze "raw" Input */
options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_cc[VMIN] = 0; /* warten auf min. 0 Zeichen */
options.c_cc[VTIME] = 10; /* Timeout 10 Sekunden */
tcflush(fd,TCIOFLUSH);
if (tcsetattr(fd, TCSAFLUSH, &options) != 0) return(-1);
char buffer[8];
// Version
buffer[0]=0x0C; //kann ich nicht senden!
buffer[1]=0x00;
buffer[2]=0x04;
printf("-Eingabe---------------------\n");
int i;
for (i=0; i<3; i++ )
{
printf("0x%02x ",buffer[i]);
}
printf("\n");
printf("-----------------------------\n");
// buffer schreiben
i = write(fd,buffer,3); //ist das OK?
printf("geschrieben: %i\n",i);
printf("-----------------------------\n");
int bb = 0;
int x = 0;
bool cmdOK=false;
char cods[10];
do
{
//printf("\n");
if((bb = read( fd, &cods[x], sizeof( cods )-x )) > 0)
//if((bb = read( fd, &cods, 5 )) > 0)
{
for (i=x;i<x+bb;i++)
{
printf("0x%02x ",cods[i]);
if (cods[i]==0x04) cmdOK=true;
}
x+=bb;
printf("in if: x=%d\n",x);
}
}while(!cmdOK);
printf("-Ausgabe---------------------\n");
for (i=0; i<x; i++ )
{
printf("0x%02x ",cods[i]);
}
printf("\n");
printf("-----------------------------\n");
close(fd);
// TTYS0 auf alten Wert setzen
tcflush(fd,TCIOFLUSH);
if (tcsetattr(fd, TCSAFLUSH, &oldtios) != 0) return(-1);
return 0;
}