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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#include <iostream>
#include <string> //manipuler des chaines de caracteres
#include <tchar.h>
#include <windows.h>//lire config.ini
#include <ctime>
#include <fstream> //manipulation de fichiers
#include "include/Appel.h"
#include "include/CSerial.h"
#include "include/function.h"
using namespace std;
/*Creation d'un service permanement connecté au port et qui lit des donnees.
*Ces donnees sont stockées dans une base de données
*
*
*/
//
int __cdecl _tmain ()
{
//Definition des constantes
string const configFile="config.ini";//fichier de configuration
string const logFile="C:/call.log";//fichier de logs
int const annee=getYear();//annee en cours
//Declaration d'un flux permettant d'écrire dans le logFile
ofstream oss(logFile.c_str); //Déclaration d'un flux permettant d'écrire dans le fichier
if (!oss) cout << "ERREUR: Impossible d'ouvrir le fichier de logs." << endl;
CSerial serial;
LONG lLastError = ERROR_SUCCESS;
//Recuperation du port dans le fichier de configuration
string address=getConfig ("PORT DE COMMUNICATION","Port",configFile);
// Attempt to open the serial port (Port)
lLastError = serial.Open(_T(address),0,0,true);
if (lLastError != ERROR_SUCCESS) oss <<"Unable to open COM-port"<<endl;
// Setup the serial port (9600,8N1, which is the default setting)
lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
if (lLastError != ERROR_SUCCESS) oss <<"Unable to set COM-port setting"<<endl;
// Setup handshaking (default is no handshaking)
lLastError = serial.SetupHandshaking(CSerial::EHandshakeHardware);
if (lLastError != ERROR_SUCCESS) oss<<"Unable to set COM-port handshaking";
// Register only for the receive event
lLastError = serial.SetMask(CSerial::EEventBreak |
CSerial::EEventCTS |
CSerial::EEventDSR |
CSerial::EEventError |
CSerial::EEventRing |
CSerial::EEventRLSD |
CSerial::EEventRecv);
if (lLastError != ERROR_SUCCESS) oss <<"Unable to set COM-port event mask"<<endl;
// Use 'non-blocking' reads, because we don't know how many bytes
// will be received. This is normally the most convenient mode
// (and also the default mode for reading data).
lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);
if (lLastError != ERROR_SUCCESS) oss<<"Unable to set COM-port read timeout."<<endl;
// Create a handle for the overlapped operations
HANDLE hevtOverlapped = ::CreateEvent(0,TRUE,FALSE,0);;
if (hevtOverlapped == 0) oss<<"Unable to create manual-reset event for overlapped I/O."<<endl;
// Setup the overlapped structure
OVERLAPPED ov = {0};
ov.hEvent = hevtOverlapped;
// Open the "STOP" handle
HANDLE hevtStop = ::CreateEvent(0,TRUE,FALSE,_T("Overlapped_Stop_Event"));
if (hevtStop == 0) oss <<"Unable to create manual-reset event for stop event."<<endl;
// Keep reading data, until an EOF (CTRL-Z) has been received
bool fContinue = true;
int badrecord(0);
do
{
// Wait for an event
lLastError = serial.WaitEvent(&ov);
if (lLastError != ERROR_SUCCESS) oss<<"Unable to wait for a COM-port event."<<endl;
// Setup array of handles in which we are interested
HANDLE ahWait[2];
ahWait[0] = hevtOverlapped;
ahWait[1] = hevtStop;
// Wait until something happens
switch (::WaitForMultipleObjects(sizeof(ahWait)/sizeof(*ahWait),ahWait,FALSE,INFINITE))
{
case WAIT_OBJECT_0:
{
// Save event
const CSerial::EEvent eEvent = serial.GetEventType();
// Handle break event
if (eEvent & CSerial::EEventBreak)
{
oss<<"\n### BREAK received ###\n"<<endl;
}
// Handle CTS event
if (eEvent & CSerial::EEventCTS)
{
oss<<"\n### Clear to send %s ###\n", serial.GetCTS()?"on":"off"<<endl;
}
// Handle DSR event
if (eEvent & CSerial::EEventDSR)
{
oss<<"\n### Data set ready %s ###\n", serial.GetDSR()?"on":"off"<<endl;
}
// Handle error event
if (eEvent & CSerial::EEventError)
{
oss<<"\n### ERROR: "<<endl;
switch (serial.GetError())
{
case CSerial::EErrorBreak: oss<<"Break condition"<<endl; break;
case CSerial::EErrorFrame: oss<<"Framing error"<<endl; break;
case CSerial::EErrorIOE: oss<<"IO device error"<<endl; break;
case CSerial::EErrorMode: oss<<"Unsupported mode"<<endl; break;
case CSerial::EErrorOverrun: oss<<"Buffer overrun"<<endl; break;
case CSerial::EErrorRxOver: oss<<"Input buffer overflow"<<endl; break;
case CSerial::EErrorParity: oss<<"Input parity error"<<endl; break;
case CSerial::EErrorTxFull: oss<<"Output buffer full"<<endl; break;
default: oss<<"Unknown"<<endl; break;
}
oss<<" ###\n"<<endl;
}
// Handle ring event
if (eEvent & CSerial::EEventRing)
{
oss<<"\n### RING ###\n"<<endl;
}
// Handle RLSD/CD event
if (eEvent & CSerial::EEventRLSD)
{
oss<<"\n### RLSD/CD %s ###\n", serial.GetRLSD()?"on":"off"<<endl;
}
// Handle data receive event
if (eEvent & CSerial::EEventRecv)
{
// Read data, until there is nothing left
DWORD dwBytesRead = 0;
do
{
char szBuffer[101];
// Read data from the COM-port
lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);
if (lLastError != ERROR_SUCCESS)
oss<<"Unable to read from COM-port."<<endl;
if (dwBytesRead > 0)
{
// Finalize the data, so it is a valid string
szBuffer[dwBytesRead] = '\0';
//Get call interesting information
int recNo,originID,terminalID,cos,dialedDigits,transLine;
int callMn,callH,duration,oldrecType,callDay,callMonth,endTime;
string recType =chaine.sbstr(0,1);
recNo = convertStringInt(chaine.sbstr(2,4));
originID = convertStringInt(chaine.sbstr(9,8));
terminalID = convertStringInt(chaine.sbstr(17:8)) ;
switch(recType)
{
case 'A':
{
cos = convertStringInt(chaine.sbstr(49,3));
//Un moyen de verifier si la ligne précédente commencait par un A
oldrecType='A';
break;
}
//Voyons si l'appel a ete tranfere
case 'S':
{
callMonth=convertStringInt(chaine.sbstr(37,2));
callDay =convertStringInt(chaine.sbstr(40:2));
callMn = convertStringInt(chaine.sbstr(43,2));
callH=convertStringInt(chaine.sbstr(46,2));
dialedDigits = convertStringInt(chaine.sbstr(58,33)); ;
transLine = terminalID;
oldrecType='S';
if(oldrecType!='A')
{
cos = '0';
};
break;
}
case 'N':
{
//on decompose la date de debut d'appel en tout petits champs entiers qui pourront etre utilises
//pour intitialiser le type time_t
callDay =convertStringInt(chaine.sbstr(40:2));
callMn = convertStringInt(chaine.sbstr(43,2));
callH=convertStringInt(chaine.sbstr(46,2));
//on recupere les autres informations au format entier
dialedDigits = convertStringInt(chaine.sbstr(58,33));
//recuperation et conversion de la duree de l'appel en secondes
duration = convertStringInt(chaine.sbstr(49:2))*3600+convertStringInt(chaine.sbstr(52:2))*60+convertStringInt(chaine.sbstr(55:2)));
dialedDigits = convertStringInt(chaine.sbstr(58,33));
transLine = ' ';
oldrecType='N';
if(oldrectype!='A')
{
cos='0';
};
break;
}
case 'E':
{
if(oldrecType=='S')
{
//on decompose la date de fin d'appel en tout petits champs entiers qui pourront etre utilises
//pour intitialiser le type time_t
endH = convertStringInt(chaine.sbstr(43:2));
endMn=convertStringInt(chaine.sbstr(46:2));
time_t debut=setDate(callDay,callMonth,annee,callH,callMn,0);
time_t debut=setDate(callDay,callMonth,annee,endH,endMn,0);
duration=difftime(debut, fin);//calcul de la duree de l'appel en secondes
};
oldrecType='E';
break;
}
default :
badrecord++;
}; //fin du switch
//On stocke les champs extraits dans un objet de type Appel.
Appel callread(originID,dialedDigits,debut,duration,transLine,cos,terminalID);
if(Appel.enregistrerDB==1) //ecrire erreur dans le fichier de log;
}
}
while (dwBytesRead > 0);
}
}
break;
case WAIT_OBJECT_0+1:
{
// Set the continue bit to false, so we'll exit
fContinue = false;
}
break;
default:
{
// Something went wrong
oss<<"Error while calling WaitForMultipleObjects."<<endl;
}
break;
};
}
while (fContinue);
// Close the port again
serial.Close();
return 0;
}
et le retour du compilateur : |
Partager