[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: How to open & capture serial communication?
From:       "muench () tainet ! de" <muench () tainet ! de>
Date:       2001-09-17 15:21:16
[Download RAW message or body]

Am Monday 17 September 2001 03:02 schrieb Boulgakov Andrei:
> Hi!
> Is there in KDE some class handling serial/parallel ports?
>  
> >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to 
unsubscribe <<
> 
> 

Hi
look at my attachments, perhaps you can find this useable.
It may not be the best, perhaps it is from my first KDE / C++ days, also 
there could be some things i needed for me ( rm_checksum(), which generates a 
checksum ... )


Greets,
	Daniel Münch

PS: Sorry for my bad english (:


["iohandler.h" (text/plain)]

/***************************************************************************
                          iohandler.h  -  description
                             -------------------
    begin                : Tue May 15 2001
    copyright            : (C) 2001 by Daniel Muench
    email                : muench@tainet.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef IOHANDLER_H
#define IOHANDLER_H

#include <qobject.h>
#include <qsocketnotifier.h>

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <locale.h>
#include <unistd.h>


#define RM_BAUDRATE B38400


#define _POSIX_SOURCE 1 /* POSIX compliant source */

#define FALSE 0
#define TRUE 1

#define EOT 4
#define ENQ 5
#define ACK 6
#define STX 2
#define NAK 21
#define CR  13



class IOHandler : public QObject  {
   Q_OBJECT
public:

	IOHandler(const char *fname=0);
	~IOHandler();

	
public slots:
	int openDevice(const char *fname);
	void closeDevice();
	int send(char *str);

signals:

	void dataReceivedOK();

private slots:
	int dataReceived(int); /* Slot, der vom QSocketNotifier aufgerufen wird, wenn daten Vorhanden sind */
/*	void parseData(); */

protected:
	char buffer[255]; // Buffer, wo das gelesene von der Schnittstelle gespeichert wird

private:

	int rm_fd; // FileDeskriptor der Schnittstelle
	struct termios oldtio,newtio; // Termios für die Schnittstelle


	QSocketNotifier *sn;
			
	char rm_checksum(char* chkstr);
	int rm_knock(void);
	
	
};


#endif

["iohandler.cpp" (text/plain)]

/***************************************************************************
                          iohandler.cpp  -  description
                             -------------------
    begin                : Tue May 15 2001
    copyright            : (C) 2001 by Daniel Muench
    email                : muench@tainet.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "iohandler.h"

/*IOHandler::IOHandler(QWidget *parent, const char *name ) : QWidget(parent,name) {
}
IOHandler::~IOHandler(){
}*/

/*IOHandler::IOHandler()
{
	rm_fd=-1;
	connect(this,SIGNAL(dataReceivedOK()),SLOT(parseData()));
}*/

IOHandler::IOHandler(const char *dev)
{
	rm_fd=-1;
	
	if(dev!=0) openDevice(dev);
//	connect(this,SIGNAL(dataReceivedOK()),SLOT(parseData()));
}

IOHandler::~IOHandler()
{
	if(rm_fd!=-1) closeDevice();

}

int IOHandler::openDevice(const char *dev)
{
 rm_fd = open(dev, O_RDWR | O_NOCTTY );
 if (rm_fd <0) {perror(dev); return(-1); }

 tcgetattr(rm_fd,&oldtio);
 bzero(&newtio, sizeof(newtio));

 newtio.c_cflag = RM_BAUDRATE | CS8 | CLOCAL | CREAD;

 newtio.c_iflag = IGNPAR | ICRNL;

 newtio.c_oflag = 0;

 newtio.c_lflag = 0;//ICANON;

 newtio.c_cc[VINTR]    = 0;
 newtio.c_cc[VQUIT]    = 0;
 newtio.c_cc[VERASE]   = 0;
 newtio.c_cc[VKILL]    = 0;
 newtio.c_cc[VEOF]     = 4;
 newtio.c_cc[VTIME]    = 0;
 newtio.c_cc[VMIN]     = 0;
 newtio.c_cc[VSWTC]    = 0;
 newtio.c_cc[VSTART]   = 0;
 newtio.c_cc[VSTOP]    = 0;
 newtio.c_cc[VSUSP]    = 0;
 newtio.c_cc[VEOL]     = 0;
 newtio.c_cc[VREPRINT] = 0;
 newtio.c_cc[VDISCARD] = 0;
 newtio.c_cc[VWERASE]  = 0;
 newtio.c_cc[VLNEXT]   = 0;
 newtio.c_cc[VEOL2]    = 0;

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


 sn=new QSocketNotifier(rm_fd,QSocketNotifier::Read,this);
 QObject::connect(sn, SIGNAL(activated(int)),this,SLOT(dataReceived(int)) );

	return(1);
}

void IOHandler::closeDevice()
{
	tcsetattr(rm_fd,TCSANOW,&oldtio);
	close(rm_fd);
	delete sn;
	rm_fd=-1;
}

int IOHandler::dataReceived(int fd)
{

	char chksum,c,c_buf[255];
	int i_rcheck,i_tries=0;


	while(i_tries<3)
	{
		i_rcheck=read(rm_fd,c_buf,255);
		if(i_rcheck==-1 || i_rcheck==0) return(-1);
		c_buf[i_rcheck]=0;
		chksum=c_buf[strlen(c_buf)-2];
		c_buf[strlen(c_buf)-2]='\0';
		
		c=ACK;
		if(rm_checksum(c_buf)==chksum)  c=ACK; else c=NAK;
		usleep(10000);
//		write(rm_fd,&c,1);
		
		if(c==ACK) i_tries=5; else i_tries++;
	}


	strcpy(buffer,c_buf);


	qDebug("IOHandler: Data Received: %s",c_buf);
	emit dataReceivedOK();

	return(1);
}



/* RMIII - Spezifische Funktionen */


int IOHandler::send(char *strtosend)
{
	int i_wrchk,i_wrchk2,i_wrchk3,i_versuche=0;
	char c,buf[10];
	
	rm_knock();
	while(i_versuche<6)
	{
			i_wrchk=write(rm_fd,strtosend,strlen(strtosend));
			
			c=rm_checksum(strtosend);
			i_wrchk2=write(rm_fd,&c,1);
			c=CR;
			i_wrchk3=write(rm_fd,&c,1);
			
		if((i_wrchk==0 || i_wrchk==-1) || (i_wrchk2==0 || i_wrchk2==-1) || (i_wrchk3==0 || \
i_wrchk3==-1)) i_versuche++; else i_versuche=10;  }
	if(i_versuche==6) return(-1);


	usleep(30000);
	i_wrchk = read(rm_fd,buf,1);
	buf[i_wrchk]=0;

	switch(buf[0])
	{
			case ACK:
								return(1);
								break;
			case NAK:
								return(-1);
								break;
			default:
								return(-1);
								break;
   }
}


char IOHandler::rm_checksum(char* chkstr)
{
    char c=0;
    int i;

    for(i=0;i<=strlen(chkstr);i++) c^=chkstr[i]; // c=c^chkstr[i]
    if(c<32) c+=32;
    return(c);
}

int IOHandler::rm_knock(void)
{
    char ch1,ch2=0;

    ch1=ENQ;

    if(write(rm_fd,&ch1,1)==-1) return(-1);
    usleep(10000);


    if(read(rm_fd,&ch2,1)==-1) return(-1);
    if(ch2==STX) return(1); else return(-1);
}

//IOParser::IOParser() {}

#include "iohandler.moc"


>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic