C++ unter QNX

Von: , Frage gestellt am Di, 13. Nov 2007

Hallo zusammen,

ich muss ein Bluetoothadapter über serielle schnittstelle unter QNX anspreschen,der Bluetoothadapter soll auf AT-Befehle reagieren,alles fonktioniert soweit,aber das problem liegt bei mir darin an der read() Funktion,bekomme ich eine komische zahl als Rückgabe nbytes = -14252556!!
hat jemand eine Idee warum das nicht funktioniert !!


#include <stdio.h> /* Standard input/output definitions */
#include <hw/inout.h> //OUT8...
#include <sys/neutrino.h> //THREAD
#include <fcntl.h> /* File control definitions */
#include <cstring>
#include <string.h> /* String function definitions */
#include <fstream>
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <stdlib.h>
#include <hw/8250.h>
#include <sys/syspage.h>
#include <errno.h> /* Error number definitions */
#include <sys/debug.h>
#include <sys/elf.h>
#include <sys/procfs.h>
#include <unistd.h> /* UNIX standard function definitions */
#include <cstdlib>
#include <iostream>
#include <inttypes.h> //need for uintptr_t type
#include <sys/uio.h>
#include <string>
#include <common/String.h>


struct termios options;

void open_port(int i,int BAUDRATE,char senbuffer[200]);

int main(int argc, char *argv[]) {

int cPort= 0;
int BAUDRATE =0 ;
char senbuffer[200];
String b("B");




std::cout << "Welcome to the Momentics IDE\n" << std::endl;
std::cout << "Bitte geben sie eine BAUDRATE ein\n"<<
std::endl;

std::cin >> BAUDRATE;
b + BAUDRATE;
std::cout << "EINGEGEBENE BAUDRATE: " << b <<std::endl;



std::cout << "Bitte waehlen ein Terminalgerat aus \n"<<
std::endl;

std::cout << "[1] steht fuer /dev/ser1 \n" << std::endl;

std::cout << "[2] steht fuer /dev/ser2 \n" << std::endl;

std::cout << "[0] steht fuer EXIT \n" << std::endl;


std::cin >> cPort;
std::cout << std::endl;



while(1)
{


std::cout << "Bitte geben Sie ein AT-Befehl ein \n"<<

std::endl;

std::cin>>senbuffer;

strcat(senbuffer,"\n");

std::cout << "EINGEGEBENER AT-Befehl: " << senbuffer

<<std::endl;


if (cPort == 'c')
return 0;

switch (cPort)


{
case 1:
std::cout << "es wurde ausgewählt: /dev/ser1 "<< std::endl;
std::cout << std::endl;
open_port(cPort,BAUDRATE,senbuffer);
break;
case 2:
std::cout << "es wurde ausgewählt: /dev/ser2 "<< std::endl;
std::cout << std::endl;
open_port(cPort,BAUDRATE,senbuffer);
break;

case 0:
return 0;

default:
std::cout << "Falsche Eingabe - bitte wiederholen Sie Ihre
Auswahl!" <<std:: endl;
break;
}




}
}
void open_port(int i,int BAUDRATE,char senbuffer[200]) {


int fd; /* File descriptor for the port */
int n ;
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */

// char senbuffer[50]; /* send buffer */
char recbuffer[200]; /*reciev buffer*/
unsigned char cmd;



// std::cout << i<< std::endl;

if (i=1) fd = open("/dev/ser1", O_RDWR | O_NOCTTY |
O_NDELAY);

else if(i=2) fd = open("/dev/ser2", O_RDWR | O_NOCTTY |
O_NDELAY);

else printf("failled to open\n");



// std::cout << fd<< std::endl;


//// The O_NOCTTY flag tells UNIX that this program doesn't want
to be the
//// "controlling terminal" for that port. If you don't
//// specify this
//// then any input (such as keyboard abort signals and so
forth)
//// will affect your process. Programs like getty(1M/8) use
this feature
//// when starting the login process, but normally a user
program does not want this behavior.
////
//// The O_NDELAY flag tells UNIX that this program doesn't
//// care what state the DCD signal line is in - whether the
other end of the port is up and running.
//// If you do not specify this flag, your process will be put
to sleep until
//// the DCD signal line is the space voltage
// fcntl(fd, F_SETFL, 0);
if (fd == -1)
{
/*
* Could not open the port.
*/

perror("open_port: Unable to open /dev/ser1 - ");
}
else{

printf("open /dev/ser\n");


}


/* RS232 konfigurieren */
if (tcgetattr(fd, &options) != 0)
{
perror("terminal: tcgetattr() failed");
return;
}


