PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Lesen aus den seriellen Schnittstellen



katia
16.05.08, 20:14
hallo Leute,
ich versuche seit drei Wochen ein String aus der seriellen Schnittstellen zu lesen .Der Sting ist von meinem mikrocontroller gesendet. Die daten sind schon auf dem Port ich kann mit dem Befehl "cat /dev/ttyS1" überprüfen, aber mit dem read Funktion bekomme ich immer die Meldung " SERIAL EAGAIN ERROR".
Bitte, wenn Jemanden weiss, wo liegt den Fehler, bitte sich meldet.
Anbei steht meine Code als anhänge.

ich danke euch.
Katia

derguteweka
16.05.08, 21:39
Ich werde diese Schallplatte nicht kaufen, sie ist zerkratzt.

nomad
17.05.08, 08:55
hi katia,
also receiver.c kann ich ohne fehler compilieren.
mit transmit.c hab ich so meine probleme wegen avr.h..... etc.
ich schick dir mal als attachment ein kleines c-programm.
sehr alt, aber ohne auf die eigenheiten des microcontrollers einzugehen.
funzzt das prog.
hoffe dir damit geholfen zu haben.
gruss
nomad

katia
18.05.08, 21:13
ich versuche das Programm morgen durchzuführen, ich melde mich bei dir.
ich danke dir

katia
18.05.08, 21:18
ich stelle gerade fest, dass ich die Datei nicht lesen, alles sieht komisch aus. Kannst du als Zip Datei speichern.
Danke

Newbie314
18.05.08, 21:24
Die Datei ist in Ordnung. Mal in "Kate" oder vi öffnen ?

nomad
19.05.08, 08:26
hi,
um's einfacher zu machen hier der code:
(das ist linux nicht win)
gruss
nomad


//================================================== =======================//
// serial.c //
// -------- //
// //
// compile with : cc serial.c -o serial //
// start with : ./serial //
// //
// for : serial communications from system via serial //
// interface to a microcontroller MC68HC05 //
// BS2-Stamp, C-Control, GPS-Receiver //
//************************************************** ************************/
// THIS PROGRAMM IS UNDER THE GNU-LICENCE //
/************************************************** *************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
************************************************** **************************
* 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. *
************************************************** **************************

03.06. -> interduction communications
Canonical Input Processing
1) schreiben gem serial_8.c
2) lesen gem serial_6/6a.c
=> einbau von Sender > schreiben auf mc
einbau von Reader > lesen von mc


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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.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 "/dev/ttyS0" // change this definition for the correct port
#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;
struct termios oldtio,newtio;
char buf[255];
char *bufcommand = buf;
char *bufcommand1 = buf;
char *recbuf = "1\n";
int rescommand;
int command;
int i;

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

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

printf("cannot open ttyS0");

exit(-1);
}
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);

gotoxy(2,8);
printf("*************************************");

res = read(fr,buf,255);
buf[res]=0; /* set end of string, so we can printf */

gotoxy(10,15);
printf("In: %s", buf); // printf(":%s:%d\n", buf, res);
gotoxy(10,16);
printf("Res: %d",res);

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

gotoxy(2,24);
printf("*************************************");

/* restore the old port settings */

tcsetattr(fr,TCSANOW,&oldtio);

close(fr);

}

Sender()
{
gotoxy(40,4);
scanf("%d",&command);

gotoxy(10,6);
printf("Input: %d",command);
gotoxy(40,4);
printf(" ");

if(command==0) sendData = send0;
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;


// sendData = "614\r";

gotoxy(10,7);
printf("SendData: %s",sendData);

fw = open(MODEMDEVICE, O_WRONLY | O_NOCTTY ); // old O_RDWR
if(fw <0)
{
perror(MODEMDEVICE);
printf("cannot open ttyS0");
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);

gotoxy(2,8);
printf("*************************************");

write(fw,sendData,2); // old 2 or res or 255

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

tcsetattr(fw,TCSANOW,&oldtio);

close(fw);

gotoxy(10,22);
printf("Command was <0> Close Serial_Device and exit(0)");
gotoxy(2,24);
printf("*************************************\n");

exit(0);
}

gotoxy(2,24);
printf("*************************************");

/* restore the old port settings */

tcsetattr(fw,TCSANOW,&oldtio);

close(fw);

gotoxy(10,22);
printf("Close Serial_Device");

} // end Sender

main()
{
clrscr();
gotoxy(2,1);
printf("The Serial-Communications");

gotoxy(10,4);
printf("Input for Commands: ");
gotoxy(10,5);
printf("(0-9) 0=Exit, 1-9=Commands");
do
{
Sender(); // schreiben

Reader(); // lesen

}while(command > 0);

} // end pgm

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

Newbie314
19.05.08, 09:53
@Kati: kannst du von den N anderen Threads die du zu dem Thema erstellt hast auf diesen verweisen ? Multiposting hilft weder dir noch den Leuten die dir helfen wollen.

nomad
19.05.08, 16:33
hi
also dasselbe als serial-c - zip-file
gruss
nomad