//---------------------------------------------------------------------------
#pragma hdrstop
#include <vcl.h>
#include "Sms_Send.h"
#include "Main_Form.h"
#include "DataModule.h"
#include "frmHint.h"
#include "frmSample.h"
#include "vector.h"
using std::vector;
//---------------------------------------------------------------------------
#pragma package(smart_init)
//定时抄表 pCriticalSection Sample_all_lock; //避免两次定时采集时间过短,同时进入采集线程
pCriticalSection Sample_one_lock; //一次数据采集,对串口的写互斥
bool Main_Terminate_sms;
bool Main_Terminate_Data;
SMS_STRUCT Sms_Var; //等待发送的短信
CUR_METER_STRUCT Cur_Meter_Info; //记录处理的当前表的信息
CUR_METER_STRUCT Old_Meter_Info;
unsigned short Modem_Rtn_ID; // 返回的Modem结果码
AnsiString smsc_addr; // 短信中心地址
AnsiString SMS_index; //短信的索引号
const bool if_mul = false; //是否是抄多表的标志
bool Process_State = FREE; // MSCommu控件的接收处理状态
unsigned short error_code = 0; // 出错代码
bool Modem_Connect_Status = false; //modem状态变量,是否已经连接
bool receive_wait = false; //由定时器接收还是由主程序接收
bool Have_Get_CSCA = false; //没有能够读取短信中心的值
unsigned char wait_for_send_sms_count = 0; //等待发送的短信的条数
sms_arr_Struct sms_arr;
//------------------------------------------------------------------------------
//函数名称:Uart_TC35_Send_SM
//功 能: 发送短信,阻塞调用方式
//入口参数:void
//
//返回值:
// void
//------------------------------------------------------------------------------
bool __fastcall Uart_TC35_Send_SM(void)
{
AnsiString str_tmp;
try{
//测试是否支持CMGS 发送指令
while(Process_State == BUSY) Sleep(100);
FormMain->Modem_TC35->Output = (AnsiString)"AT+CMGS=?\r";
if(Process_TC35_Return_Data(RTN_OK) == false) {
if(Main_Terminate_Data == true) {
System_Hint_Item("短信发送操作被中止");
}
error_code = 900;
return false; //如果不支持则返回失败
}
//发送AT+CMGS=数量
str_tmp = "AT+CMGS=";
if((Sms_Var.tpdu_len /100) != 0) {
str_tmp += " ";
str_tmp[str_tmp.Length()] = ((Sms_Var.tpdu_len/100)%10)+0x30; // 如果有百位则发送百位
}
if((Sms_Var.tpdu_len /10) != 0) {
str_tmp += " ";
str_tmp[str_tmp.Length()] = ((Sms_Var.tpdu_len/10)%10)+0x30; // 如果有十位则发送十位
}
if((Sms_Var.tpdu_len) != 0) {
str_tmp += " ";
str_tmp[str_tmp.Length()] = (Sms_Var.tpdu_len%10) + 0x30; // 添加个位数
}
str_tmp += '\r';
while(Process_State == BUSY) Sleep(100);
FormMain->Modem_TC35->Output = str_tmp; //发送AT+CMGS=LEN\r
if(Process_TC35_Return_Data(SMS_SEND_PROMPT) == false) {
if(Main_Terminate_Data == true) {
System_Hint_Item("短信发送操作被中止");
}
error_code = 901;
return false;
} //等待回应发送提示符">"的返回
str_tmp = Sms_Var.smsc_code;
str_tmp += Sms_Var.call_phone_code;
str_tmp += Sms_Var.sms_message;
while(Process_State == BUSY) Sleep(100);
FormMain->Modem_TC35->Output = str_tmp;
if(Process_TC35_Return_Data(SMS_SEND_OK) == false) {
if(Main_Terminate_Data == true) {
System_Hint_Item("短信发送操作被中止");
}
error_code = 902;
return false;
}
else return true; //返回是否发送成功
}
catch(...) { return false;}
}
//------------------------------------------------------------------------------
//函数名称:SMS_Set_smsc_addr
//功 能: 设置发送短信的短信中心地址
//入口参数:void
//
//返回值:
// void
//------------------------------------------------------------------------------
void __fastcall SMS_Set_smsc_addr(AnsiString smsc_code)
{
Sms_Var.smsc_code = "0891";
Sms_Var.smsc_code += smsc_code;
}
//------------------------------------------------------------------------------
//函数名称:SMS_Set_smsc_addr
//功 能: 设置发送短信的短信中心地址
//入口参数:void
//
//返回值:
// void
//------------------------------------------------------------------------------
void __fastcall SMS_Set_call_phone(AnsiString call_phone_no)
{
AnsiString str_tmp;
Sms_Var.call_phone_code = "0103";
Sms_Var.call_phone_code += "0D91";
Sms_Var.call_phone_code += "68";
//首先去除电话号码的前后空格
call_phone_no = call_phone_no.Trim();
// 开始把电话号码转变为指定格式
for(int i=1;i<= call_phone_no.Length();i+=2)
{
if(i+1 > call_phone_no.Length()) str_tmp += 'F';
else str_tmp += call_phone_no[i+1];
str_tmp += call_phone_no[i];
}
Sms_Var.call_phone_code += str_tmp;
Sms_Var.call_phone_code += "3208"; //说明短信是UCS2编码方式,短信是属于CLASS3的
} //以后下位机短信要直接往串口里扔,所以要把这里短信类设成3 321B
//------------------------------------------------------------------------------
//函数名称:SMS_Set_smsc_addr
//功 能: 设置发送短信的短信中心地址
//入口参数:void
//
//返回值:
// void
//------------------------------------------------------------------------------
void __fastcall SMS_Set_tpdu(WideString message)
{
Sms_Var.sm_len = message.Length()*2;
Sms_Var.sms_message = "";
Sms_Var.sms_message += ValToAnsi(Sms_Var.sm_len,2);
for(unsigned short i = 1;i<= message.Length(); i++)
{
Sms_Var.sms_message += ValToAnsi(message[i],4);
}
char ch = 0x1a; //ctrl+z
Sms_Var.sms_message += ch;
}
//------------------------------------------------------------------------------
//函数名称:SMS_Cal_tpdu_len
//功 能: 计算短信的TPDU的长度
//入口参数:void
//
//返回值:
// void
//------------------------------------------------------------------------------
void __fastcall SMS_Cal_tpdu_len(void)
{
Sms_Var.tpdu_len = 14 + Sms_Var.sm_len;
}
//---------------------------------------------------------------------------
//函数名称:Get_CSCA
//功 能: 读取短信中心的地址
//入口参数:void
//
//返回值:
// void
//------------------------------------------------------------------------------
bool __fastcall Get_CSCA(void)
{
if(Have_Get_CSCA == false) {
while(Process_State == BUSY) Sleep(100);
try{FormMain->Modem_TC35->Output = (AnsiString)"AT+CSCA?\r";
Have_Get_CSCA = Process_TC35_Return_Data(SMSC_ADDR);
}
catch(...) { Have_Get_CSCA = false; }
}
return Have_Get_CSCA;
}
// -------------- 短信处理函数结束----------------------------------------------
//------------------------------------------------------------------------------
// ------------------------采用线程模式发送短信---------------------------------
void Send_Sms_Thread(AnsiString phone_no,AnsiString message,AnsiString mes)
{
CUR_METER_ARG_STRUCT *my_sms = new CUR_METER_ARG_STRUCT;
my_sms->phone_no = phone_no;
my_sms->message = message;
my_sms->Mes = mes;
DWORD tid;
void *(*func)(void *) = Send_Sms;
pthread_create(&tid, NULL, func, (void *)my_sms); //采用线程发送短信命令
}
//---------------------------------------------------------------------------
//发送短信,先把各种参数都设定好之后
void *Send_Sms(void *arg)
{
CUR_METER_ARG_STRUCT *sms = (CUR_METER_ARG_STRUCT *) arg;
Critical region(&Sample_one_lock);
bool enter = false;
wait_for_send_sms_count ++;
System_Message(4,wait_for_send_sms_count);
while(Main_Terminate_sms == false)
{
if(region.tryenter() == true) {
- 1
- 2
- 3
- 4
前往页