PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Probleme mit RS232 - software



nomad
19-10-2007, 09:00
Probleme mit RS232

hallo,

beschaeftige mich zur zeit mit mit der entwicklung einer rs232-verbindung zu einem microcontroller.
das untenstehende pgm usbRS232Prop.c
das programm habe ich schon frueher erfolgreich benutzt um microcontroller via rs232 anzubinden.
Im augenblick habe ich etliche probleme damit:

1) in der funktion Reader() (also lesen) bleibt das programm haengen.
das heisst, die linuxbox wieder neu starten..
gibt es softwaremaessig eine moeglichkeit, dass wenn die funktion ss. >>>>>
keinen input bekommt, nach einer gewissen zeit, der ganze stuff abgebrochen wird, und das
programm korrekt beendet wird (ohne neustart)

for(i=1;i<=100;i++)
{
>>>> res = read(fr,buf,255);
buf[res]=0; /* set end of string, so we can printf */

// delay(1000);

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

rescommand = res;
printf("CommandoBuf: %s",buf);
printf("Commr: %d\n",rescommand);
}

2) das untenstehende pgm. liest zeilenweise den input mit abschliessenden <\r> also
CR carrigeReturn (ascii dec 13)
wie muesste ich das pgm umbauen, damit nur einzelne zeichen gelesen werden???

ueber hilfe wuerde ich mich sehr freuen.
mfg
nomad


// usbRS232Prop.c
// compile with : cc -o usbRS232Prop usbRS232Prop.c

************************************************** **************************
* Contributions:
* The Linux Serial Programming HOWTO by
* Peter H. Baumann, Peter.Baumann@dlr.de v1.0, 22 January 1998
* This document describes how to program communications with
* devices over a serial port on a Linux box.
************************************************** **************************/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.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>

#define BAUDRATE B9600 // baudrate settings

#define MODEMDEVICE_1 "/dev/ttyUSB0" // usbCOM 1
#define MODEMDEVICE_2 "/dev/ttyUSB1" // usbCOM 2
#define MODEMDEVICE_3 "/dev/ttyUSB2" // usbCOM 3


#define _POSIX_SOURCE 1 // POSIX compliant source
#define FALSE 0
#define TRUE 1

typedef unsigned char byte;

int STOP=FALSE;

int fr,fw,c, res,n;
struct termios oldtio,newtio;

char buf[255];

char *bufcommand = buf;
char *bufcommand1 = buf;
char *recbuf = "1\n";
int rescommand;
int command;
int i;
int cnt; // zaehler fuer lesen

char *send0 = "0\r";
char *send1 = "1\r";
char *send2 = "2\r";
char *send3 = "3\r";
char *send4 = "4\r";
char *send5 = "5\r";
char *send6 = "6\r";
char *send7 = "7\r";
char *send8 = "8\r";
char *send9 = "9\r";
char *send10 = "10\r";
char *send12 = "12344\r"; // spezial weil sonst zu schnell

char *sendData;


/*** 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);
}
/************************************************** ***********************/

Reader()
{
printf("In usbReader\n");

fr = open(MODEMDEVICE_1, O_RDONLY | O_NOCTTY );
if(fr <0)
{
perror(MODEMDEVICE_1);

printf("cannot open ttyUSB0\n");

exit(-1);
}

printf("After Open\n");

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

newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

newtio.c_iflag = IGNPAR | ICRNL; // Raw output.
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */

// now clean the modem line and activate the settings for the port

tcflush(fr, TCIFLUSH);
tcsetattr(fr,TCSANOW,&newtio);

printf("Now read the Input\n");

// new stuff from serialin = mehrmaliges lesen
int i;

for(i=1;i<=100;i++)
{
res = read(fr,buf,255);
buf[res]=0; /* set end of string, so we can printf */

// delay(1000);

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

rescommand = res;
printf("CommandoBuf: %s",buf);
printf("Commr: %d\n",rescommand);

}


/*restore the old port settings */

tcsetattr(fr,TCSANOW,&oldtio);

close(fr);

printf("Close usbReader as Serial_Device\n");

}

Sender()
{
printf("In Sender\n");

//scanf("%d",&command);

command = 10;

printf("Input: %d\n",command);

if(command==0)
{
/* restore the old port settings */

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

printf("Command was <0> Close Serial_Device and exit(0)\n");
exit(0);
}
if(command==1) sendData = send1;
if(command==2) sendData = send2;
if(command==3) sendData = send3;
if(command==4) sendData = send4;
if(command==5) sendData = send5;
if(command==6) sendData = send6;
if(command==7) sendData = send7;
if(command==8) sendData = send8;
if(command==9) sendData = send9;
if(command==10) sendData = send10;
if(command==12) sendData = send12;

sendData = send12;

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

fw = open(MODEMDEVICE_1, O_WRONLY | O_NOCTTY ); // old O_RDWR
if(fw <0)
{
// perror(MODEMDEVICE);
printf("cannot open /dev/ttyUSB0\n");
exit(-1);
}
tcgetattr(fw,&oldtio); // save current serial port settings
bzero(&newtio,sizeof(newtio)); // clear struct for new port settings

newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

newtio.c_iflag = IGNPAR | ICRNL; // Raw output.
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;

newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */

// now clean the modem line and activate the settings for the port

tcflush(fw, TCIFLUSH);
tcsetattr(fw,TCSANOW,&newtio);

int i;

for(i=0;i<=100;i++)
{
write(fw,sendData,3); // 3 old 2 or res or 255

delay(100);

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

if(command == 0)
{
/* restore the old port settings */

tcsetattr(fw,TCSANOW,&oldtio);

close(fw);

printf("Command was <0> Close Serial_Device and exit(0)\n");

exit(0);
}


/* restore the old port settings */

tcsetattr(fw,TCSANOW,&oldtio);

close(fw);

printf("Close Sender as Serial_Device\n");

} // end Sender

main()
{
printf("The USB-SerialProp-Test Version 0.1.\n");

printf("Input for Commands: \n");
// printf("(0-9) 0=Exit, 1-9, 10 & 12 = Commands\n");

// - usbPort 1 : chmod a+rw /dev/ttyUSB0 - hinterer seitlicher usbPort
// - usbPort 2 : chmod a+rw /dev/ttyUSB1 - vorderer seitlicher usbPort

system("chmod a+rw /dev/ttyUSB0");

// system("chmod a+rw /dev/ttyUSB1");
// system("chmod a+rw /dev/ttyUSB2");

cnt = 0;
do
{
Sender(); // schreiben

cnt++;

Reader(); // lesen

if(cnt==10)
{
/* restore the old port settings */

tcsetattr(fw,TCSANOW,&oldtio);

close(fw);

printf("Command was <0> Close Serial_Device and exit(0)\n");

exit(0);

// printf("Exit\n");
// break;
}


}while(command > 0);

printf("Exit and Bye....\n");

} // end pgm

/************************************************** *************************/