PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : RS232- Uebermittlung eines laengeren Strings in c++



nomad
18-04-2010, 14:26
hi,
hab folgendes problem.
ich muss von einem microcontroller aus datenwerte an meinen linux-laptop
uebermitteln:
der microcontroller-code ist OK

Der String (1)sieht so aus:



Serial.str(string("$"))
Serial.dec(111)
Serial.str(string(":"))
Serial.dec(222)
Serial.str(string(":"))
Serial.dec(333)
Serial.str(string(":"))
Serial.tx(13)


diese uebermittlung ist ok, kein problem
sobald aber string(2) groesser wird also



Serial.str(string("$"))
Serial.dec(111)
Serial.str(string(":"))
Serial.dec(222)
Serial.str(string(":"))
Serial.dec(333)
Serial.str(string(":"))
Serial.dec(444)
Serial.str(string(":"))
Serial.dec(555)
Serial.str(string(":"))
Serial.dec(666)
Serial.str(string(":"))
Serial.dec(777)
Serial.str(string(":"))
Serial.dec(888)
Serial.tx(13)


wird nur der nur der inhalt von string (1) erkannt.
auch nach nomaligen compile keine aenderung
das prog vom microcontroller wurde auch neu kompiliert

ich habe die vermutung dass folgende function ev. etwas damit
zu tun hat:


res = read(fr_prop,buf,255);


eine vergroesserung des wertes brachte im augenblick nichts ????

der consolen output sieht so aus:


Command: 1
SendData: 1
S_Len: 3
SendDataString: 1
Communications: Sender: 1
Sender is closed
Now in READER
Now Read
In[0]: $111:222:333
Res: 12
Input: $111:222:333
Commr: 12
OK
Reader is Closed


ich weiss ehrlich gesagt nicht wie ich zu den 8 dummyWerten komm.

fuer hinweise, tips und hilfe waere ich sehr dankbar
gruss nomad



//=== code ================================================== ====//
// serialPropDevBoard12.cpp
// compile with
// USB-SerialCom:
// g++ serialPropDevBoard12.cpp -o serialPropDevBoard12
// run : ./serialPropDevBoard12

/************************************************** *************************************************/
#include<iostream>
#include <math.h>
// C-Stuff for RS232
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <termios.h>
#include <unistd.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <errno.h>
#include "curses.h"
#include <ctype.h>
#include <sys/io.h>

#include <sys/stat.h>
#include <string.h>
#include <sstream>

using namespace std;

#define _POSIX_SOURCE 1 // POSIX compliant source
// new for communications
#define BAUDRATE_PROP B9600 // baudrate settings
#define MODEMDEVICE_PROP "/dev/ttyUSB0"

#define MAXTOKLEN 255 // for tokenizer
#define BUFLEN 255
#define S_MAXTOKLEN 8

struct termios oldtio,newtio, options;

int fw_prop; // w=write
int fr_prop; // r=read
int res;
int rescommand;
int s_len;

char combuf[255];
char buffer[BUFLEN];
char buf[255];
char s_buffer[BUFLEN];

int inputs;

// Muss so sein, sonst fehler bei system(init_1);
// in main
const char *init_1 = "chmod a+rw /dev/ttyUSB0";

// sendCommands to mc
// noch offen
char *sendDataStr;
char *recDataStr;

char sendData[12]; // for umwandlung int to string with sprintf
const char *sendEOF = ("\r");

//char buffer [50]; // example bei sprintf
int n;
int exits;
int antw; // for output
int i; // counter

bool bool_Done;
bool bool_ReaderDone;
bool bool_SenderDone;
bool bool_Init;

// new sequences
int state;
int oldState;
int remote_cnt;
int stop_cnt;
int left_cnt;
int right_cnt;
int cnt_1;
int cnt;
int comDoneInt = 5;

bool comDone;

/************************************************** *************************
* Don't forget to give the appropriate serial ports the right permissions *
* (e. g.: chmod a+rw /dev/ttyS0)! *
* baudrate settings are defined in <asm/termbits.h>, which is *
* included by <termios.h> == system(init_1); *
************************************************** **************************
/*** from conio.h ************************************************** *******/

void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); fflush(stdout); }
void clrscr(void) { printf("\033[H\033[J"); fflush(stdout); }

// delay (nnn)
void delay(long millisek)
{
int mikrosek = millisek*1000;
struct timeval timeout;

timeout.tv_sec = mikrosek / 1000000L;
timeout.tv_usec = mikrosek % 1000000L;
select(0, NULL, NULL, NULL, &timeout);
}

