hoty | Voila, ce n'est pas de la super programmation modulaire, mais ca marchait.
theDialog c'est un pointeur sur une fenetre qui permettait d'afficher des messages.
Deux modems etaient supportes :
ManagePortWaveCom
ManagePortPremiCell (Nokia)
SMSLock c'est la declartion d'une entree dans une section critique (protection de la queue de message).
MsgQueue c'etait une CStringList.
Pour poster un message, le programme faisait :
Code :
- SMSLock L;
- if (urgent)
- MsgQueue.AddTail(msg);
- else
- MsgQueue.AddHead(msg);
|
et un thread executant une des deux fct ManagePort... lisait la queue et envoyait le message :
_beginthread(ManagePort...,2048,port);
Enfin, tu peux lancer plusieur thread, un par port.
Code :
- #include "stdafx.h"
- HANDLE Ports[MAXPORT];
- bool isReady[MAXPORT];
- UINT SMSSent;
- UINT SMSSending;
- UINT PortError = 0;
- extern CString cfgPIN; // PIN de la carte SIM
- bool InitPort(UINT port)
- {
- CString wrk;
- //
- wrk.Format("COM%d",port);
- Ports[port] = CreateFile(wrk,
- GENERIC_READ|GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL );
- if ( Ports[port] == INVALID_HANDLE_VALUE )
- theDialog->Display(" error opening: %d",GetLastError());
- else
- {
- COMMTIMEOUTS scTO;
- DCB dcb;
- //
- memset (&scTO,0,sizeof(scTO));
- scTO.ReadIntervalTimeout = 250;
- scTO.ReadTotalTimeoutMultiplier = 2000;
- scTO.ReadTotalTimeoutConstant = 1000;
- scTO.WriteTotalTimeoutMultiplier = 2000;
- scTO.WriteTotalTimeoutConstant = 1000;
- SetCommTimeouts (Ports[port], &scTO);
- //
- if (GetCommState(Ports[port], &dcb))
- {
- dcb.BaudRate = CBR_9600; // set the baud rate
- dcb.ByteSize = 8; // data size, xmit, and rcv
- dcb.Parity = NOPARITY; // no parity bit
- dcb.StopBits = ONESTOPBIT; // one stop bit
- if (SetCommState(Ports[port], &dcb))
- {
- return true;
- }
- else
- {
- theDialog->Display(" error setting comport: %d",GetLastError());
- CloseHandle(Ports[port]);
- Ports[port] = INVALID_HANDLE_VALUE;
- }
- }
- else
- {
- theDialog->Display(" error getting comport: %d",GetLastError());
- CloseHandle(Ports[port]);
- Ports[port] = INVALID_HANDLE_VALUE;
- }
- }
- return false;
- }
- // text must be 4 characters long
- bool WaitForOk4(HANDLE handle, const char * text)
- {
- char wrk[8];
- DWORD nb;
- CString Win;
- //
- for (;;)
- {
- if (ReadFile( handle, wrk, 4 , &nb, NULL))
- {
- wrk[nb] = 0;
- Win += wrk;
- if (Win.Find(text) != -1) return true;
- if (nb==0) return false;
- if (Win.GetLength()>4)
- Win.Delete(0,Win.GetLength()-4);
- }
- else
- return false;
- }
- }
- // text must be 2 characters long
- bool WaitForOk2(HANDLE handle, const char * text)
- {
- char wrk[8];
- DWORD nb;
- CString Win;
- //
- for (;;)
- {
- if (ReadFile( handle, wrk, 2 , &nb, NULL))
- {
- wrk[nb] = 0;
- Win += wrk;
- if (Win.Find(text) != -1) return true;
- if (nb==0) return false;
- if (Win.GetLength()>2)
- Win.Delete(0,Win.GetLength()-2);
- }
- else
- return false;
- }
- }
- void _cdecl ManagePortPremiCell(void * p)
- {
- UINT port = (UINT)p;
- DWORD nb;
- CString Number,Msg;
- //
- if (!InitPort(port)) return;
- //
- for (;;) // Reset loop
- {
- // Exit previous session
- Sleep(2000);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "EXIT\r", 5, &nb, NULL);
- //
- // Reset box
- Sleep(2000);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "atz\r", 4, &nb, NULL);
- //
- // Enter SMS mode
- Sleep(3000);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "at*c\r", 5, &nb, NULL);
- //
- if (!WaitForOk4(Ports[port],"SMS>" ))
- {
- theDialog->Display("#%d: Error entering SMS mode (not a PremiCell?). Will retry in 30s.",port);
- CloseHandle(Ports[port]);
- Ports[port] = INVALID_HANDLE_VALUE;
- //
- Sleep (30000);
- if (!InitPort(port)) return;
- continue;
- }
- //
- isReady[port] = true;
- //
- for (;;) // Message loop -------------------------------------------------------
- {
- // Wait for something in the queue
- theDialog->Display("#%d: Ready.",port);
- for (BOOL gotMessage=FALSE; !gotMessage;)
- {
- Sleep (200);
- {
- SMSLock L;
- if (!MsgQueue.IsEmpty())
- {
- // Retrieves message from the queue and increments counter
- gotMessage = TRUE;
- SMSSending++;
- Msg = MsgQueue.GetHead();
- MsgQueue.RemoveHead();
- }
- }
- }
- //
- // Msg format: "06060606;Message text"
- Number = Msg.SpanExcluding(";" );
- Msg.Delete(0,Number.GetLength()+1);
- if (Msg.GetLength()>150)
- Msg.Delete(150,Msg.GetLength());
- //
- theDialog->Display("#%d: \"%s\" to %s",port,Msg,Number);
- //
- // Sending message
- bool MsgSent = false;
- WriteFile( Ports[port], "SEND\r", 5, &nb, NULL);
- if (WaitForOk4(Ports[port],"NO):" ))
- {
- // Send number
- Sleep(15);
- WriteFile( Ports[port], Number+"\r", Number.GetLength()+1, &nb, NULL);
- if (WaitForOk4(Ports[port],"IT):" ))
- {
- // Send message
- Sleep(15);
- WriteFile( Ports[port], Msg+"\x1a", Msg.GetLength()+1, &nb, NULL);
- if (WaitForOk4(Ports[port],"SMS>" ))
- MsgSent = true;
- }
- }
- //
- if (MsgSent)
- {
- SMSLock L; // Success
- SMSSent++;
- SMSSending--;
- }
- else
- break;
- } // for next message ----------------------------------------------------------
- //
- // Repost the current message
- {
- AddMsgInQueue(Number+";"+Msg,true);
- //
- SMSLock L;
- SMSSending--;
- PortError++;
- }
- // Reset
- theDialog->Display("#%d: Error sending message. Reposting and reseting.",port);
- Sleep(5000);
- //
- } // for reset
- }
- void _cdecl ManagePortWaveCom(void * p)
- {
- UINT port = (UINT)p;
- DWORD nb;
- CString Number,Msg;
- //
- if (!InitPort(port)) return;
- //
- // Reset box ------------------------------------------------
- Sleep(2000);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "atz\r", 4, &nb, NULL);
- if (!WaitForOk2(Ports[port],"OK" ))
- {
- theDialog->Display("#%d: Error reseting",port);
- return;
- }
- //
- // Enter PIN
- Sleep(2000);
- WriteFile( Ports[port], "AT+CPIN?\r", 9, &nb, NULL);
- if (!WaitForOk4(Ports[port],"EADY" ))
- {
- Sleep(500);
- WriteFile( Ports[port], "AT+CPIN?\r", 9, &nb, NULL);
- if (WaitForOk4(Ports[port]," PIN" ) && cfgPIN.GetLength()==4)
- {
- Sleep(500);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "AT+CPIN="+cfgPIN+"\r", 13, &nb, NULL);
- if (!WaitForOk2(Ports[port],"OK" ))
- {
- theDialog->Display("#%d: Error entering PIN code",port);
- return;
- }
- }
- else
- {
- theDialog->Display("#%d: Error SIM",port);
- return;
- }
- }
- //
- for (;;) // Reset loop --------------------------------------
- {
- // Exit previous session
- Sleep(2000);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "\x1a\r", 2, &nb, NULL);
- //
- // Reset box
- Sleep(2000);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "atz\r", 4, &nb, NULL);
- //
- // Enter SMS text mode
- Sleep(3000);
- PurgeComm( Ports[port], PURGE_TXCLEAR|PURGE_RXCLEAR);
- WriteFile( Ports[port], "AT+CSDH=1\r", 10, &nb, NULL);
- //
- isReady[port] = true;
- //
- for (;;) // Message loop -------------------------------------------------------
- {
- // Wait for something in the queue
- theDialog->Display("#%d: Ready.",port);
- for (BOOL gotMessage=FALSE; !gotMessage;)
- {
- Sleep (200);
- {
- SMSLock L;
- if (!MsgQueue.IsEmpty())
- {
- // Retrieves message from the queue and increments counter
- gotMessage = TRUE;
- SMSSending++;
- Msg = MsgQueue.GetHead();
- MsgQueue.RemoveHead();
- }
- }
- }
- //
- // Msg format: "06060606;Message text"
- Number = Msg.SpanExcluding(";" );
- Msg.Delete(0,Number.GetLength()+1);
- if (Msg.GetLength()>150)
- Msg.Delete(150,Msg.GetLength());
- //
- theDialog->Display("#%d: \"%s\" to %s",port,Msg,Number);
- //
- // Sending message
- bool MsgSent = false;
- WriteFile( Ports[port], "AT+CMGS="+Number+"\r", 8+Number.GetLength()+1, &nb, NULL);
- if (WaitForOk2(Ports[port],"\n>" ))
- {
- // Send message
- Sleep(15);
- WriteFile( Ports[port], Msg+"\x1a", Msg.GetLength()+1, &nb, NULL);
- if (WaitForOk2(Ports[port],"OK" ))
- MsgSent = true;
- }
- //
- if (MsgSent)
- {
- SMSLock L; // Success
- SMSSent++;
- SMSSending--;
- }
- else
- break;
- } // for next message ----------------------------------------------------------
- //
- // Repost the current message
- {
- AddMsgInQueue(Number+";"+Msg,true);
- //
- SMSLock L;
- SMSSending--;
- PortError++;
- }
- // Reset
- theDialog->Display("#%d: Error sending message. Reposting and reseting.",port);
- Sleep(5000);
- //
- } // for reset
- }
|
|