c++ 中的调制解调器呼叫

发布于 2024-07-22 21:15:55 字数 4350 浏览 9 评论 0原文

我试图在一端进行调制解调器呼叫,而在另一端程序应答呼叫。 它似乎没有检测到运营商。 我做错了什么吗? 我错过了什么吗?

int main(int argc, char** argv)
{
ParseArgs(argc,argv);

SerialPort* port = SerialPort::New();
if(!port)
    Error(ErrorNoMemory,"Can't create port");

int error = port->Open(PortNum, SendFlag);
if(error)
    Error(error,"Can't open port");

error = port->Initialise(PortBaud);
if(error)
    Error(error,"Can't initialise port");

if(ReceiveFlag)
{
    port->Listen();
    Receive(port); //after the call is stablished I send a file
}
else
{
    port->Dial(PhoneNumber);
     Send(port);
}
port->Close();
delete port;

return 0;
}

打开端口部分:

int Open(unsigned port, bool SendFlag)
{
// make name for port...
char name[] = "\\\\.\\com???.???";
char* nameNumber = name+sizeof(name)-8;
char* nameEnd = nameNumber;
if(port>999){
    return ErrorInvalidPort;
}
if(port>99)
{
    *nameEnd++ = '0'+port/100;
    port %= 100;
}
if(port>9)
{
    *nameEnd++ = '0'+port/10;
    port %= 10;
}
*nameEnd++ = '0'+port;
*nameEnd = 0;

// open port...
    hSerial = CreateFile(name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if(hSerial==INVALID_HANDLE_VALUE)
{
    switch(GetLastError())
    {
        case ERROR_FILE_NOT_FOUND:
            return ErrorInvalidPort;
        case ERROR_ACCESS_DENIED:
            return ErrorPortInUse;
        default:
            return Error();
    }
}

SetupComm( hSerial, 1024, 1024 );
if (!SendFlag)
{
    if (!SetCommMask(hSerial, EV_RING |EV_RLSD))
         // Error setting communications mask
        printf("error mascara");
}
else
{
    if (!SetCommMask(hSerial, EV_RLSD))
    {
        // Error setting communications mask
        printf("error mascara");
    }
}

return 0;
}

初始化部分:

int Initialise(unsigned baud)
{
// flush port...
if(!FlushFileBuffers(hSerial))
    return Error();
if(!PurgeComm(hSerial, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR))
    return Error();

// configure port...
DCB dcb ;
if(!GetCommState(hSerial, &dcb))
    return Error();
dcb.BaudRate    = CBR_115200;
dcb.ByteSize    = 8;
dcb.StopBits    = ONESTOPBIT;
dcb.Parity        = NOPARITY;
if(!SetCommState(hSerial, &dcb))
{
    if(GetLastError()==ERROR_INVALID_PARAMETER)
        return ErrorInvalidSettings;
    return Error();
}

// set timeouts to zero so read/writes return immediately...
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout        = MAXDWORD;
timeouts.ReadTotalTimeoutConstant       = 0;
timeouts.ReadTotalTimeoutMultiplier     = 0;
timeouts.WriteTotalTimeoutConstant      = 0;
timeouts.WriteTotalTimeoutMultiplier= 0;
if(!SetCommTimeouts(hSerial, &timeouts))
    return Error();

// flush port again...
if(!PurgeComm(hSerial, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR))
    return Error();
return 0;
}

拨号部分:

int Dial(char *telefono)
{

unsigned long int  n = 0;
DWORD dwCommEvent;
DWORD bytes;
DWORD dwRead;
char cadena[15];
char  chRead;


sprintf(cadena, "ATDT%s\r", telefono);

if (!WriteFile( hSerial, "ATH1\r", strlen("ATH1\r"), (&(n)), 0 ))
{
    printf("error");
}
FlushFileBuffers( hSerial );
Sleep(1000);

if (!WriteFile( hSerial, cadena, strlen(cadena), (&(n)), 0))
{
    printf("error");
}
FlushFileBuffers( hSerial );
Sleep(10000);

printf("Marcamos");

do
{
    printf("Espero eventos");
    if(WaitCommEvent(hSerial, &dwCommEvent, NULL))
    {
        if(dwCommEvent & EV_RLSD)
        {
            printf("rlsd");
            break;
        }
        else
        {
            printf("otro");
        }
    }
    printf("fin del bucle");
} while(true);



return 0;
}

监听部分:

int Listen()
{
DWORD dwCommEvent;
unsigned long int  n = 0;

do
{
    printf("ESpero eventos");
    if(WaitCommEvent(hSerial, &dwCommEvent, NULL))
    {
        if(dwCommEvent & EV_RING)
        {
            printf("RING");

            if (!WriteFile( hSerial, "ATA\r", strlen("ATA\r"), (&(n)), 0 ))
            {
                printf("error");
            }
            FlushFileBuffers( hSerial );
            break;
        }
        else if (dwCommEvent & EV_RLSD )
        {
            break;
        }
    }
    printf("fin del bucle");
} while(true);
return 0;
}

I'm trying to make a modem call in one end, and in the other end the program answers the call. It doesn't seem to detect the carrier. Am I doing anything wrong? Am I missing something?

int main(int argc, char** argv)
{
ParseArgs(argc,argv);

SerialPort* port = SerialPort::New();
if(!port)
    Error(ErrorNoMemory,"Can't create port");

int error = port->Open(PortNum, SendFlag);
if(error)
    Error(error,"Can't open port");

error = port->Initialise(PortBaud);
if(error)
    Error(error,"Can't initialise port");

if(ReceiveFlag)
{
    port->Listen();
    Receive(port); //after the call is stablished I send a file
}
else
{
    port->Dial(PhoneNumber);
     Send(port);
}
port->Close();
delete port;

return 0;
}

The part of opening the port:

int Open(unsigned port, bool SendFlag)
{
// make name for port...
char name[] = "\\\\.\\com???.???";
char* nameNumber = name+sizeof(name)-8;
char* nameEnd = nameNumber;
if(port>999){
    return ErrorInvalidPort;
}
if(port>99)
{
    *nameEnd++ = '0'+port/100;
    port %= 100;
}
if(port>9)
{
    *nameEnd++ = '0'+port/10;
    port %= 10;
}
*nameEnd++ = '0'+port;
*nameEnd = 0;

// open port...
    hSerial = CreateFile(name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if(hSerial==INVALID_HANDLE_VALUE)
{
    switch(GetLastError())
    {
        case ERROR_FILE_NOT_FOUND:
            return ErrorInvalidPort;
        case ERROR_ACCESS_DENIED:
            return ErrorPortInUse;
        default:
            return Error();
    }
}

SetupComm( hSerial, 1024, 1024 );
if (!SendFlag)
{
    if (!SetCommMask(hSerial, EV_RING |EV_RLSD))
         // Error setting communications mask
        printf("error mascara");
}
else
{
    if (!SetCommMask(hSerial, EV_RLSD))
    {
        // Error setting communications mask
        printf("error mascara");
    }
}

return 0;
}

The initialise part:

int Initialise(unsigned baud)
{
// flush port...
if(!FlushFileBuffers(hSerial))
    return Error();
if(!PurgeComm(hSerial, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR))
    return Error();

// configure port...
DCB dcb ;
if(!GetCommState(hSerial, &dcb))
    return Error();
dcb.BaudRate    = CBR_115200;
dcb.ByteSize    = 8;
dcb.StopBits    = ONESTOPBIT;
dcb.Parity        = NOPARITY;
if(!SetCommState(hSerial, &dcb))
{
    if(GetLastError()==ERROR_INVALID_PARAMETER)
        return ErrorInvalidSettings;
    return Error();
}

// set timeouts to zero so read/writes return immediately...
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout        = MAXDWORD;
timeouts.ReadTotalTimeoutConstant       = 0;
timeouts.ReadTotalTimeoutMultiplier     = 0;
timeouts.WriteTotalTimeoutConstant      = 0;
timeouts.WriteTotalTimeoutMultiplier= 0;
if(!SetCommTimeouts(hSerial, &timeouts))
    return Error();

// flush port again...
if(!PurgeComm(hSerial, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR))
    return Error();
return 0;
}

The dial part:

int Dial(char *telefono)
{

unsigned long int  n = 0;
DWORD dwCommEvent;
DWORD bytes;
DWORD dwRead;
char cadena[15];
char  chRead;


sprintf(cadena, "ATDT%s\r", telefono);

if (!WriteFile( hSerial, "ATH1\r", strlen("ATH1\r"), (&(n)), 0 ))
{
    printf("error");
}
FlushFileBuffers( hSerial );
Sleep(1000);

if (!WriteFile( hSerial, cadena, strlen(cadena), (&(n)), 0))
{
    printf("error");
}
FlushFileBuffers( hSerial );
Sleep(10000);

printf("Marcamos");

do
{
    printf("Espero eventos");
    if(WaitCommEvent(hSerial, &dwCommEvent, NULL))
    {
        if(dwCommEvent & EV_RLSD)
        {
            printf("rlsd");
            break;
        }
        else
        {
            printf("otro");
        }
    }
    printf("fin del bucle");
} while(true);



return 0;
}

The listening part:

int Listen()
{
DWORD dwCommEvent;
unsigned long int  n = 0;

do
{
    printf("ESpero eventos");
    if(WaitCommEvent(hSerial, &dwCommEvent, NULL))
    {
        if(dwCommEvent & EV_RING)
        {
            printf("RING");

            if (!WriteFile( hSerial, "ATA\r", strlen("ATA\r"), (&(n)), 0 ))
            {
                printf("error");
            }
            FlushFileBuffers( hSerial );
            break;
        }
        else if (dwCommEvent & EV_RLSD )
        {
            break;
        }
    }
    printf("fin del bucle");
} while(true);
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫南 2024-07-29 21:15:55

You might have an easier time of it using Microsoft's Telephony API (TAPI) rather than trying to talk to the modem directly.

黯然#的苍凉 2024-07-29 21:15:55

您可能想要尝试在接收端发出 ATS0=1 并让调制解调器自动应答(如果您愿意,可以用您想要的振铃次数代替 1)。

您可能会发现智能调制解调器并不总是通过环路 (EV_RING) 信号。

编辑:当您不希望调制解调器再自动应答时,请不要忘记“ATS0=0”。

You may want to try issuing an ATS0=1 on the receiving side and let the modem autoanswer (substitute the number of rings you want instead of 1 if you wish).

You may discover that smart modems don't always pass through the ring (EV_RING) signal.

EDIT: Don't forget to 'ATS0=0' when you don't want the modem to auto-answer any more.

哑剧 2024-07-29 21:15:55

也许您需要在应答中应用 A0 和/或 S0=1 Hayes 命令边。

Maybe you need to apply the A0 and / or S0=1 Hayes command on the answering side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文