/************************************************** *************************************************/
int Reader()
{
int x;
printf("Now in READER\n");

fr_prop = open(MODEMDEVICE_PROP, O_RDONLY | O_NOCTTY );
if(fr_prop <0)
{
perror(MODEMDEVICE_PROP);
printf("cannot open ttyUBS0");
exit(-1);
}

tcgetattr(fr_prop,&oldtio); // save current serial port settings
bzero(&newtio,sizeof(options)); // clear struct for new port settings

fcntl(fr_prop, F_SETFL,0);

if(tcgetattr(fr_prop, &options) != 0) return(-1);

cfsetspeed(&options,B9600);
/*setze Optionen */

options.c_cflag &= ~PARENB; // NO PARITYBIT
options.c_cflag &= ~CSTOPB; // 1 STOPPBIT
options.c_cflag &= ~CSIZE; // 8 DATENBIT
options.c_cflag |= CS8;
options.c_cflag |= (CLOCAL | CREAD); // CD-SIGNAL IGNORE

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; // SETZE RAW-INPUT

options.c_cc[VTIME] = 10; /* TIMEOUT 10 SEKUNDEN */
options.c_cc[VMIN] = 0; /* wait auf min. 0 zeichen old blocking read until
/*1character arrives */

// now clean the modem line and activate the settings for the port
tcflush(fr_prop, TCIFLUSH);

if(tcsetattr(fr_prop,TCSAFLUSH, &options) != 0) return (-1);

printf("Now Read\n");

comDone = false;

while( !comDone )
{
res = read(fr_prop,buf,255);
buf[res]=0; /* set end of string, so we can printf */

printf("In[%d]: %s\n",i,buf); // printf(":%s:%d\n", buf, res);
printf("Res: %d\n",res);

rescommand = res;

x++;
printf("Input: %s\n",buf);
printf("Commr: %d\n",rescommand);

//inputs = atoi(buf);
//printf("Inputs: %d\n",inputs);

comDoneInt = 5;

if(comDoneInt == inputs)
{
//comDone = true;
//gotoxy(16,20);
printf(" OK \n");
//antw = 0;
break;
}
}

/* restore the old port settings */
tcsetattr(fr_prop,TCSANOW,&oldtio);

close(fr_prop);
printf("Reader is Closed\n");

}

/************************************************** *************************************************/
int Sender()
{
printf("Communications: Sender: %s\n",sendDataStr);

fw_prop = open(MODEMDEVICE_PROP, O_WRONLY | O_NOCTTY );
if(fw_prop < 0)
{
perror(MODEMDEVICE_PROP);
printf("cannot open ttyUSB0");
exit(-1);
}

tcgetattr(fw_prop,&oldtio); // save current serial port settings
bzero(&newtio,sizeof(options)); // clear struct for new port settings

fcntl(fw_prop, F_SETFL,0);
if(tcgetattr(fw_prop, &options) != 0) return(-1);
cfsetspeed(&options,B9600);
/*setze Optionen */
options.c_cflag &= ~PARENB; // NO PARITYBIT
options.c_cflag &= ~CSTOPB; // 1 STOPPBIT
options.c_cflag &= ~CSIZE; // 8 DATENBIT
options.c_cflag |= CS8;
options.c_cflag |= (CLOCAL | CREAD); // CD-SIGNAL IGNORE

// kein echo, keine steuerzeichen, keine interrupts
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; // SETZE RAW-INPUT

options.c_cc[VTIME] = 10; /* TIMEOUT 10 SEKUNDEN */
options.c_cc[VMIN] = 0; /* wait auf min. 0 zeichen old blocking read until 1


// now clean the modem line and activate the settings for the port
tcflush(fw_prop, TCIFLUSH);

if(tcsetattr(fw_prop,TCSAFLUSH, &options) != 0) return (-1);

// here new write to BS2-BOE in c
write(fw_prop,sendDataStr,s_len); // len: old 2

/* restore the old port settings */

tcsetattr(fw_prop,TCSANOW,&oldtio);
close(fw_prop);

printf("Sender is closed\n");
bool_SenderDone = false;

} // end Sender

/************************************************** *************************************************/
void SendData()
{
Sender();
Reader();
}
/*** servoController ************************************************** ****************************/
int servoControl()
{
// Starting communications
clrscr();
gotoxy(1,1);
printf("SerialPropDevBoard (SerialCom) Version 0.12");
gotoxy(1,2);
printf("Sequences: ");
gotoxy(1,3);
printf("<0> - LinuxSystem Exit, <1> Want DummyDatas");
gotoxy(1,6);
printf("Commands: ");

// old do-while serialstuff
while( !comDone )
{
gotoxy(12,2);
scanf("%d",&antw);
gotoxy(8,14);
printf(" ");

if(antw > 0)
{
gotoxy(1,14);
printf("Command: %d \n",antw);

n = sprintf (sendData, "%d ", antw);
//printf ("[%s] is a %d char long string\n",sendData,n);

printf("SendData: %s\n",sendData);

sendDataStr = (char *)malloc(strlen(sendData));
strcpy(sendDataStr,sendData);

s_len = strlen(sendEOF);

strncat(sendDataStr,sendEOF,s_len);

s_len = strlen(sendDataStr);
printf("S_Len: %d\n",s_len);
printf("SendDataString: %s\n",sendDataStr);

SendData();

gotoxy(12,2);
printf(" ");
}
else
{
comDone = true;
gotoxy(1,1);
printf("Back to the Clip... \n");
break;
}

} // end while comStuff
}
/*** main ************************************************** ***************************************/
int main()
{
done = false;
comDone = false;

// serial stuff
cnt_1 = 0;
state = 0;
oldState = state;

// init usb as /dev/ttyUSB0
system(init_1); // init_1 = "chmod a+rw /dev/ttyUSB0";

// now into While-Loop
while(!done)
{
comDone = false;

// here function for serial comm

servoControl();
// Reader();

if(comDone == true)
{
done = true;
break;
}

} // end While

printf("END and bye bye....\n");
return 0;
} // end main
/************************************************** ************************************************/