IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

envoie commandes at a un modem serie et interruption


Sujet :

C++

  1. #1
    Nouveau Candidat au Club
    Inscrit en
    Septembre 2008
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Septembre 2008
    Messages : 2
    Points : 1
    Points
    1
    Par défaut envoie commandes at a un modem serie et interruption
    Bonjour à tous,

    Voilà j'essaye de faire un programme sous VC++ 2k8 composant un numero et abonnant au bout de X secs (sachant qu'en face çà ne décroche jamais - c'est un équipement industriel doté de modem qui se réveille après callback...).

    J'ai les 2 commandes AT qui vont bien, à savoir:

    ATDT0123456 où 0123456 est le num à composer
    et
    ATH0 qui me permet de couper la connect

    et ce bout de code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
     
    #include "stdafx.h"
    #include<windows.h>
    #include<stdio.h>
     
    class CSerialComm  
    {
    public:
     
     
        BOOL     m_bPortReady;
        HANDLE   m_hCom;
        DCB      PortDCB;
        COMMTIMEOUTS m_CommTimeouts;
        BOOL     bWriteRC;
        BOOL     bReadRC;
        DWORD     iBytesWritten;
        DWORD     iBytesRead;
        BOOL     m_CommOpen;
        BOOL    _bPortReady;
        char     sBuffer[128];
        char     cPortName[5];
     
     
     
        CSerialComm();
        virtual ~CSerialComm();
        BOOL OpenSerialPort();
        BOOL Read(char *buffer, unsigned int ByteCount);
        BOOL Write(char *buffer);
        BOOL CloseSerialPort();
        DWORD GetModemInfo();
    };
     
     
     
    DWORD dwError;
     
    CSerialComm::CSerialComm()
    {
     
    }
     
    CSerialComm::~CSerialComm()
    {
        CloseHandle(m_hCom);
    }
     
    BOOL CSerialComm::OpenSerialPort()
    {
     
    /*
    	GetCommTimeouts (m_hCom, &m_CommTimeouts);
     
         // Change the COMMTIMEOUTS structure settings.
         m_CommTimeouts.ReadIntervalTimeout = MAXDWORD;
         m_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
         m_CommTimeouts.ReadTotalTimeoutConstant = 0;
         m_CommTimeouts.WriteTotalTimeoutMultiplier = 10;
         m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
    */
     
      strcpy(cPortName,"COM3");    
      m_hCom = CreateFileA (cPortName ,// Pointer to the name of the port
                          GENERIC_READ | GENERIC_WRITE,
                                        // Access (read/write) mode
                          0,            // Share mode
                          NULL,         // Pointer to the security attribute
                          OPEN_EXISTING,// How to open the serial port
                          0,            // Port attributes
                          NULL);        // Handle to port with attribute
                                        // to copy
     
      // if fail to open the port, return FALSE.
      if ( m_hCom == INVALID_HANDLE_VALUE )
        // Could not open the port.
          return FALSE;
     
     
      PortDCB.DCBlength = sizeof (DCB);     
     
    // Get the default port setting information.
      GetCommState (m_hCom, &PortDCB);
     
    // Change the DCB structure settings.
    //  PortDCB.BaudRate = 9600;              // Current baud
    //  PortDCB.fBinary = TRUE;               // Binary mode; no EOF check
    //  PortDCB.fParity = TRUE;               // Enable parity checking.
    //  PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control
    //  PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control
    //  PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
                                            // DTR flow control type
    //  PortDCB.fDsrSensitivity = FALSE;      // DSR sensitivity
    //  PortDCB.fTXContinueOnXoff = TRUE;     // XOFF continues Tx
    //  PortDCB.fOutX = FALSE;                // No XON/XOFF out flow control
    //  PortDCB.fInX = FALSE;                 // No XON/XOFF in flow control
    //  PortDCB.fErrorChar = FALSE;           // Disable error replacement.
    //  PortDCB.fNull = FALSE;                // Disable null stripping.
    //  PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
                                            // RTS flow control
    //  PortDCB.fAbortOnError = FALSE;        // Do not abort reads/writes on
                                            // error.
    //  PortDCB.ByteSize = 8;                 // Number of bits/bytes, 4-8
    //  PortDCB.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space
    //  PortDCB.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2
     
    //  unsigned long inBuff = 1200;
    //  unsigned long outBuff = 1200;
    //  SetupComm(m_hCom,inBuff,outBuff);
     
     
      // Configure the port according to the specifications of the DCB
      // structure.
      SetCommState(m_hCom,&PortDCB);
     
      return true;
     
    }
     
    BOOL CSerialComm::Write(char *buffer)
    {
        DWORD dummy;
        if(buffer == NULL)
           return FALSE;
     
     
            WriteFile(  m_hCom,
                        buffer,
                        (DWORD)strlen(buffer),
                        &dummy,
                        0
                     );
     
    return true;
     
    }
     
    BOOL CSerialComm::Read(char *buffer, unsigned int ByteCount)
    {
     
        DWORD dummy;
        if((ByteCount==0) || (buffer == NULL))
            return FALSE;
     
     
        if(!ReadFile(m_hCom,buffer,(DWORD)ByteCount,&dummy,NULL))
            return FALSE;
     
        return TRUE;
     
    }
     
    BOOL CSerialComm::CloseSerialPort()
    {
     
     
        if(CloseHandle(m_hCom)) return TRUE ;
        else return FALSE;
     
    }
     
     
     
     
    void main()
    {
        char buff[800];
     
        for(int i=0;i<800;i++)
            buff[i] = 0;
     
        CSerialComm oSerialComm;
        oSerialComm.OpenSerialPort();
        oSerialComm.Write("ATDT0123456\r");
        oSerialComm.Read(buff,510);
        oSerialComm.CloseSerialPort();
     
        printf("%s",buff);
    }
    J'ai donc d'abord essayé de mettre un :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    ::Sleep(10000);
    oSerialComm.Write("ATH0\r");
    à la suite du oSerialComm.Write("ATDT0123456\r");

    ...mais ce qu'il s'est passé c'est que l'appel a été composé jusqu'à abandon après une bonne minute, puis ensuite la main est passée à mon sleep et à la seconde commande (qui n'avait donc plus lieue d'etre).

    Ensuite j'ai essayé de modifier le timeout sur envoie/reception de données par le bout de code touchant au timeout entre /* et */ en début de classe BOOL CSerialComm::OpenSerialPort().
    Malheureusement, pas de réponse d'en face semble ne pas correspondre à un timeout, car j'esperais un raccrochage après les 10secs indiquées et j'ai eu droit à nouveau à ma minute avant expiration.

    Comment puis-je m'y prendre pour interrompre la composition après X secs, une idée?

    Merci d'avance! :p

  2. #2
    Nouveau Candidat au Club
    Inscrit en
    Septembre 2008
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Septembre 2008
    Messages : 2
    Points : 1
    Points
    1
    Par défaut
    Je viens de penser à un envoie de chaque commande sur un thread séparé.
    Les 2 threads seraient lancés en meme temps, mais celui avec ath0 comporterais un sleep de X secs à son commencement.

    Ca devrait marcher mais c'est peut-etre compliqué, est-ce que vous voyez autre chose?

  3. #3
    Rédacteur

    Avatar de ram-0000
    Homme Profil pro
    Consultant en sécurité
    Inscrit en
    Mai 2007
    Messages
    11 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultant en sécurité
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mai 2007
    Messages : 11 517
    Points : 50 367
    Points
    50 367
    Par défaut
    Je pense qu'il est dangereux d'avoir 2 thread (ou plus) qui se partagent une ressource commune (en l'occurence ton modem)

    Tu ne peux pas imaginer un seul thread qui traite des commandes recues par une fifo (commande numéroter, commande abort, commande sleep, ...)

Discussions similaires

  1. Envoie d'une commande ASCII via le port serie
    Par Ashelthan dans le forum Débuter avec Java
    Réponses: 4
    Dernier message: 21/07/2008, 10h59
  2. Reponse au decrochage (modem serie)
    Par mac&cheese dans le forum C++
    Réponses: 1
    Dernier message: 08/05/2008, 17h03
  3. Problème envoie de données sur le port Serie
    Par petiteso dans le forum C#
    Réponses: 6
    Dernier message: 06/12/2007, 13h33
  4. [MySQL] Aide envoie commande (calcul quantité + prix commande total)
    Par klue_ dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 29/05/2007, 11h49
  5. Connexion internet modem série avec kppp
    Par ggnore dans le forum Réseau
    Réponses: 16
    Dernier message: 25/08/2004, 17h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo