zemil
Messages postés5Date d'inscriptiondimanche 24 août 2008StatutMembreDernière intervention16 septembre 2008
-
16 sept. 2008 à 20:59
SebLinck
Messages postés212Date d'inscriptionmardi 17 mai 2005StatutMembreDernière intervention23 juin 2011
-
18 sept. 2008 à 09:48
je sui sous linux depuis peux je program avec kdevelope en c++
par hazard j'ais trouvé un logiciel qui arive a faire se que je recherche jé ais donc recupéré la
source mais je narive toujours pas a faire se que je recherche c'est a dir piloté les 3 sortie depuis mon programe et je nais pas resusi a compilé le programe entierement
j'ai besoin de recopier 3 variable bool dans les 3 sortie rts txd drt comme dans le logiciel
comuniqué avec un ecran a led qui contien des registre a décalage 16 registre de 8
avec une des sortir je crée une horloge avec une 2emme une sorti data au front monten de la sortie clock la dernier led s'alume si la sortie data et a 1 et decal toute les led la 3emme sortie et un latche je sé pas comment sa se dit en français
mon programe sapel ap001widget.h ap001widget.cpp et ap001widget.ui la partie grafique
mes 3 variable sont sortie[0] sortie[1] sortie[2]
j'ai 2 fonction que je lance depuis ap001Widget.cpp quand japuie sur 2 bouton
serial.h
/***************************************************************************
* Copyright (C) 2005 Nicolas Hadacek <hadacek@kde.org> *
* Copyright (C) 2003-2004 Alain Gibaud *
* Copyright (C) 2002-2003 Stephen Landamore <stephen@landamore.com> *
* *
* 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 SERIAL_H
#define SERIAL_H
#include "common/global/global.h"
#ifdef Q_OS_UNIX
# include <termios.h>
#elif defined(Q_OS_WIN)
# include
#endif
#include "port_base.h"
serial.cpp
/***************************************************************************
* Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org> *
* Copyright (C) 2003-2004 Alain Gibaud *
* Copyright (C) 2002-2003 Stephen Landamore <stephen@landamore.com> *
* *
* 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 "serial.h"
#ifdef Q_OS_UNIX
# include <stdio.h>
# include <fcntl.h>
# include <sys/time.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/ioctl.h>
# include <errno.h>
# include // needed on some system
#endif
#include <qdatetime.h>
//-----------------------------------------------------------------------------
QStringList *Port::Serial::_list = 0;
#if defined(Q_OS_UNIX)
const Port::Serial::Handle Port::Serial::INVALID_HANDLE = -1;
#elif defined(Q_OS_WIN)
const Port::Serial::Handle Port::Serial::INVALID_HANDLE = INVALID_HANDLE_VALUE;
#endif
///ouverture du port et ranvoir le numero-----------------------------
Port::Serial::Handle Port::Serial::openHandle(const QString &device, IODirs dirs)
{
#if defined(Q_OS_UNIX)
// open non blocking: avoid missing DCD (comment from xwisp2)
int mode = O_NOCTTY | O_NONBLOCK;
if ( dirs & In ) {
if ( dirs & Out ) mode |= O_RDWR;
else mode |= O_RDONLY;
} else mode |= O_WRONLY;
return ::open(device.latin1(), mode);
#elif defined(Q_OS_WIN)
int mode = 0;
if ( dirs & In ) mode |= GENERIC_READ;
if ( dirs & Out ) mode |= GENERIC_WRITE;
return CreateFileA(device.latin1(), mode, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
#endif
}
//ouverture du port et ranvoir le numero-------------------------------
Port::IODirs Port::Serial::probe(const QString &device)
{
Handle handle = openHandle(device, In);
if ( handle==INVALID_HANDLE ) return NoIO;
closeHandle(handle);
handle = openHandle(device, In | Out);
if ( handle==INVALID_HANDLE ) return In;
closeHandle(handle);
return (In | Out);
}
QStringList Port::Serial::deviceList()
{
QStringList list;
#if defined(Q_OS_UNIX)
// standard serport in user space
for (uint i=0; i<8; i++) list.append(QString("/dev/ttyS%1").arg(i));
// new devfs serport flavour
for (uint i=0; i<8; i++) list.append(QString("/dev/tts/%1").arg(i));
// standard USB serport in user space
for (uint i=0; i<8; i++) list.append(QString("/dev/ttyUSB%1").arg(i));
// new devfs USB serport flavour
for (uint i=0; i<8; i++) list.append(QString("/dev/usb/tts/%1").arg(i));
// FreeBSD
for (uint i=0; i<8; i++) list.append(QString("/dev/ttyd%1").arg(i));
// Slackware 11 devfs USB Serial port support.
for (uint i=0; i<8; i++) list.append(QString("/dev/tts/USB%1").arg(i));
// MacOSX devfs USB Serial port support.
list.append("/dev/tty.usbserial");
#elif defined(Q_OS_WIN)
for (uint i=1; i<10; i++) list.append(QString("COM%1").arg(i));
#endif
return list;
}
const QStringList &Port::Serial::probedDeviceList()
{
if ( _list==0 ) {
QStringList all = deviceList();
_list = new QStringList;
for (uint i=0; iappend(all[i]);
}
return *_list;
}
bool Port::Serial::getParameters(Parameters ¶meters)
{
if ( _fd==INVALID_HANDLE ) return false;
#if defined(Q_OS_UNIX)
if ( tcgetattr(_fd, ¶meters)<0 )
{
setSystemError(i18n("Could not get file descriptor parameters"));
return false;
}
#elif defined(Q_OS_WIN)
if ( !GetCommState(_fd, ¶meters.dcb) || !GetCommTimeouts(_fd, ¶meters.comtmo) )
{
setSystemError(i18n("Could not get file descriptor parameters"));
return false;
}
#endif
return true;
}
bool Port::Serial::setParameters(const Parameters ¶meters)
{
if ( _fd==INVALID_HANDLE ) return false;
#if defined(Q_OS_UNIX)
if ( tcsetattr(_fd, TCSANOW, ¶meters)<0 )
{
setSystemError(i18n("Could not set file descriptor parameters"));
return false;
}
#elif defined(Q_OS_WIN)
Parameters tmp = parameters;
if ( !SetCommState(_fd, &tmp.dcb) || !SetCommTimeouts(_fd, &tmp.comtmo) )
{
setSystemError(i18n("Could not set file descriptor parameters"));
return false;
}
#endif
return true;
}
///demande ouvertur du port-----------------------------------
bool Port::Serial::internalOpen()
{
_fd = openHandle(_device, In | Out);
if ( _fd==INVALID_HANDLE ) {
setSystemError(i18n("Could not open device "%1" read-write").arg(_device));
return false;
}
if ( !getParameters(_oldParameters) ) return false; // save configuration
#if defined(Q_OS_UNIX)
if ( _properties & Blocking ) {
int flags = fcntl(_fd, F_GETFL, 0);
if ( fcntl(_fd, F_SETFL, flags & ~O_NONBLOCK)<0 ) {
setSystemError(i18n("Could not modify file descriptor flags"));
return false;
}
}
#endif
return flush(DEFAULT_TIMEOUT);
}
//demande ouvertur du port-----------------------------------
void Port::Serial::internalClose()
{
if ( _fd==INVALID_HANDLE ) return;
if ( _properties & NeedFlush ) flush(0);
if ( _properties & NeedBreak ) doBreak(1);
setParameters(_oldParameters);
closeHandle(_fd);
_fd = INVALID_HANDLE;
}
bool Port::Serial::internalSend(const char *data, uint size, uint timeout)
{
if ( _fd==INVALID_HANDLE ) return false;
QTime time;
time.start();
for (uint todo=size; todo!=0; ) {
#if defined(Q_OS_UNIX)
int res = write(_fd, data+size-todo, todo);
if ( res<0 && errno!=EAGAIN )
{
if ( (_properties & NeedDrain) && !drain(timeout) ) return false;
}
#elif defined(Q_OS_WIN)
DWORD res = 0;
if ( WriteFile(_fd, data+size-todo, todo, &res, NULL)==0 )
{
if ( (_properties & NeedDrain) && !drain(timeout) ) return false;
}
#endif
setSystemError(i18n("Error sending data"));
return false;
}
if ( res>0 ) todo -= res;
else {
if ( uint(time.elapsed())>timeout ) {
log(Log::LineType::Error, i18n("Timeout sending data (%1/%2 bytes sent).").arg(size-todo).arg(size));
return false;
}
msleep(1);
}
return true;
}
bool Port::Serial::internalReceive(uint size, char *data, uint timeout)
{
if ( _fd==INVALID_HANDLE ) return false;
QTime time;
time.start();
for(uint todo=size; todo!=0; ) {
#if defined(Q_OS_UNIX)
// this help reduce CPU usage. It also prevents blocking if the serial cable is disconnected
fd_set rfd;
FD_ZERO(&rfd);
FD_SET(_fd, &rfd);
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout%1000)*1000;
int res = select(_fd+1, &rfd, 0, 0, &tv);
if ( res<0 ) {
setSystemError(i18n("Error receiving data"));
return false;
}
if ( res==0 ) {
log(Log::LineType::Error, i18n("Timeout waiting for data."));
return false;
}
res = read(_fd, data+size-todo, todo);
if ( res<0 && errno!=EAGAIN ) {
setSystemError(i18n("Error receiving data"));
return false;
}
#elif defined(Q_OS_WIN)
DWORD res = 0;
if ( ReadFile(_fd, data+size-todo, todo, &res, NULL)==0 ) {
setSystemError(i18n("Error receiving data"));
return false;
}
#endif
if ( res>0 ) todo -= res;
else {
if ( uint(time.elapsed())>timeout ) {
log(Log::LineType::Error, i18n("Timeout receiving data (%1/%2 bytes received).").arg(size-todo).arg(size));
return false;
}
msleep(1);
}
}
return true;
}
bool Port::Serial::drain(uint timeout)
{
if ( _fd==INVALID_HANDLE ) return false;
#if defined(Q_OS_UNIX)
// tcdrain will block if the serial cable is disconnected
// so we first check for data in output buffer...
QTime time;
time.start();
for (;;) {
int nb;
if ( ioctl(_fd, TIOCOUTQ, &nb)==-1 ) {
setSystemError(i18n("Error checking for data in output buffer"));
return false;
}
if ( nb==0 ) break;
if ( uint(time.elapsed())>timeout ) {
_fd = INVALID_HANDLE; // otherwise close blocks...
log(Log::LineType::Error, i18n("Timeout sending data (%1 bytes left).").arg(nb));
return false;
}
}
if ( tcdrain(_fd)<0 ) {
setSystemError(i18n("Error while draining"));
return false;
}
#endif
return true;
}
bool Port::Serial::internalSetPinOn(Pin pin, bool on)
{
#if defined(Q_OS_UNIX)
int bit = 0;
switch (pin) {
case TX: return ( ioctl(_fd, on ? TIOCSBRK : TIOCCBRK, 0)>=0 );
case DTR: bit = TIOCM_DTR; break;
case RTS: bit = TIOCM_RTS; break;
case RI: bit = TIOCM_RI; break;
default: Q_ASSERT(false); return false;
}
return ( ioctl(_fd, on ? TIOCMBIS : TIOCMBIC, &bit)>=0 );
#elif defined(Q_OS_WIN)
DWORD func = 0;
switch (pin) {
case TX: func = (on ? SETBREAK : CLRBREAK); break;
case DTR: func = (on ? SETDTR : CLRDTR); break;
case RTS: func = (on ? SETRTS : CLRRTS); break;
case RI: // #### not possible with Win32 API ??
default: Q_ASSERT(false); return false;
}
return ( EscapeCommFunction(_fd, func)!=0 );
#endif
}
bool Port::Serial::setPinOn(uint pin, bool on, LogicType type)
{
if ( _fd==INVALID_HANDLE ) return false;
if ( type==NegativeLogic ) on = !on;
Q_ASSERT( pin<Nb_Pins );
Q_ASSERT( PIN_DATA[pin].dir==Out );
if ( !internalSetPinOn(Pin(pin), on) ) {
setSystemError(i18n("Error setting bit %1 of serial port to %2").arg(PIN_DATA[pin].label).arg(on));
return false;
}
return true;
}
bool Port::Serial::internalReadPin(Pin pin, LogicType type, bool &value)
{
#if defined(Q_OS_UNIX)
int bits;
if ( ioctl(_fd, TIOCMGET, &bits)<0 ) return false;
int mask = 0;
switch (pin) {
case DCD: mask = TIOCM_CD; break;
case RX : mask = TIOCM_SR; break;
case DSR: mask = TIOCM_DSR; break;
case CTS: mask = TIOCM_CTS; break;
default: Q_ASSERT(false); return false;
}
value = ((type==NegativeLogic ? ~bits : bits) & mask);
return true;
#elif defined(Q_OS_WIN)
DWORD status;
if ( GetCommModemStatus(_fd, &status)==0 ) return false;
switch (pin) {
case DCD: value = (status & MS_RLSD_ON); break;
case DSR: value = (status & MS_DSR_ON); break;
case CTS: value = (status & MS_CTS_ON); break;
case RX: // not possible with Win32 API ??
default: Q_ASSERT(false); return false;
}
if ( type==NegativeLogic) value = !value;
return true;
#endif
}
direct.h
/************<<<<***************************************************************
* Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org> *
* Copyright (C) 2003-2004 Alain Gibaud *
* Copyright (C) Brian C. Lane *
* *
* 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 DIRECT_H
#define DIRECT_H
#include "devices/pic/prog/pic_prog.h"
namespace Direct
{enum State { Low 0, High 1, Off = Low, On = High };
enum PinType { Vpp = 0, Vdd, Clock, DataOut, DataIn, DataRW, Nb_PinTypes };
struct PinData {
const char *label, *offLabel, *onLabel, *key, *comment;
Port::IODir dir;
bool canBeGround;
const char *testComment;
};
extern const PinData PIN_DATA[Nb_PinTypes];
//-----------------------------------------------------------------------------
class HardwareData;
enum Type { Normal, EPEToolkitMK3 };
struct HData
{
int pins[Nb_PinTypes];
uint clockDelay;
Type type;
};
class Hardware : public ::Programmer::PicHardware
{
public:
static Hardware *create(Port::Base *port, const Device::Data &device, const HData &data);
// hardware test --- please use it for a newly
// designed/constructed programmer board
// because pin assignation is hard coded in this
// routine, you might have to edit it. ++Gib:
//static int hardware_test();
// timing test --- please use it to ensure that
// the program meets the timing specifications
//static int timing_test();
class SerialHardware : public Hardware
{
public:
SerialHardware(::Programmer::Base &base, const QString &portDevice, const HardwareData &data);
};
class ParallelHardware : public Hardware
{
public:
ParallelHardware(::Programmer::Base &base, const QString &portDevice, const HardwareData &data);
};
} // namespace
#endif
direct.cpp
/***************************************************************************
* Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org> *
* Copyright (C) 2003-2004 Alain Gibaud *
* Copyright (C) Brian C. Lane *
* *
* 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 "direct.h"
//-----------------------------------------------------------------------------
const Direct::PinData Direct::PIN_DATA[Nb_PinTypes] = {
{ I18N_NOOP("MCLR (Vpp)"), "0 V", "13 V", "vpp",
I18N_NOOP("The VPP pin is used to select the high voltage programming mode."),
Port::Out, false, I18N_NOOP("Check this box to turn voltage on/off for this pin.") },
{ I18N_NOOP("Power (Vdd)"), "0 V", "5 V", "vdd",
I18N_NOOP("The VDD pin is used to apply 5V to the programmed device.\nMust be set to GND if your programmer doesn't control the VDD line."),
Port::Out, true, I18N_NOOP("Check this box to turn voltage on/off for this pin.") },
{ I18N_NOOP("Clock"), "0 V", "5 V", "clock",
I18N_NOOP("The CLOCK pin is used to synchronize serial data of the DATA IN and DATA OUT pins."),
Port::Out, false, I18N_NOOP("Check this box to turn voltage on/off for this pin.") },
{ I18N_NOOP("Data Out"), "0 V", "5 V", "datao",
I18N_NOOP("The DATA OUT pin is used to send data to the programmed device."),
Port::Out, false, I18N_NOOP("Check this box to turn voltage on/off for this pin.") },
{ I18N_NOOP("Data In"), "0 V", "5 V", "datai",
I18N_NOOP("The DATA IN pin is used to receive data from the programmed device."),
Port::In, false, I18N_NOOP("This pin is driven by the programmed device.\nWithout device, it must follow the "Data out" pin (when powered on).") },
{ I18N_NOOP("Data R/W"), "Data in", "Data out", "drw",
I18N_NOOP("The DATA RW pin selects the direction of data buffer.\nMust be set to GND if your programmer does not use bi-directionnal buffer."),
Port::Out, true, I18N_NOOP("Check this box to change DATA buffer direction.") }
};
//-----------------------------------------------------------------------------
namespace Direct
{
class SerialPort : public Port::Serial
{
public:
SerialPort(const QString &device, Log::Base &base)
: Serial(device, NeedBreak, base) {}
bool open() {
if ( !Port::Serial::open() ) return false;
if ( !setMode(IgnoreBreak | IgnoreParity, ByteSize8 | EnableReceiver | IgnoreControlLines, S9600, 0) ) return false;
// set up lines for "idle state" ???
return true;
}
};
} // namespace
// stage 6 - VPP check
hw.setPin(Vpp, On);
cout << "16F84 pin 4 (VDD) must be between 13V and 14V"<<endl ;
do
{
cout << "OK ? (y/n) " ;
cin >> t ;
} while(t[0] != 'y' && t[0] != 'n') ;
hw.setPin(Vpp , Off);
if( t[0] == 'n') return 6 ;
// stage 7 - test input data
// set data out hi, because bi-directionnal
// on pin 13 uses the open collector capability of 7407
hw.setPin(DataOut, High);
int in = hw.readBit();
if( !in )
{
cout << "DataIn error (16F84 pin 13) : must be 5V and is not" << endl ;
return 7 ;
}
cout << "Please set 16F84 pin 13 (DataIn) low " <<
"(connect it to 16F84 pin 5 - GND)" << endl ;
do
{
cout << "Done ? (y/n) " ;
cin >> t ;
} while(t[0] != 'y' && t[0] != 'n') ;
in = hw.readBit();
if(in )
{
cout << "DataIn error (pin 13) : must be 0V and is not" << endl ;
return 7 ;
}