
KurpeusLondon
|
A tout hasard, je post mon code : ======================== Main File =======================
#include <winsock2.h> #include <windows.h> #pragma comment(lib,"ws2_32.lib")
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include "crc.h" #include "sendUdpTrap.c"
/*============================================================================= Définition de constantes =============================================================================*/ #define RX_SIZE 1024 /* taille tampon d'entrée */ #define TX_SIZE 1024 /* taille tampon de sortie */ #define MAX_WAIT_READ 5000 /* temps max d'attente pour lecture (en ms) */
const char * readOnly = "r"; const char * writeOnly = "w"; const char * readWrite = "a+";
int portCom = 8;
/*============================================================================= Variables globales. =============================================================================*/
/* Handle du port COM ouvert */ HANDLE g_hCOM = NULL;
/* Délais d'attente sur le port COM */ COMMTIMEOUTS g_cto = { -1, /* ReadIntervalTimeOut */ 0, /* ReadTotalTimeOutMultiplier */ 50, /* ReadTotalTimeOutConstant */ 5, /* WriteTotalTimeOutMultiplier */ 50 /* WriteTotalTimeOutConstant */ };
/* Configuration du port COM */ DCB g_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 */ 28672, /* XonLim */ 7168, /* XoffLim */ 8, /* ByteSize */ NOPARITY, /* Parity */ ONESTOPBIT, /* StopBits */ 0x11, /* XonChar */ 0x13, /* XoffChar */ '?', /* ErrorChar */ 0x1A, /* EofChar */ 0x10 /* EvtChar */ };
/*============================================================================= Fonctions du module. =============================================================================*/ BOOL OpenCOM (int nId); BOOL CloseCOM (); BOOL ReadCOM (void* buffer, int nBytesToRead, int* pBytesRead); BOOL WriteCOM (void* buffer, int nBytesToWrite, int* pBytesWritten); void initCom (int BaudRate, int fRtsControle, int ByteSize, int Parity, int StopsBits, char XonChar, char XoffChar, char ErrorChar, char EovChar, char EvtChar); unsigned char * CRC16 ( unsigned char puchMsg, unsigned short usDataLen ); void freeMem(struct sTop * pTop); void initStruct(struct sTop* pTop); int sendTrap(char * pBufferOut);
struct sTop{ struct sData * u12Main; struct sData * u23Main; struct sData * u31Main; struct sData * v1Main; struct sData * v2Main; struct sData * v3Main; struct sData * freqMain; struct sData * u12Gen; struct sData * u23Gen; struct sData * u31Gen; struct sData * v1Gen; struct sData * v2Gen; struct sData * v3Gen; struct sData * freqGen; };
struct sData{ char * label; unsigned int value; char frame[9]; /* Modbus communication use addr "0" to broadcast and 1 to 247 to address a node. 248 to 255 are reserved addresses. To be convert in Hex */ /* According to the ATI100 transfer panel, broadcast adresse only usable for writing functions (6 and 16). */ /* ModBus define two function to compute the CRC : RTU and ASCII. ATI100 use RTU */ };
char tableHex[14] = { 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12, 0x14, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7A, 0x7C}; char tableLabel[14][9]= {"u12Main","u23Main","u31Main","v1Main","v2Main","v3Main","freqMain","u12Gen","u23Gen","u31Gen","v1Gen", "v2Gen","v3Gen","freqGen"};
struct sData * createStruct(unsigned int code, char * label);
int main() {
/* variables locales */ char bufferIn[16]; // char * pBufferIn;
int idReq = 14, nBytesWritten, nBytesRead; int * pItemOut = NULL; int * pItemIn = NULL; int crcError = 1; unsigned int valueItem;
(......)
======================== File sendUdpTrap==================
#include <winsock2.h> #include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
int sendTrap(char * pBufferOut) { WSADATA wsa; WSAStartup(MAKEWORD(2,0),&wsa);
SOCKET sock; SOCKADDR_IN sin;
char ip[15]; int port;
sprintf("%s",ip, "192.168.100.20"); port = 162;
sin.sin_family=AF_INET; sin.sin_addr.s_addr=inet_addr(ip); sin.sin_port=htons(port);
sock=socket(AF_INET,SOCK_DGRAM,0);//On initialise le socket avec SOCK_DGRAM pour dire qu'on est en UDP bind(sock,(SOCKADDR*)&sin,sizeof(sin));
memset(pBufferOut,0,sizeof(pBufferOut));
sendto(sock,pBufferOut,sizeof(pBufferOut),0,(SOCKADDR*)&sin,sizeof(sin)); //sendto envoie des packets udp, ses parametres : // 1 : le socket avec lequel on envoie // 2 : la chaine a envoyer // 3 : la taille de cette chaine // 4 : ??? chais po // 5 : les parametre (convertis en structure SOCKADDR // 6 : la taille de la structure SOCKADDR_IN
return 0; }
|