/*
* Set the new options for the port...
*/

cfsetispeed(&options, BAUDRATE);
cfsetospeed(&options, BAUDRATE);




tcflow(fd, TCOOFFHW );
// Benutzung von Hardware Flow Control für
die Aussetzung der Ausgabe auf dem Gerät in Verbindung mit fd

tcflow(fd,TCOONHW);
// Benutzung von Hardware Flow Control, um
die Ausgabe auf das Gerät in Verbindung mit fd.

tcflow(fd,TCIOFFHW);
tcflow(fd,TCIONHW );

//Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD); //Read
und Local setzen


//keine Parität, 1 Start&Stopbit, 8 Datenbits

options.c_cflag &= ~PARENB;
/* disable parity */

options.c_cflag &= ~PARODD;
/* Turn odd off => even */

options.c_cflag &= ~CSTOPB;
/* Set 1 Stopbit */

options.c_cflag &= ~CSIZE;
/* Mask the character size bits */


//keine Paritaet
options.c_cflag |= CS8;
/* Select 8 data bits */



//Disable hardware flowcontrol
// options.c_cflag |= CRTSCTS;




//options.c_cflag |= CNEW_RTSCTS; /* Also
called CRTSCTS */


//Raw Data Ausgabe
options.c_oflag &= ~OPOST;
options.c_oflag |= ONLCR;





//Raw data Einlesen mit Parity-check
// options.c_lflag &= ~(ICANON | ECHO | ECHOE |
ISIG);

// options.c_lflag &= ~(ICANON |ISIG);
// options.c_iflag |= (INPCK | ISTRIP);

//cfmakeraw (&options); // real raw!

//Software flowcontrol ausschalten
options.c_iflag |= (IXON | IXOFF | IXANY);

//Neue Einstellungen sofort auf Port übertagen
tcsetattr(fd, TCSAFLUSH, &options);


if (tcsetattr(fd, TCSAFLUSH, &options) != 0)
{
perror("terminal: tcsetattr() failed");
}

printf("Einstellungen wurden gespeichert\n");

//Puffer leeren
tcflush(fd, TCIFLUSH);






flushall();

n = write(fd, senbuffer, strlen(senbuffer)); //Writing
data to the port


tcdrain(fd); //warten, bis alles gesendet.


std::cout << "Es wurde geschickt AT-Befehl: " <<
senbuffer<<std::endl;

std::cout << "AT-Befehl Größe: " <<
strlen(senbuffer)<<std::endl;

if (n < 0){

fputs("write() of AT failed!\n", stderr);

}
else{

printf("es wurde geschrieben\n");



}



printf("es wird gelesen\n");



nbytes=read(fd,recbuffer,strlen(recbuffer));


if(nbytes < 0){

fputs("read() failed!\n", stderr);

}else
{
std::cout << "Wert grosser " << nbytes<<std::endl;
}




std::cout << "Es wurde gelesen: recbuffer " <<
recbuffer<<std::endl;




std::cout << "Es wurde so viel Bytes " << nbytes<<std::endl;
std::cout << "Es wurde aufjedenfall gelesen: cmd " <<
cmd<<std::endl;



tcsetattr(fd,TCSANOW,&options);







close(fd);
}


Was habe ich hier falsch gemacht !!




vielen Dank

1 Antworten zu dieser Frage

  1. Antwort von nach 7 Tagen 0 hilfreich
    Re: C++ unter QNX

    problem liegt bei mir darin an der read() Funktion,bekomme ich
    eine komische zahl als Rückgabe nbytes = -14252556!!
    char recbuffer[200]; /*reciev buffer*/
    nbytes=read(fd,recbuffer,strlen(recbuffer));
    Das ist eine äußerst schlampige Programmierung! Welchen Wert hat denn strlen(recbuffer)? Offensichtlich hängt der von der Initialisiertung von recbuffer ab. Und diese gibt es nicht. recbuffer[200] ist mit den zufälligen Werten initialisiert, die man auf dem Stack an dieser Stelle findet.

    Wahrscheinlich wurde nbytes (und anderes) auf Grund eines willkürlichen Wertes von strlen(recbuffer) überschrieben.

    Wenn read so

    size_t read(int fildes,void *buf,size_t nbyte);

    deklariert ist, dann sollte das dritte Argument die Zahl der Bytes, die man empfangen kann und möchte, darstellen, also z.B.

    nbytes=read(fd,recbuffer,sizeof(recbuffer));

Keine passende Antwort gefunden? Jetzt eigene Frage stellen!