怎样用c语言驱动1台modem拨通另一台modem?

1e21 2003-09-11 04:54:14
请说明的详细些,或给点思路!
有例子最好了!先谢了!
...全文
35 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Goolin 2003-09-17
  • 打赏
  • 举报
回复
我们可以用别的方法的呀,代理服务器CCProxy能够实现局域网内的机器共享Modem上网
uhml 2003-09-15
  • 打赏
  • 举报
回复
请说明环境,才好对症
1e21 2003-09-15
  • 打赏
  • 举报
回复
各位我要的不是用VC的,是底层方面,有人做过嵌入开发的这类通讯吗?
emmyjeff 2003-09-14
  • 打赏
  • 举报
回复
再给你一个我以前项目做过用AT命令控制modem的类

// Modem.h: interface for the CModem class.
//
//////////////////////////////////////////////////////////////////////

#ifndef __MODEM_H__
#define __MODEM_H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "string"
#include "iostream"
#include "windows.h"
using namespace std;


const int COM_RCV_ERR = -1;
const int COM_RCV_EXCEED_MAX_SIZE = -2;
const int COM_NOT_RCV_COMPLETE = -3;
const int MODEM_SENT_COMMAND_STATE = 0;
const int MODEM_RCV_REPORT_STATE = 1;
const int MODEM_COMMAND_STATE = 0;
const int MODEM_RCV_STATE = 1;
const int MODEM_TRAN_STATE = 2;
const int MODEM_FULL_STATE = 3;

class CModem
{
public:
void record();
int RcvCmdResponse(char *rcv, int size);
void RcvProcessReport();
static void RcvReport(void *p);
bool WaitReport();
int RecieveBytes(char *rcv, int size);
bool SendCommand(const char *command);
bool InitalModem(int com_port);

CModem();
virtual ~CModem();



private:
volatile int m_modemState;
volatile int m_modemSendCmdState;
volatile int m_ringNumber;
HANDLE m_handle;
void SetTimeout();

};

#endif __MODEM_H__



// Modem.cpp: implementation of the CModem class.
//
//////////////////////////////////////////////////////////////////////

#include "Modem.h"

#include <process.h>

CModem::CModem():m_modemSendCmdState(MODEM_RCV_REPORT_STATE)
,m_modemState(MODEM_COMMAND_STATE), m_ringNumber(0)
{

}

CModem::~CModem()
{

}

bool CModem::InitalModem(int com_port)
{
char com[6] = "com";
char port[3] = {0};
itoa(com_port, port, 10);
strcat(com, port);
#ifdef __DEBUG__
cout<<endl<<"====>>port number is: "<<com<<endl;
#endif
m_handle =
CreateFile(com, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM, NULL);
if (m_handle == NULL)
{
cout<<"====>open "<<com<<" failed!"<<endl;
return false;
}
DCB commDCB;
GetCommState(m_handle, &commDCB);
SetTimeout();
return true;
}

void CModem::SetTimeout()
{
COMMTIMEOUTS commTimer;
GetCommTimeouts(m_handle, &commTimer);
commTimer.ReadIntervalTimeout = 500;
commTimer.ReadTotalTimeoutMultiplier = 50;
commTimer.ReadTotalTimeoutConstant = 5000;
commTimer.WriteTotalTimeoutConstant = 5;
commTimer.WriteTotalTimeoutMultiplier = 5;
SetCommTimeouts(m_handle, &commTimer);
}

bool CModem::SendCommand(const char *command)
{
const int MAX_RESEND_TIMES = 3;
int len = strlen(command);
unsigned long sendBytes = 0;
unsigned long totalSendBytes = 0;
int reSendTimes = 0;
while(totalSendBytes < len)
{
if (WriteFile(m_handle, command, len - totalSendBytes, &sendBytes, NULL)
== FALSE)
{
return false;
}
totalSendBytes = totalSendBytes + sendBytes;
reSendTimes++;
if (totalSendBytes < len && reSendTimes == MAX_RESEND_TIMES)
{
return false;
}
}
return true;
}

int CModem::RecieveBytes(char *rcv, int size)
{
char buffer[200] = {0};
bool responseEnd = false;
const int MAX_RESEND_TIMES = 3;
int reRcvTimes = 0;
unsigned long totalRcvBytes = 0;
unsigned long rcvBytes = 0;

//test
static int tests = 0;
tests++;
cout<<"enter this function "<<tests<<"times"<<endl;
//test

if (rcv == NULL)
{
return COM_RCV_ERR;
}
memset(rcv, 0, size);
while(reRcvTimes < MAX_RESEND_TIMES && totalRcvBytes < size)
{
memset(buffer, 0, sizeof(buffer));
BOOL res = ReadFile(m_handle, buffer, size - totalRcvBytes, &rcvBytes, NULL);
if (FALSE == res)
{
DWORD err = GetLastError();
cout<<"ReadFile function failed. Error number is "<<err<<endl;
}
totalRcvBytes += rcvBytes;
reRcvTimes++;
if (totalRcvBytes <= size) //这个地方(原来为totalRcvBytes < size)浪费了我两天时间,切忌!切忌!!
{
memcpy(rcv + totalRcvBytes - rcvBytes, buffer, rcvBytes);
}
else
{
return COM_RCV_EXCEED_MAX_SIZE;
}
}
if (totalRcvBytes == size)
{
return size;
}
return COM_NOT_RCV_COMPLETE;
}

