Comment envoyer des données sur un port série sous xp

tchen01 Messages postés 1 Date d'inscription lundi 8 mars 2004 Statut Membre Dernière intervention 9 mars 2004 - 9 mars 2004 à 17:31
ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 - 9 mars 2004 à 23:09
je voudrais envoyer des commandes hexadecimales sur un port série en c++ sous xp et les commandes outp et inp ne sont pas valables.

merci d'avance

1 réponse

ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
9 mars 2004 à 23:09
utilise les API Win32 CreateFile et WriteFile :

un exemple qui fait un appel sur un modem connecté au port série :

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

#define RX_SIZE 4096
#define TX_SIZE 4096

COMMTIMEOUTS cto =
{
0xFFFFFFFF,// ReadIntervalTimeOut
0, // ReadTotalTimeOutMultiplier
0, // ReadTotalTimeOutConstant
0, // WriteTotalTimeOutMultiplier
0 // WriteTotalTimeOutConstant
};

DCB dcb =
{
sizeof(DCB), // DCBlength
9600, // BaudRate
TRUE, // fBinary
FALSE, // fParity
FALSE, // fOutxCtsFlow
FALSE, // fOutxDsrFlow
DTR_CONTROL_ENABLE,// fDtrControl
FALSE, // fDsrSensitivity
FALSE, // fTXContinueOnXoff
FALSE, // fOutX
FALSE, // fInX
FALSE, // fErrorChar
FALSE, // fNull
RTS_CONTROL_ENABLE,// fRtsControl
FALSE, // fAbortOnError
0, // fDummy2
0, // wReserved
0x100, // XonLim
0x100, // XoffLim
8, // ByteSize
NOPARITY, // Parity
ONESTOPBIT, // StopBits
0x11, // XonChar
0x13, // XoffChar
'?', // ErrorChar
0x1A, // EofChar
0x10 // EvtChar
};

HANDLE hComm = NULL;

BOOL Setup ();
BOOL Write (LPVOID, DWORD);
BOOL Read (LPVOID, DWORD);
BOOL WaitAnswer (DWORD, char*);

BOOL Call ();
BOOL HangUp ();

int main(int argc, char* argv[])
{
// ouverture port série
hComm = CreateFile("COM3", GENERIC_READ|GENERIC_WRITE, 0, NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM, NULL);
if(hComm == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed\r\n");
return -1;
}

// config
if(!Setup())
return -1;

if(Call())
{
Sleep(20000);
HangUp();
}

// fermeture port série
CloseHandle(hComm);
return 0;
}

BOOL Write(LPVOID lpBuffer,DWORD dwSize)
{
DWORD dwWritten;
WriteFile(hComm, lpBuffer, dwSize, &dwWritten, NULL);
return (dwSize == dwWritten);
}

BOOL Read(LPVOID lpBuffer,DWORD dwSize)
{
DWORD dwRead;
ReadFile(hComm, lpBuffer, dwSize, &dwRead, NULL);
((LPBYTE)lpBuffer)[dwRead] = '\0';
return (dwRead != 0);
}

BOOL Setup()
{
if(!SetupComm(hComm, RX_SIZE, TX_SIZE))
{
printf("SetupComm failed\r\n");
return FALSE;
}

if(!SetCommTimeouts(hComm, &cto))
{
printf("SetCommTimeouts failed\r\n");
return FALSE;
}

if(!SetCommState(hComm, &dcb))
{
printf("SetCommState failed\r\n");
return FALSE;
}

PurgeComm(hComm, PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
EscapeCommFunction(hComm, SETDTR);
return TRUE;
}

BOOL WaitAnswer(DWORD dwTime, char* szText)
{
char reponse[256];
// tant que le délai n'est pas expiré
DWORD dwLastTime = GetTickCount();
while(GetTickCount() < dwLastTime+dwTime*1000)
{
// pause
Sleep(100);
// lire
if(Read(reponse, sizeof(reponse)))
{
printf(reponse);
if(strstr(reponse, szText) != NULL)
return TRUE;
}
}
return FALSE;
}

BOOL Call()
{
// init
char szInit[] = "AT\r\n";
printf("Init -> %s", szInit);
if(!Write(szInit, strlen(szInit)))
return FALSE;
if(!WaitAnswer(20,"OK"))
{
printf("\r\nEchec\r\n");
return FALSE;
}

// appel
char szCall[] = "ATDxxxxxxxxxx\r\n";
printf("Call -> %s", szCall);
if(!Write(szCall, strlen(szCall)))
return FALSE;
if(!WaitAnswer(20,"CONNECT"))
{
printf("\r\nEchec\r\n");
return FALSE;
}

return TRUE;
}

BOOL HangUp()
{
// +++
char* szCmd1 = "+++";
printf("HangUp -> %s\r\n", szCmd1);
if(!Write(szCmd1, strlen(szCmd1)))
return FALSE;

// ATH
char* szCmd2 = "ATH\r\n";
printf("HangUp ->%s", szCmd2);
if(!Write(szCmd2, strlen(szCmd2)))
return FALSE;

return TRUE;

0
Rejoignez-nous