Hallo stochri,
Ich habe mich mal in c++ versucht, es ist ganz einfach die serielle Schnittstelle anzusprechen.
Ich hab da eine Klasse geschrieben, aber in C ist es auch ohne Problemme möglich:
Dazu der Header:Code:#include "avr.h" #include <iostream> using namespace std; avr::avr(void) : fd_avr(-1) { } avr::~avr(void) { closeDevice(); fd_avr = -1; } void avr::waitms(int ms) { struct timeval tv; tv.tv_sec=0; tv.tv_usec=ms*1000; select(0, NULL, NULL, NULL, &tv); } int avr::open_port(void) { if (fd_avr != -1) { //Bereits geöffnet, will nicht nochmal öffen return 1; } struct termios newtio; fd_avr = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY ); if (fd_avr == -1) { perror(DEVICE); return(-1); } else { fcntl(fd_avr, F_SETFL, FNDELAY); newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD ; newtio.c_iflag = IGNPAR | ICRNL ; newtio.c_oflag = 0; newtio.c_lflag &= ~(ICANON); tcflush(fd_avr, TCIOFLUSH); tcsetattr(fd_avr,TCSANOW,&newtio); return fd_avr; } } int avr::write_port(unsigned char _byte){ if (fd_avr == -1) { cout << "ERROR use avr::open() first" << endl; return 0; } int a = write(fd_avr, &_byte ,1); return a; } int avr::read_port(unsigned char * _byte) { if (fd_avr == -1) { cout << "ERROR use avr::open() first" << endl; return 0; } int size = read(fd_avr,_byte,1); return size ; } int avr::getfd() { return fd_avr; } bool avr::isOpen() { return (fd_avr != -1) ? true : false; } void avr::closeDevice() { if( isOpen() ) close(fd_avr); fd_avr = -1; }
Und ein kleines Testprogramm:Code:#ifndef _AVR_H_ #define _AVR_H_ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> #include <ctype.h> #include <assert.h> #define DEVICE "/dev/ttyS0" #define BAUDRATE B9600 class AVR { public: avr(void); ~avr(); void waitms(int); int open_port(void); // Serielle Schnittstelle öffnen int write_port(unsigned char); //ein Byte schreiben int read_port(unsigned char * _byte); //ein Byte lesen int read_portBlocking(unsigned char * _byte);//Block lesen int getfd(void); //liefert den Filedescriptor zurück bool isOpen(void); //Ist der Port schon offen? void closeDevice(); //Port schliessen private: int fd_avr; // Filedescriptor }; #endif //_AVR_H_
Ich hoffe, Du kommst damit klar.Code:#include "avr.h" #include <iostream> using namespace std; int main (void) { AVR * port = new avr(); unsigned char empfangen; int a; port->open_port(); while(1){ port->write_port(255); port->write_port(1); port->waitms(500); port->write_port(254); port->waitms(5); port->write_port(1); port->waitms(500); port->write_port(255); port->waitms(5); port->write_port(0); port->waitms(500); port->write_port(254); port->waitms(5); port->write_port(0); port->waitms(500); port->write_port(253); port->write_port(0); port->read_port(&empfangen); a = empfangen; port->read_port(&empfangen); if (empfangen == 0) { cout << "Temperatur beträgt " << a << " Grad" << endl; }else { cout << "Temperatur beträgt " << a << ",5 Grad" << endl; } port->waitms(5); } return 0; }
das Testprogamm macht nichts anderes als als paar Werte an die Serielle zu schicken, womit der AVR seinerseits die Temperaturwerte schickt, die dann am Bildschirm angezeigt werden.
Wenn interesse besteht, Suche ich den Link, wo das alles beschreiben ist, jetzt finde ich ihn nicht![]()
Gruß Sebastian






Zitieren

Lesezeichen