bool CModem::WaitReport()
{
_beginthread(RcvReport, 0, this);
return true;
}

void CModem::RcvReport(void *p)
{
CModem *p_modem = (CModem *)p;
DWORD eventMask = EV_RXCHAR;

BOOL res = SetCommMask(p_modem->m_handle, eventMask);
if (0 == res)
{
DWORD err = GetLastError();
cout<<"SetCommMask function error: "<<err<<endl;
cout<<"p_modem->m_handle = "<<p_modem->m_handle<<endl;
}

p_modem->m_modemSendCmdState = MODEM_RCV_REPORT_STATE;
res = WaitCommEvent(p_modem->m_handle, &eventMask, NULL);//必须在设置掩码后才能调用此函数
if (0 == res)
{
DWORD err = GetLastError();
cout<<"WaitCommEvent function error: "<<err<<endl;
cout<<"p_modem->m_handle = "<<p_modem->m_handle<<endl;
}
if (p_modem->m_modemSendCmdState == MODEM_SENT_COMMAND_STATE) //在发送命令前设置此位
{
return;
}

//调用事件上报处理程序
p_modem->RcvProcessReport();
}

void CModem::RcvProcessReport()
{
char buffer[2] = {0};
int rcvBytes = 0;

rcvBytes = this->RecieveBytes(buffer, sizeof(buffer));
//BOOL res = ReadFile(m_handle, buffer, 2, &rcvBytes, NULL);
//加入例外处理:如果读取失败
//加入例外处理:如果读取失败
if (buffer[0] == char(16) && buffer[1] == 'R')
{
m_ringNumber++;
cout<<"===>>recive "<<rcvBytes<<"===>>recieve ring....."<<endl;

this->RcvReport(this);
}
else
{
if (rcvBytes <= 0)
{
this->RcvReport(this);
}
m_ringNumber = 0;
cout<<"====>>>>>reive>>"<<buffer<<"<<===recieved number is"<<rcvBytes<<endl;
this->RcvReport(this);
}
}

int CModem::RcvCmdResponse(char *rcv, int size)
{
char buffer[200] = {0};
bool responseEnd = false;
const int MAX_RESEND_TIMES = 15;
int reRcvTimes = 0;
unsigned long totalRcvBytes = 0;
unsigned long rcvBytes = 0;

//test
static int tests = 0;
tests++;
cout<<"enter this function "<<tests<<"times"<<endl;
//test

if (rcv == NULL)
{
return COM_RCV_ERR;
}
memset(rcv, 0, size);
while(reRcvTimes < MAX_RESEND_TIMES && totalRcvBytes < size)
{
memset(buffer, 0, sizeof(buffer));
BOOL res = ReadFile(m_handle, buffer, size - totalRcvBytes, &rcvBytes, NULL);
if (FALSE == res)
{
DWORD err = GetLastError();
cout<<"ReadFile function failed. Error number is "<<err<<endl;
}
totalRcvBytes += rcvBytes;
reRcvTimes++;
if (totalRcvBytes <= size) //这个地方(原来为totalRcvBytes < size)浪费了我两天时间,切忌!切忌!!
{
strcat(rcv, buffer);
string str(rcv);
if (str.find("ERROR") != string::npos
|| str.find("OK") != string::npos
|| str.find("CONNECT") != string::npos)
{
return totalRcvBytes;
}
}
else
{
return COM_RCV_EXCEED_MAX_SIZE;
}
}
if (totalRcvBytes == size)
{
return size;
}
return COM_NOT_RCV_COMPLETE;
}

void CModem::record()
{

}
emmyjeff 2003-09-14
  • 打赏
  • 举报
回复
最直接的办法就是用modem的AT命令,具体为:

打开连接modem的串口用(CreateFile(32位win)函数或者OpenComm(16位win)函数)
读取串口的配置DCB
设置串口的配置(如波特率、字节超时、帧超时等)
给modem发送拨号的AT命令(根据modem不同AT命令有所不同,主要有Rockwell和intel芯片两种,网上好多地方有AT命令机集可以下载)
继续用AT命令干其他事情.....
jpyc 2003-09-14
  • 打赏
  • 举报
回复
delphi的不行吗?
http://218.56.11.178:8020/web/index.aspx

-> 下载基地->例程-硬件控制->串口通讯控制器

-``````````->控件-硬件控制->TurboPower Async Professional//这是控件
uhml 2003-09-14
  • 打赏
  • 举报
回复
有个串口类,封装了串口操作,在此基础上,先设置远程和本地MODEM为自动应答方式,然后在本地用AT指令拨号,或者使用通信组件MScomm也行,务必要连接正确才能有结果。
可以参考龚建伟的相关介绍(http://www.gjwtech.com/)

我也有些资料
http://home.91i.net/richardhuang
klbt 2003-09-12
  • 打赏
  • 举报
回复
不清楚,帮你顶。

2,425

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 其他硬件开发
社区管理员
  • 其他硬件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