请教使用spcomm发送短信
请教使用spcomm发送短信:
我这两天使用spcomm发送短信,短信猫Wavecom,使用AT指令。代码如下。
问题:
执行后手机能够收到短信,但程序收不到猫的返回值。在OnReceive中,BufferLength=1,Buffer[0]=0x1a。另外,若读取猫中卡号,使用AT指令:AT+CNUM,还是一样。
若用超级终端,则可正常发送与收到返回值等。
//test code
//send
void __fastcall TForm1::SendBtnClick(TObject *Sender)
{
AnsiString strCmd , strRet;
CommCheck->PortOpen = false ;
CommCheck->CommName = "COM6";
CommCheck->PortOpen = true ;
if(CommCheck->PortOpen)
{
//Edit1: mobilephone number
//Memo2: sms content
char buf[]={0x1a,0x00};
strCmd.sprintf("AT+CMGS=%s\r%s\r",Edit1->Text.Trim().c_str(),Memo2->Text.c_str());
CommCheck->WriteCommData(strCmd.c_str(),strCmd.Length());
Sleep(10);
CommCheck->WriteCommData(buf,1);
Sleep(10);
//waiting for smscat return OK or Error
//OnReceive() 得到返回值....
Memo1->Lines->Add(CommCheck->CommName+": send sms, phone num:,"+Edit1->Text.Trim());
}
else
{
Memo1->Lines->Add(CommCheck->CommName+": open fail!");
}
}
//---------------------------------------
//receive from smscat
void __fastcall TForm1::CommCheckReceiveData(TObject *Sender, PChar Buffer,
WORD BufferLength)
{
int n = BufferLength ;
AnsiString re = AnsiString(Buffer);
}
//spcom setting
object CommCheck: TComm
PortOpenError = 'Error opening serial port'
CommName = 'COM6'
BaudRate = 115200
RtsControl = RtsEnable
XonLimit = 500
XoffLimit = 500
ByteSize = _8
Parity = None
StopBits = _1
XonChar = #17
XoffChar = #19
ReplacedChar = #0
ReadIntervalTimeout = 100
ReadTotalTimeoutMultiplier = 0
ReadTotalTimeoutConstant = 0
WriteTotalTimeoutMultiplier = 0
WriteTotalTimeoutConstant = 0
OnReceiveData = CommCheckReceiveData
Left = 648
Top = 120
end
------最佳解决方案--------------------
我用MSComm做的很正常,建议你用阻塞方式更可靠些。
[其他解释]
http://www.doc88.com/p-20255404195.html
[其他解释]