SMS开发,请大家多多支持,多提宝贵意见,见者有分(300分)

  • 主题发起人 LanFairy
  • 开始时间
电子商务的定义


◆IBM 定义:
电子商务=Web+企业业务。
◆HP 定义:
通过电子化的手段来完成商业贸易活动的一种方式。
◆Sybase 定义:
电子商务可以用二维坐标系来表示:前端和后端应用组成其中一个坐标轴,“企业到企业”和“企业到顾客”组成另一个标轴,而所有的产品都是这个坐标系中的点。
◆Intel 定义:
电子商务=电子化的市场+电子化的交易+电子化的服务。
◆GE 定义:
电子商务乃任何商务交易形式或商务信息交流通过电子信息高速公路运行或动作。而信息高速公路泛指一切电子信息传递网络。
 
其实大家都是需要一个这样的开发接口,可惜是免费的不好找。
 
作者:redpower
出处:


手机短信息SMS开发—编码,解码
1、 英文编码
缺省的GSM字符集为7位编码,ASCII码为8位编码,编码就是将8位ASCII编码转换为7位编码。
例如:1234 编码后得到31D98C06
2进制表示
8位编码 00110001 00110010 00110011 00110100
7位编码 00110001 11011001 10001100 00000110
通过例子可以看出,将ascii8位编码的Bit8去掉,依次将下7位编码的后几位逐次移到前面,形成新的8位编码。
以下是C++Builder的实现代码:
String __stdcall EncodeEnglish(String InputStr)
{
int n,len,cur;
String tempstr,returnstr;
unsigned char mid1[2],mid2[2];
len=InputStr.Length();
n=0;
for(int i=1;i<=len;i++)
{
if (i<len)
{
strcpy(mid1,InputStr.SubString(i,1).c_str());
strcpy(mid2,InputStr.SubString(i+1,1).c_str());
cur=(mid1[0]>>n)|((mid2[0]<<(7-n))&amp;
0xff);
}
else

{
strcpy(mid1,InputStr.SubString(i,1).c_str());
cur=(mid1[0]>>n)&amp;
0x7f;
}
FmtStr(tempstr,"%2.2X",ARRAYOFCONST((cur)));
returnstr=returnstr+tempstr;
n=(n+1)%7;
if (n==0)
i++;
}
return returnstr;
}
2、 英文解码
简单地说就是将7位字符编码转换为8为字符编码
以下是C++Builder的实现代码:
int ReturnHex(int Value)
{
switch (Value)
{
case 0:
Value=0x7f;
break;
case 1:
Value=0x3f;
break;
case 2:
Value=0x1f;
break;
case 3:
Value=0x0f;
break;
case 4:
Value=0x07;
break;
case 5:
Value=0x03;
break;
case 6:
Value=0x01;
break;
case 7:
Value=0x00;
break;
}
return Value;
}
String __stdcall DecodeEnglish (String InputStr)
{
unsigned char InStr[300];
char OutStr[300];
String str;
int j=0,i=0;
int Point=0;
int temp;
memset(InStr,0,301);
memset(OutStr,0,301);
for(int i=0;i<InputStr.Length();i=i+2)
{
str="0x"+InputStr.SubString(i+1,2);
InStr[i/2]=StrToInt(str);
}
while(j<=InputStr.Length()/2)
{
if(Point==0)
OutStr=InStr[j]&amp;ReturnHex(Point);
else

OutStr=((InStr[j]&amp;ReturnHex(Point))<<Point)|(InStr[j-1]>>(8-Point));
if(Point%7==0&amp;&amp;Point!=0)
Point=0;
else

Point=Point+1;
i++;
j=i-(i/8);

}
OutStr[12]=((InStr[12]&amp;0x07)<<5)|(InStr[11]>>(8-5));
return AnsiString(OutStr);
}
3、 中文编码
中文编码较为简单,就是将GB2312的中文编码转换为代码页为CP936的Unicode编码即可
以下是C++Builder的实现代码
String EncodeChinese(String InputStr)
{
int cur;
String tempstr,returnstr;
WideString ws;
wchar_t mid[2];
ws=WideString(InputStr);
for(int i=1;i<=ws.Length();i++)
{
wcscpy(mid,ws.SubString(i,1).c_bstr());
cur=mid[0];
FmtStr(tempstr,"%4.4X",ARRAYOFCONST((cur)));
returnstr=returnstr+tempstr;
}
return returnstr;
}
4、 中文解码
将代码页为CP936的Unicode编码转换为GB2312的中文编码即可

以下是C++Builder的实现代码
String DecodeChinese(String InputStr)
{
wchar_t Buf[300];
for(int i=0;i<InputStr.Length();i=i+4)
{
Buf[i/4]=StrToInt("0x"+InputStr.SubString(i+1,4));
}
Buf[InputStr.Length()/4]=0;
return WideCharToString(Buf);
}
ChangNing(Redpower)
changning@telekbird.com.cn
2001-8-7
 
作者:11830
出处:


用Delphi发送SMS(手机短消息服务)
下面的例子演示了如何通过SOAP来调用一个SMS的WEB服务。
首先通过下面的地址下载并安装微软SOAP工具包2.0:
http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/001/580/msdncompositedoc.xml
然后就可以使用下面的简短代码发送SMS了。
uses
ComObj;

procedure TForm1.Button1Click(Sender: TObject);
var
SoapClient: OleVariant;
v: OleVariant;
begin

SoapClient := CreateOleObject('MSSOAP.SoapClient');
SoapClient.mssoapinit('http://sal006.salnetwork.com:83/lucin/smsmessaging/process.xml');
SoapClient.SendTextMessage('Number', 'MessageBody', 'Sender');
end;

其中Number为国家代码+电话号码(不需要以零打头),
如 06 12345678 在荷兰:31612345678
 
短信息服务?short message service?
简称:SMS?特点是能让移动电话用户相互发送简短的文字信息。
SMS最初是为技术人员设计的信息服务,它所具有的灵活性使它不仅可处理文字,
还能处理其他二进制数据,如发送电话铃声等。它还支持信息传播,这为第三方服务
打开了大门,如股票报价或发布体育比赛的得分等,只要营运商同意交换SMS信息,
用户能低价向全球发送信息,也可以在电话与ICQ等以互联网为基础的信息服务之间
发送消息。
由于SMS的多份拷贝的能力,很容易发生同时向大量移动电话用户发送垃圾邮件的情况。
一封针对某人的SMS炸弹可能带给他来自营运商的额外收费,使他的电话死机。事实上
较为先进的电话显然容易受到攻击:去年,日本的NTT移动通讯的i-mode系统就受到了
一种负载在信息上的病毒的攻击,这种病毒会在用户不知道的情况下拨通急救号码。
随着移动电话更加先进,使用更广泛,它们成了更受骇客青睐的目标。
据Trend Micro Inc. ?J.TDM?的防病毒研究负责人乔.哈特曼?Joe Hartman?说,
病毒作者已经相互挑战,看谁先编写出移动电话病毒。
 
GPRS是通用分组无线业务(General Packet Radio Service)的简称,
它突破了GSM网只能提供电路交换的思维方式,只通过增加相应的功能实体和对现有的
基站系统进行部分改造来实现分组交换,这种改造的投入相对来说并不大,但得到的用户
数据速率却相当可观。
GPRS手机的分类?
类别A:GPRS分组业务和GSM话音业务可以同时进行;
类别B:可用于GPRS分组业务和GSM话音业务,但两者不能同时工作;
类别C:只能用于分组业务。
A类手机能进行业务切换:PS->CS->PS, PS指数据业务,CS指话音业务。
例如,当A类手机在数据传送期间,用普通手机拨打A类手机,A类手机应能应答呼叫
并通话,通话过程中继续保持数据传送。
B类手机也能进行业务切换:PS->CS->PS。
类似地,当B类手机在数据传送期间,普通手机也可拨打B类手机,B类手机会有相应
的提示,应答后就切换至话音业务,而数据业务则被悬置,待话音业务结束后,又可
切换回数据业务.
 
http://www.bytesworld.com/webpage/technology/gprs.htm
 
哇,好深奥呀,过瘾,请继续。
 
to lyywy,
你的例子在运行到SendTextMessage会出错~~
 
最近也在搞这些东东,不过用的是GSM MODEM之类的进行发送。
贴出一个让大家参考参考!!![:)]

unit SMSDriver_Commdrv;
interface
uses
Windows, Messages,Classes,SysUtils,Forms;
//for delphi5
// Windows, Messages,Classes,SysUtils;
//for delphi6 because func AllocateHwnd
// now defined in Classes too. So If use delphi6 ,need not include 'Forms'.
const
// messages from read/write threads
PWM_GOTCOMMDATA = WM_USER + 1;
PWM_RECEIVEERROR = WM_USER + 2;
PWM_REQUESTHANGUP = WM_USER + 3;
PWM_MODEMSTATECHANGE = WM_USER + 4;
type
TParity = ( None, Odd, Even, Mark, Space );
TStopBits = ( sb_1, sb_1_5, sb_2 );
TByteSize = ( db_5,db_6, db_7, db_8 );
TBaudRate = (br_110, br_300, br_600, br_1200, br_2400,br_4800, br_9600, br_14400,
br_19200, br_38400, br_56000, br_128000, br_256000);
TDtrControl = ( DtrEnable, DtrDisable, DtrHandshake );
TRtsControl = ( RtsEnable, RtsDisable, RtsHandshake, RtsTransmissionAvailable );
TCommError = (ceOpen,ceRead,ceWrite);
//通信错误:打开错误、读错误、写错误
ECommsError = class( Exception );
TReceiveDataEvent = procedure(Sender: TObject;
Buffer: Pointer;
BufferLength: Word) of object;
TModemStateChangeEvent = procedure(Sender: TObject;
ModemEvent : DWORD) of object;
TSendDataEmptyEvent = procedure(Sender: TObject) of object;
TCommErrorEvent = procedure(Sender:TObject;CommError:TCommError) of object;
const
//
// Modem Event Constant
//
ME_CTS = 1;
ME_DSR = 2;
ME_RING = 4;
ME_RLSD = 8;
type
TReadThread = class( TThread )
protected
procedure Execute;
override;
public
hCommFile: THandle;
hCloseEvent: THandle;
hComm32Window: THandle;
function SetupCommEvent( lpOverlappedCommEvent: POverlapped;
var lpfdwEvtMask: DWORD ): Boolean;
function SetupReadEvent( lpOverlappedRead: POverlapped;
lpszInputBuffer: LPSTR;
dwSizeofBuffer: DWORD;
var lpnNumberOfBytesRead: DWORD ): Boolean;
function HandleCommEvent( lpOverlappedCommEvent: POverlapped;
var lpfdwEvtMask: DWORD;
fRetrieveEvent: Boolean ): Boolean;
function HandleReadEvent( lpOverlappedRead: POverlapped;
lpszInputBuffer: LPSTR;
dwSizeofBuffer: DWORD;
var lpnNumberOfBytesRead: DWORD ): Boolean;
function HandleReadData( lpszInputBuffer: LPCSTR;
dwSizeofBuffer: DWORD ): Boolean;
function ReceiveData( lpNewString: LPSTR;
dwSizeofNewString: DWORD ): BOOL;
function ReceiveError( EvtMask : DWORD ): BOOL;
function ModemStateChange( ModemEvent : DWORD ) : BOOL;
procedure PostHangupCall;
end;

TComm = class( TObject )
private
ReadThread: TReadThread;
hCommFile: THandle;
hCloseEvent: THandle;
FHWnd: THandle;
FCommName: String;
FBaudRate: TBaudRate;
FParityCheck: Boolean;
FOutx_CtsFlow: Boolean;
FOutx_DsrFlow: Boolean;
FDtrControl: TDtrControl;
FDsrSensitivity: Boolean;
FTxContinueOnXoff: Boolean;
FOutx_XonXoffFlow: Boolean;
FInx_XonXoffFlow: Boolean;
FReplaceWhenParityError: Boolean;
FIgnoreNullChar: Boolean;
FRtsControl: TRtsControl;
FXonLimit: WORD;
FXoffLimit: WORD;
FByteSize: TByteSize;
FParity: TParity;
FStopBits: TStopBits;
FXonChar: AnsiChar;
FXoffChar: AnsiChar;
FReplacedChar: AnsiChar;
FReadIntervalTimeout: DWORD;
FReadTotalTimeoutMultiplier: DWORD;
FReadTotalTimeoutConstant: DWORD;
FWriteTotalTimeoutMultiplier: DWORD;
FWriteTotalTimeoutConstant: DWORD;
FOnReceiveData: TReceiveDataEvent;
FOnRequestHangup: TNotifyEvent;
FOnModemStateChange:TModemStateChangeEvent;
FOnSendDataEmpty: TSendDataEmptyEvent;
FOnCommError : TCommErrorEvent;
//add By jim 2001-07-25
procedure SetBaudRate( Rate : TBaudRate );
procedure SetParityCheck( b : Boolean );
procedure SetOutx_CtsFlow( b : Boolean );
procedure SetOutx_DsrFlow( b : Boolean );
procedure SetDtrControl( c : TDtrControl );
procedure SetDsrSensitivity( b : Boolean );
procedure SetTxContinueOnXoff( b : Boolean );
procedure SetOutx_XonXoffFlow( b : Boolean );
procedure SetInx_XonXoffFlow( b : Boolean );
procedure SetReplaceWhenParityError( b : Boolean );
procedure SetIgnoreNullChar( b : Boolean );
procedure SetRtsControl( c : TRtsControl );
procedure SetXonLimit( Limit : WORD );
procedure SetXoffLimit( Limit : WORD );
procedure SetByteSize( Size : TByteSize );
// procedure SetParity( p : TParity );
procedure SetStopBits( Bits : TStopBits );
procedure SetXonChar( c : AnsiChar );
procedure SetXoffChar( c : AnsiChar );
procedure SetReplacedChar( c : AnsiChar );
// procedure SetReadIntervalTimeout( v : DWORD );
// procedure SetReadTotalTimeoutMultiplier( v : DWORD );
// procedure SetReadTotalTimeoutConstant( v : DWORD );
// procedure SetWriteTotalTimeoutMultiplier( v : DWORD );
// procedure SetWriteTotalTimeoutConstant( v : DWORD );
procedure CommWndProc( var msg: TMessage );
procedure _SetCommState;
procedure _SetCommTimeout;
protected
{ Protected declarations }
procedure CloseReadThread;
procedure ReceiveData(Buffer: PChar;
BufferLength: Word);
procedure ReceiveError( EvtMask : DWORD );
procedure ModemStateChange( ModemEvent : DWORD );
procedure RequestHangup;
public
{ Public declarations }
constructor Create;
destructor Destroy;override;
property Handle: THandle read hCommFile;
function WriteCommData( pDataToWrite: PChar;
dwSizeofDataToWrite: Word ): Boolean;
function GetModemState : DWORD;
function Open:Boolean;
procedure Close;
property CommName: String read FCommName write FCommName;
property BaudRate: TBaudRate read FBaudRate write SetBaudRate;
property ParityCheck: Boolean read FParityCheck write SetParityCheck;
property Outx_CtsFlow: Boolean read FOutx_CtsFlow write SetOutx_CtsFlow;
property Outx_DsrFlow: Boolean read FOutx_DsrFlow write SetOutx_DsrFlow;
property DtrControl: TDtrControl read FDtrControl write SetDtrControl;
property DsrSensitivity: Boolean read FDsrSensitivity write SetDsrSensitivity;
property TxContinueOnXoff: Boolean read FTxContinueOnXoff write SetTxContinueOnXoff;
property Outx_XonXoffFlow: Boolean read FOutx_XonXoffFlow write SetOutx_XonXoffFlow;
property Inx_XonXoffFlow: Boolean read FInx_XonXoffFlow write SetInx_XonXoffFlow;
property ReplaceWhenParityError: Boolean read FReplaceWhenParityError write SetReplaceWhenParityError;
property IgnoreNullChar: Boolean read FIgnoreNullChar write SetIgnoreNullChar;
property RtsControl: TRtsControl read FRtsControl write SetRtsControl;
property XonLimit: WORD read FXonLimit write SetXonLimit;
property XoffLimit: WORD read FXoffLimit write SetXoffLimit;
property ByteSize: TByteSize read FByteSize write SetByteSize;
property Parity: TParity read FParity write FParity;
property StopBits: TStopBits read FStopBits write SetStopBits;
property XonChar: AnsiChar read FXonChar write SetXonChar;
property XoffChar: AnsiChar read FXoffChar write SetXoffChar;
property ReplacedChar: AnsiChar read FReplacedChar write SetReplacedChar;

property OnReceiveData: TReceiveDataEvent
read FOnReceiveData write FOnReceiveData;
property OnModemStateChange: TModemStateChangeEvent
read FOnModemStateChange write FOnModemStateChange;
property OnRequestHangup: TNotifyEvent
read FOnRequestHangup write FOnRequestHangup;
property OnSendDataEmpty: TSendDataEmptyEvent
read FOnSendDataEmpty write FOnSendDataEmpty;
property OnCommError:TCommErrorEvent
read FOnCommError write FOnCommError;
end;

const
// Default size of the Input Buffer used by this code.
INPUTBUFFERSIZE = 2048;
implementation
(******************************************************************************)
// TComm PUBLIC METHODS
(******************************************************************************)
constructor TComm.Create;
begin
ReadThread := nil;
hCommFile := 0;
hCloseEvent := 0;
FCommName := 'COM1';
FBaudRate := br_9600;
FParityCheck := False;
FOutx_CtsFlow := False;
FOutx_DsrFlow := False;
FDtrControl := DtrEnable;
FDsrSensitivity := False;
FTxContinueOnXoff := True;
FOutx_XonXoffFlow := True;
FInx_XonXoffFlow := True;
FReplaceWhenParityError := False;
FIgnoreNullChar := False;
FRtsControl := RtsEnable;
FXonLimit := 500;
FXoffLimit := 500;
FByteSize := db_8;
FParity := None;
FStopBits := sb_1;
FXonChar := chr($11);
// Ctrl-Q
FXoffChar := chr($13);
// Ctrl-S
FReplacedChar := chr(0);
FReadIntervalTimeout := 100;
FReadTotalTimeoutMultiplier := 0;
FReadTotalTimeoutConstant := 0;
FWriteTotalTimeoutMultiplier := 0;
FWriteTotalTimeoutConstant := 0;
end;

destructor TComm.Destroy;
begin
inherited Destroy;
end;

//
// FUNCTION: Open
//
// PURPOSE: Starts communications over the comm port.
//
// PARAMETERS:
// hNewCommFile - This is the COMM File handle to communicate with.
// This handle is obtained from TAPI.
//
// Output:
// Successful: Startup the communications.
// Failure: Raise a exception
//
// COMMENTS:
//
// StartComm makes sure there isn't communication in progress already,
// creates a Comm file, and creates the read and write threads. It
// also configures the hNewCommFile for the appropriate COMM settings.
//
// If StartComm fails for any reason, it's up to the calling application
// to close the Comm file handle.
//
//
function TComm.Open:Boolean;
var
hNewCommFile: THandle;
begin
Result := False;
// Are we alreadydo
ing comm?
if (hCommFile <> 0) then
begin
if Assigned(FOnCommError) then
FOnCommError(self,ceOpen)
else
raise ECommsError.Create( 'This serial port already opened' );
exit;
end;

FHWnd := AllocateHWnd(CommWndProc);
//2001-07-26
hNewCommFile := CreateFile( PChar(FCommName),
GENERIC_READ or GENERIC_WRITE,
0, {not shared}
nil, {no security ??}
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,
0 {template} );
if hNewCommFile = INVALID_HANDLE_VALUE then
begin
if Assigned(FOnCommError) then
FOnCommError(self,ceOpen)
else
raise ECommsError.Create( 'Error opening serial port' );
exit;
end;
// Is this a valid comm handle?
if GetFileType( hNewCommFile ) <> FILE_TYPE_CHAR then
begin
CloseHandle( hNewCommFile );
if Assigned(FOnCommError) then
FOnCommError(self,ceOpen)
else
raise ECommsError.Create( 'File handle is not a comm handle ' );
exit;
end;

if not SetupComm( hNewCommFile, 4096, 4096 ) then
begin
CloseHandle( hCommFile );
if Assigned(FOnCommError) then
FOnCommError(self,ceOpen)
else
raise ECommsError.Create( 'Cannot setup comm buffer' );
exit;
end;

// It is ok to continue.
hCommFile := hNewCommFile;
// purge any information in the buffer
PurgeComm( hCommFile, PURGE_TXABORT or PURGE_RXABORT or
PURGE_TXCLEAR or PURGE_RXCLEAR ) ;
// Setting the time-out value
_SetCommTimeout;
// Querying then
setting the comm port configurations.
_SetCommState;
// Create the event that will signal the threads to close.
hCloseEvent := CreateEvent( nil, True, False, nil );
if hCloseEvent = 0 then
begin
CloseHandle( hCommFile );
hCommFile := 0;
if Assigned(FOnCommError) then
FOnCommError(self,ceOpen)
else
raise ECommsError.Create( 'Unable to create event' );
exit;
end;

try
ReadThread := TReadThread.Create( True {suspended} );
except
ReadThread := nil;
CloseHandle( hCloseEvent );
CloseHandle( hCommFile );
hCommFile := 0;
if Assigned(FOnCommError) then
FOnCommError(self,ceOpen)
else
raise ECommsError.Create( 'Unable to create read thread' );
exit;
end;
ReadThread.hCommFile := hCommFile;
ReadThread.hCloseEvent := hCloseEvent;
ReadThread.hComm32Window := FHWnd;
// Comm threads should have a higher base priority than the UI thread.
// If theydo
n't, then
any temporary priority boost the UI thread gains
// could cause the COMM threads to loose data.
ReadThread.Priority := tpHighest;
ReadThread.Resume;
Result := True;
// Everything was created ok. Ready to go!
end;

//
// FUNCTION: Close
//
// PURPOSE: Stop and end all communication threads.
//
// PARAMETERS:
// none
//
// RETURN VALUE:
// none
//
// COMMENTS:
//
// Tries to gracefully signal all communication threads to
// close, but terminates them if it has to.
//
//
//procedure TComm.StopComm;
procedure TComm.Close;
begin
// No need to continue if we're not communicating.
if hCommFile = 0 then
Exit;
// Close the threads.
CloseReadThread;
// Not needed anymore.
CloseHandle( hCloseEvent );
// Now close the comm port handle.
CloseHandle( hCommFile );
hCommFile := 0;
DeallocateHWnd(FHwnd);
//2001-07-26
end;

//
// FUNCTION: GetModemState
//
// PURPOSE: Read the state of modem input pin right now
//
// PARAMETERS:
// none
//
// RETURN VALUE:
//
// A DWORD variable containing one or more of following codes:
//
// Value Meaning
// ---------- -----------------------------------------------------------
// MS_CTS_ON The CTS (clear-to-send) signal is on.
// MS_DSR_ON The DSR (data-set-ready) signal is on.
// MS_RING_ON The ring indicator signal is on.
// MS_RLSD_ON The RLSD (receive-line-signal-detect) signal is on.
//
// If this comm have bad handle or not yet opened, the return value is 0
//
// COMMENTS:
//
// This member function calls GetCommModemStatus and return its value.
// Before calling this member function, you must have a successful
// 'StartOpen' call.
//
//
function TComm.GetModemState : DWORD;
var
dwModemState : DWORD;
begin
if not GetCommModemStatus( hCommFile, dwModemState ) then
Result := 0
else
Result := dwModemState
end;

(******************************************************************************)
// TComm PROTECTED METHODS
(******************************************************************************)
//
// FUNCTION: CloseReadThread
//
// PURPOSE: Close the Read Thread.
//
// PARAMETERS:
// none
//
// RETURN VALUE:
// none
//
// COMMENTS:
//
// Closes the Read thread by signaling the CloseEvent.
// Purges any outstanding reads on the comm port.
//
// Note that terminating a thread leaks memory.
// Besides the normal leak incurred, there is an event object
// thatdo
esn't get closed. This isn't worth worrying about
// since it shouldn't happen anyway.
//
//
procedure TComm.CloseReadThread;
begin
// If it exists...
if ReadThread <> nil then
begin
// Signal the event to close the worker threads.
SetEvent( hCloseEvent );
// Purge all outstanding reads
PurgeComm( hCommFile, PURGE_RXABORT + PURGE_RXCLEAR );
// Wait 10 seconds for it to exit. Shouldn't happen.
// if (WaitForSingleObject(ReadThread.Handle, 10000) = WAIT_TIMEOUT) then
ReadThread.Terminate;
ReadThread.Free;
ReadThread := nil
end
end;
{TComm.CloseReadThread}

procedure TComm.ReceiveData(Buffer: PChar;
BufferLength: Word);
begin
if Assigned(FOnReceiveData) then
FOnReceiveData( self, Buffer, BufferLength )
end;

procedure TComm.ReceiveError( EvtMask : DWORD );
begin
// if Assigned(FOnReceiveError) then
// FOnReceiveError( self, EvtMask )
if Assigned(FOnCommError) then
FOnCommError(self,ceRead);
end;

procedure TComm.ModemStateChange( ModemEvent : DWORD );
begin
if Assigned(FOnModemStateChange) then
FOnModemStateChange( self, ModemEvent )
end;

procedure TComm.RequestHangup;
begin
if Assigned(FOnRequestHangup) then
FOnRequestHangup( Self )
end;

{
procedure TComm._SendDataEmpty;
begin
if Assigned(FOnSendDataEmpty) then
FOnSendDataEmpty( self )
end;
}
(******************************************************************************)
// TComm PRIVATE METHODS
(******************************************************************************)
procedure TComm.CommWndProc( var msg: TMessage );
begin
case msg.msg of
PWM_GOTCOMMDATA:
begin
ReceiveData( PChar(msg.LParam), msg.WParam );
LocalFree( msg.LParam )
end;
PWM_RECEIVEERROR: ReceiveError( msg.LParam );
PWM_MODEMSTATECHANGE:ModemStateChange( msg.LParam );
PWM_REQUESTHANGUP: RequestHangup;
// PWM_SENDDATAEMPTY: _SendDataEmpty
end
end;

procedure TComm._SetCommState;
var
dcb: Tdcb;
commprop: TCommProp;
fdwEvtMask: DWORD;
begin
// Configure the comm settings.
// NOTE: Most Comm settings can be set through TAPI, but this means that
// the CommFile will have to be passed to this component.
GetCommState( hCommFile, dcb );
GetCommProperties( hCommFile, commprop );
GetCommMask( hCommFile, fdwEvtMask );
// fAbortOnError is the only DCB dependancy in TapiComm.
// Can't guarentee that the SP will set this to what we expect.
{dcb.fAbortOnError := False;
NOT VALID}
// dcb.BaudRate := FBaudRate;
{** Set Baud Rate **}
case FBaudRate of
br_110: DCB.BaudRate := CBR_110;
br_300: DCB.BaudRate := CBR_300;
br_600: DCB.BaudRate := CBR_600;
br_1200: DCB.BaudRate := CBR_1200;
br_2400: DCB.BaudRate := CBR_2400;
br_4800: DCB.BaudRate := CBR_4800;
br_9600: DCB.BaudRate := CBR_9600;
br_14400: DCB.BaudRate := CBR_14400;
br_19200: DCB.BaudRate := CBR_19200;
br_38400: DCB.BaudRate := CBR_38400;
br_56000: DCB.BaudRate := CBR_56000;
br_128000: DCB.BaudRate := CBR_128000;
br_256000: DCB.BaudRate := CBR_256000;
end;

dcb.Flags := 1;
// Enable fBinary
if FParityCheck then
dcb.Flags := dcb.Flags or 2;
// Enable parity check
// setup hardware flow control
if FOutx_CtsFlow then
dcb.Flags := dcb.Flags or 4;
if FOutx_DsrFlow then
dcb.Flags := dcb.Flags or 8;
if FDtrControl = DtrEnable then
dcb.Flags := dcb.Flags or $10
else
if FDtrControl = DtrHandshake then
dcb.Flags := dcb.Flags or $20;
if FDsrSensitivity then
dcb.Flags := dcb.Flags or $40;
if FTxContinueOnXoff then
dcb.Flags := dcb.Flags or $80;
if FOutx_XonXoffFlow then
dcb.Flags := dcb.Flags or $100;
if FInx_XonXoffFlow then
dcb.Flags := dcb.Flags or $200;
if FReplaceWhenParityError then
dcb.Flags := dcb.Flags or $400;
if FIgnoreNullChar then
dcb.Flags := dcb.Flags or $800;
if FRtsControl = RtsEnable then
dcb.Flags := dcb.Flags or $1000
else
if FRtsControl = RtsHandshake then
dcb.Flags := dcb.Flags or $2000
else
if FRtsControl = RtsTransmissionAvailable then
dcb.Flags := dcb.Flags or $3000;
dcb.XonLim := FXonLimit;
dcb.XoffLim := FXoffLimit;
dcb.ByteSize := Ord( FByteSize ) + 5;
dcb.Parity := Ord( FParity );
dcb.StopBits := Ord( FStopBits );
dcb.XonChar := FXonChar;
dcb.XoffChar := FXoffChar;
dcb.ErrorChar := FReplacedChar;
SetCommState( hCommFile, dcb )
end;

procedure TComm._SetCommTimeout;
var
commtimeouts: TCommTimeouts;
begin
GetCommTimeouts( hCommFile, commtimeouts );
// The CommTimeout numbers will very likely change if you are
// coding to meet some kind of specification where
// you need to reply within a certain amount of time after
// recieving the last byte. However, If 1/4th of a second
// goes by between recieving two characters, its a good
// indication that the transmitting end has finished, even
// assuming a 1200 baud modem.
commtimeouts.ReadIntervalTimeout := FReadIntervalTimeout;
commtimeouts.ReadTotalTimeoutMultiplier := FReadTotalTimeoutMultiplier;
commtimeouts.ReadTotalTimeoutConstant := FReadTotalTimeoutConstant;
commtimeouts.WriteTotalTimeoutMultiplier := FWriteTotalTimeoutMultiplier;
commtimeouts.WriteTotalTimeoutConstant := FWriteTotalTimeoutConstant;
SetCommTimeouts( hCommFile, commtimeouts );
end;

procedure TComm.SetBaudRate( Rate : TBaudRate );
begin
if Rate = FBaudRate then
Exit;
FBaudRate := Rate;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetParityCheck( b : Boolean );
begin
if b = FParityCheck then
Exit;
FParityCheck := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetOutx_CtsFlow( b : Boolean );
begin
if b = FOutx_CtsFlow then
Exit;
FOutx_CtsFlow := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetOutx_DsrFlow( b : Boolean );
begin
if b = FOutx_DsrFlow then
Exit;
FOutx_DsrFlow := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetDtrControl( c : TDtrControl );
begin
if c = FDtrControl then
Exit;
FDtrControl := c;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetDsrSensitivity( b : Boolean );
begin
if b = FDsrSensitivity then
Exit;
FDsrSensitivity := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetTxContinueOnXoff( b : Boolean );
begin
if b = FTxContinueOnXoff then
Exit;
FTxContinueOnXoff := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetOutx_XonXoffFlow( b : Boolean );
begin
if b = FOutx_XonXoffFlow then
Exit;
FOutx_XonXoffFlow := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetInx_XonXoffFlow( b : Boolean );
begin
if b = FInx_XonXoffFlow then
Exit;
FInx_XonXoffFlow := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetReplaceWhenParityError( b : Boolean );
begin
if b = FReplaceWhenParityError then
Exit;
FReplaceWhenParityError := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetIgnoreNullChar( b : Boolean );
begin
if b = FIgnoreNullChar then
Exit;
FIgnoreNullChar := b;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetRtsControl( c : TRtsControl );
begin
if c = FRtsControl then
Exit;
FRtsControl := c;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetXonLimit( Limit : WORD );
begin
if Limit = FXonLimit then
Exit;
FXonLimit := Limit;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetXoffLimit( Limit : WORD );
begin
if Limit = FXoffLimit then
Exit;
FXoffLimit := Limit;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetByteSize( Size : TByteSize );
begin
if Size = FByteSize then
Exit;
FByteSize := Size;
if hCommFile <> 0 then
_SetCommState
end;
{
procedure TComm.SetParity( p : TParity );
begin
if p = FParity then
Exit;
FParity := p;
if hCommFile <> 0 then
_SetCommState
end;
}
procedure TComm.SetStopBits( Bits : TStopBits );
begin
if Bits = FStopBits then
Exit;
FStopBits := Bits;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetXonChar( c : AnsiChar );
begin
if c = FXonChar then
Exit;
FXonChar := c;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetXoffChar( c : AnsiChar );
begin
if c = FXoffChar then
Exit;
FXoffChar := c;
if hCommFile <> 0 then
_SetCommState
end;

procedure TComm.SetReplacedChar( c : AnsiChar );
begin
if c = FReplacedChar then
Exit;
FReplacedChar := c;
if hCommFile <> 0 then
_SetCommState
end;
{
procedure TComm.SetReadIntervalTimeout( v : DWORD );
begin
if v = FReadIntervalTimeout then
Exit;
FReadIntervalTimeout := v;
if hCommFile <> 0 then
_SetCommTimeout
end;

procedure TComm.SetReadTotalTimeoutMultiplier( v : DWORD );
begin
if v = FReadTotalTimeoutMultiplier then
Exit;
FReadTotalTimeoutMultiplier := v;
if hCommFile <> 0 then
_SetCommTimeout
end;

procedure TComm.SetReadTotalTimeoutConstant( v : DWORD );
begin
if v = FReadTotalTimeoutConstant then
Exit;
FReadTotalTimeoutConstant := v;
if hCommFile <> 0 then
_SetCommTimeout
end;

procedure TComm.SetWriteTotalTimeoutMultiplier( v : DWORD );
begin
if v = FWriteTotalTimeoutMultiplier then
Exit;
FWriteTotalTimeoutMultiplier := v;
if hCommFile <> 0 then
_SetCommTimeout
end;

procedure TComm.SetWriteTotalTimeoutConstant( v : DWORD );
begin
if v = FWriteTotalTimeoutConstant then
Exit;
FWriteTotalTimeoutConstant := v;
if hCommFile <> 0 then
_SetCommTimeout
end;
}
(******************************************************************************)
// READ THREAD
(******************************************************************************)
//
// PROCEDURE: TReadThread.Execute
//
// PURPOSE: This is the starting point for the Read Thread.
//
// PARAMETERS:
// None.
//
// RETURN VALUE:
// None.
//
// COMMENTS:
//
// The Read Thread uses overlapped ReadFile and sends any data
// read from the comm port to the Comm32Window. This is
// eventuallydo
ne through a PostMessage so that the Read Thread
// is never away from the comm port very long. This also provides
// natural desynchronization between the Read thread and the UI.
//
// If the CloseEvent object is signaled, the Read Thread exits.
//
// Separating the Read and Write threads is natural for a application
// where there is no need for synchronization between
// reading and writing. However, if there is such a need (for example,
// most file transfer algorithms synchronize the reading and writing),
// then
it would make a lot more sense to have a single thread to handle
// both reading and writing.
//
//
procedure TReadThread.Execute;
var
szInputBuffer: array[0..INPUTBUFFERSIZE-1] of Char;
nNumberOfBytesRead: DWORD;
HandlesToWaitFor: array[0..2] of THandle;
dwHandleSignaled: DWORD;
fdwEvtMask: DWORD;
// Needed for overlapped I/O (ReadFile)
overlappedRead: TOverlapped;
// Needed for overlapped Comm Event handling.
overlappedCommEvent: TOverlapped;
label
EndReadThread;
begin
FillChar( overlappedRead, Sizeof(overlappedRead), 0 );
FillChar( overlappedCommEvent, Sizeof(overlappedCommEvent), 0 );
// Lets put an event in the Read overlapped structure.
overlappedRead.hEvent := CreateEvent( nil, True, True, nil);
if overlappedRead.hEvent = 0 then
begin
PostHangupCall;
goto EndReadThread
end;

// And an event for the CommEvent overlapped structure.
overlappedCommEvent.hEvent := CreateEvent( nil, True, True, nil);
if overlappedCommEvent.hEvent = 0 then
begin
PostHangupCall();
goto EndReadThread
end;

// We will be waiting on these objects.
HandlesToWaitFor[0] := hCloseEvent;
HandlesToWaitFor[1] := overlappedCommEvent.hEvent;
HandlesToWaitFor[2] := overlappedRead.hEvent;
// Setup CommEvent handling.
// Set the comm mask so we receive error signals.
if not SetCommMask(hCommFile, EV_ERR or EV_RLSD or EV_RING ) then
begin
PostHangupCall;
goto EndReadThread
end;

// Start waiting for CommEvents (Errors)
if not SetupCommEvent( @overlappedCommEvent, fdwEvtMask ) then
goto EndReadThread;
// Start waiting for Read events.
if not SetupReadEvent( @overlappedRead,
szInputBuffer, INPUTBUFFERSIZE,
nNumberOfBytesRead ) then
goto EndReadThread;
// Keep looping until we break out.
while Truedo
begin
// Wait until some event occurs (data to read;
error;
stopping).
dwHandleSignaled := WaitForMultipleObjects(3, @HandlesToWaitFor,
False, INFINITE);
// Which event occured?
case dwHandleSignaled of
WAIT_OBJECT_0: // Signal to end the thread.
begin
// Time to exit.
goto EndReadThread
end;

WAIT_OBJECT_0 + 1: // CommEvent signaled.
begin
// Handle the CommEvent.
if not HandleCommEvent( @overlappedCommEvent, fdwEvtMask, TRUE ) then
goto EndReadThread;
// Start waiting for the next CommEvent.
if not SetupCommEvent( @overlappedCommEvent, fdwEvtMask ) then
goto EndReadThread
{break;??}
end;

WAIT_OBJECT_0 + 2: // Read Event signaled.
begin
// Get the new data!
if not HandleReadEvent( @overlappedRead,
szInputBuffer,
INPUTBUFFERSIZE,
nNumberOfBytesRead ) then
goto EndReadThread;
// Wait for more new data.
if not SetupReadEvent( @overlappedRead,
szInputBuffer, INPUTBUFFERSIZE,
nNumberOfBytesRead ) then
goto EndReadThread
{break;}
end;

WAIT_FAILED: // Wait failed. Shouldn't happen.
begin
PostHangupCall;
goto EndReadThread
end
else
// This case should never occur.
begin
PostHangupCall;
goto EndReadThread
end
end {case dwHandleSignaled}
end;
{while True}
// Time to clean up Read Thread.
EndReadThread:
PurgeComm( hCommFile, PURGE_RXABORT + PURGE_RXCLEAR );
CloseHandle( overlappedRead.hEvent );
CloseHandle( overlappedCommEvent.hEvent )
end;
{TReadThread.Execute}
//
// FUNCTION: SetupReadEvent(LPOVERLAPPED, LPSTR, DWORD, LPDWORD)
//
// PURPOSE: Sets up an overlapped ReadFile
//
// PARAMETERS:
// lpOverlappedRead - address of overlapped structure to use.
// lpszInputBuffer - Buffer to place incoming bytes.
// dwSizeofBuffer - size of lpszInputBuffer.
// lpnNumberOfBytesRead - address of DWORD to place the number of read bytes.
//
// RETURN VALUE:
// TRUE if able to successfully setup the ReadFile. FALSE if there
// was a failure setting up or if the CloseEvent object was signaled.
//
// COMMENTS:
//
// This function is a helper function for the Read Thread. This
// function sets up the overlapped ReadFile so that it can later
// be waited on (or more appropriatly, so the event in the overlapped
// structure can be waited upon). If there is data waiting, it is
// handled and the next ReadFile is initiated.
// Another possible reason for returning FALSE is if the comm port
// is closed by the service provider.
//
//
//
function TReadThread.SetupReadEvent( lpOverlappedRead: POverlapped;
lpszInputBuffer: LPSTR;
dwSizeofBuffer: DWORD;
var lpnNumberOfBytesRead: DWORD ): Boolean;
var
dwLastError: DWORD;
label
StartSetupReadEvent;
begin
Result := False;
StartSetupReadEvent:
// Make sure the CloseEvent hasn't been signaled yet.
// Check is needed because this function is potentially recursive.
if WAIT_TIMEOUT <> WaitForSingleObject(hCloseEvent,0) then
Exit;
// Start the overlapped ReadFile.
if ReadFile( hCommFile,
lpszInputBuffer^, dwSizeofBuffer,
lpnNumberOfBytesRead, lpOverlappedRead ) then
begin
// This would only happen if there was data waiting to be read.
// Handle the data.
if not HandleReadData( lpszInputBuffer, lpnNumberOfBytesRead ) then
Exit;
// Start waiting for more data.
goto StartSetupReadEvent
end;

// ReadFile failed. Expected because of overlapped I/O.
dwLastError := GetLastError;
// LastError was ERROR_IO_PENDING, as expected.
if dwLastError = ERROR_IO_PENDING then
begin
Result := True;
Exit
end;

// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if dwLastError = ERROR_INVALID_HANDLE then
Exit;
// Unexpected error come here. No idea what could cause this to happen.
PostHangupCall
end;
{TReadThread.SetupReadEvent}
//
// FUNCTION: HandleReadData(LPCSTR, DWORD)
//
// PURPOSE: Deals with data after its been read from the comm file.
//
// PARAMETERS:
// lpszInputBuffer - Buffer to place incoming bytes.
// dwSizeofBuffer - size of lpszInputBuffer.
//
// RETURN VALUE:
// TRUE if able to successfully handle the data.
// FALSE if unable to allocate memory or handle the data.
//
// COMMENTS:
//
// This function is yet another helper function for the Read Thread.
// It LocalAlloc()s a buffer, copies the new data to this buffer and
// calls PostWriteToDisplayCtl to let the EditCtls module deal with
// the data. Its assumed that PostWriteToDisplayCtl posts the message
// rather than dealing with it right away so that the Read Thread
// is free to get right back to waiting for data. Its also assumed
// that the EditCtls module is responsible for LocalFree()ing the
// pointer that is passed on.
//
//
function TReadThread.HandleReadData( lpszInputBuffer: LPCSTR;
dwSizeofBuffer: DWORD ): Boolean;
var
lpszPostedBytes: LPSTR;
begin
Result := False;
// If we got data and didn't just time out empty...
if dwSizeofBuffer <> 0 then
begin
//do
something with the bytes read.
lpszPostedBytes := PChar( LocalAlloc( LPTR, dwSizeofBuffer+1 ) );
if lpszPostedBytes = nil{NULL} then
begin
// Out of memory
PostHangupCall;
Exit
end;

Move( lpszInputBuffer^, lpszPostedBytes^, dwSizeofBuffer );
lpszPostedBytes[dwSizeofBuffer] := #0;
Result := ReceiveData( lpszPostedBytes, dwSizeofBuffer )
end
end;
{TReadThread.HandleReadData}
//
// FUNCTION: HandleReadEvent(LPOVERLAPPED, LPSTR, DWORD, LPDWORD)
//
// PURPOSE: Retrieves and handles data when there is data ready.
//
// PARAMETERS:
// lpOverlappedRead - address of overlapped structure to use.
// lpszInputBuffer - Buffer to place incoming bytes.
// dwSizeofBuffer - size of lpszInputBuffer.
// lpnNumberOfBytesRead - address of DWORD to place the number of read bytes.
//
// RETURN VALUE:
// TRUE if able to successfully retrieve and handle the available data.
// FALSE if unable to retrieve or handle the data.
//
// COMMENTS:
//
// This function is another helper function for the Read Thread. This
// is the function that is called when there is data available after
// an overlapped ReadFile has been setup. It retrieves the data and
// handles it.
//
//
function TReadThread.HandleReadEvent( lpOverlappedRead: POverlapped;
lpszInputBuffer: LPSTR;
dwSizeofBuffer: DWORD;
var lpnNumberOfBytesRead: DWORD ): Boolean;
var
dwLastError: DWORD;
begin
Result := False;
if GetOverlappedResult( hCommFile,
lpOverlappedRead^, lpnNumberOfBytesRead, False ) then
begin
Result := HandleReadData( lpszInputBuffer, lpnNumberOfBytesRead );
Exit
end;

// Error in GetOverlappedResult;
handle it.
dwLastError := GetLastError;
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if dwLastError = ERROR_INVALID_HANDLE then
Exit;
// Unexpected error come here. No idea what could cause this to happen.
PostHangupCall
end;
{TReadThread.HandleReadEvent}
//
// FUNCTION: SetupCommEvent(LPOVERLAPPED, LPDWORD)
//
// PURPOSE: Sets up the overlapped WaitCommEvent call.
//
// PARAMETERS:
// lpOverlappedCommEvent - Pointer to the overlapped structure to use.
// lpfdwEvtMask - Pointer to DWORD to received Event data.
//
// RETURN VALUE:
// TRUE if able to successfully setup the WaitCommEvent.
// FALSE if unable to setup WaitCommEvent, unable to handle
// an existing outstanding event or if the CloseEvent has been signaled.
//
// COMMENTS:
//
// This function is a helper function for the Read Thread that sets up
// the WaitCommEvent so we can deal with comm events (like Comm errors)
// if they occur.
//
//
function TReadThread.SetupCommEvent( lpOverlappedCommEvent: POverlapped;
var lpfdwEvtMask: DWORD ): Boolean;
var
dwLastError: DWORD;
label
StartSetupCommEvent;
begin
Result := False;

StartSetupCommEvent:
// Make sure the CloseEvent hasn't been signaled yet.
// Check is needed because this function is potentially recursive.
if WAIT_TIMEOUT <> WaitForSingleObject( hCloseEvent,0 ) then
Exit;
// Start waiting for Comm Errors.
if WaitCommEvent( hCommFile, lpfdwEvtMask, lpOverlappedCommEvent ) then
begin
// This could happen if there was an error waiting on the
// comm port. Lets try and handle it.
if not HandleCommEvent( nil, lpfdwEvtMask, False ) then
begin
{??? GetOverlappedResultdo
es not handle "NIL" as defined by Borland}
Exit
end;

// What could cause infinite recursion at this point?
goto StartSetupCommEvent
end;

// We expect ERROR_IO_PENDING returned from WaitCommEvent
// because we are waiting with an overlapped structure.
dwLastError := GetLastError;
// LastError was ERROR_IO_PENDING, as expected.
if dwLastError = ERROR_IO_PENDING then
begin
Result := True;
Exit
end;

// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if dwLastError = ERROR_INVALID_HANDLE then
Exit;
// Unexpected error. No idea what could cause this to happen.
PostHangupCall
end;
{TReadThread.SetupCommEvent}
//
// FUNCTION: HandleCommEvent(LPOVERLAPPED, LPDWORD, BOOL)
//
// PURPOSE: Handle an outstanding Comm Event.
//
// PARAMETERS:
// lpOverlappedCommEvent - Pointer to the overlapped structure to use.
// lpfdwEvtMask - Pointer to DWORD to received Event data.
// fRetrieveEvent - Flag to signal if the event needs to be
// retrieved, or has already been retrieved.
//
// RETURN VALUE:
// TRUE if able to handle a Comm Event.
// FALSE if unable to setup WaitCommEvent, unable to handle
// an existing outstanding event or if the CloseEvent has been signaled.
//
// COMMENTS:
//
// This function is a helper function for the Read Thread that (if
// fRetrieveEvent == TRUE) retrieves an outstanding CommEvent and
// deals with it. The only event that should occur is an EV_ERR event,
// signalling that there has been an error on the comm port.
//
// Normally, comm errors would not be put into the normal data stream
// as this sample is demonstrating. Putting it in a status bar would
// be more appropriate for a real application.
//
//
function TReadThread.HandleCommEvent( lpOverlappedCommEvent: POverlapped;
var lpfdwEvtMask: DWORD;
fRetrieveEvent: Boolean ): Boolean;
var
dwDummy: DWORD;
dwErrors: DWORD;
dwLastError: DWORD;
dwModemEvent: DWORD;
begin
Result := False;
// If this fails, it could be because the file was closed (and I/O is
// finished) or because the overlapped I/O is still in progress. In
// either case (or any others) its a bug and return FALSE.
if fRetrieveEvent then
begin
if not GetOverlappedResult( hCommFile,
lpOverlappedCommEvent^, dwDummy, False ) then
begin
dwLastError := GetLastError;
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if dwLastError = ERROR_INVALID_HANDLE then
Exit;
PostHangupCall;
Exit
end
end;

// Was the event an error?
if (lpfdwEvtMask and EV_ERR) <> 0 then
begin
// Which error was it?
if not ClearCommError( hCommFile, dwErrors, nil ) then
begin
dwLastError := GetLastError;
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if dwLastError = ERROR_INVALID_HANDLE then
Exit;
PostHangupCall;
Exit
end;

// Its possible that multiple errors occured and were handled
// in the last ClearCommError. Because all errors were signaled
// individually, but cleared all at once, pending comm events
// can yield EV_ERR while dwErrors equals 0. Ignore this event.
if not ReceiveError( dwErrors ) then
Exit;
Result := True
end;

dwModemEvent := 0;
if ((lpfdwEvtMask and EV_RLSD) <> 0) then
dwModemEvent := ME_RLSD;
if ((lpfdwEvtMask and EV_RING) <> 0) then
dwModemEvent := dwModemEvent or ME_RING;
if dwModemEvent <> 0 then
begin
if not ModemStateChange( dwModemEvent ) then
begin
Result := False;
Exit
end;

Result := True
end;

if ((lpfdwEvtMask and EV_ERR)=0) and (dwModemEvent=0) then
begin
// Should not have gotten here.
PostHangupCall
end
end;
{TReadThread.HandleCommEvent}
function TReadThread.ReceiveData( lpNewString: LPSTR;
dwSizeofNewString: DWORD ): BOOL;
begin
Result := False;
if not PostMessage( hComm32Window, PWM_GOTCOMMDATA,
WPARAM(dwSizeofNewString), LPARAM(lpNewString) ) then
PostHangupCall
else
Result := True
end;

function TReadThread.ReceiveError( EvtMask : DWORD ): BOOL;
begin
Result := False;
if not PostMessage( hComm32Window, PWM_RECEIVEERROR, 0, LPARAM(EvtMask) ) then
PostHangupCall
else
Result := True
end;

function TReadThread.ModemStateChange( ModemEvent : DWORD ) : BOOL;
begin
Result := False;
if not PostMessage( hComm32Window, PWM_MODEMSTATECHANGE, 0, LPARAM(ModemEvent) ) then
PostHangupCall
else
Result := True
end;

procedure TReadThread.PostHangupCall;
begin
PostMessage( hComm32Window, PWM_REQUESTHANGUP, 0, 0 )
end;

//--------------------------------------------------------------------
//
// FUNCTION: WriteCommData(PChar, Word)
//
// PURPOSE: Send a String to the Write Thread to be written to the Comm.
//
// PARAMETERS:
// pszStringToWrite - String to Write to Comm port.
// nSizeofStringToWrite - length of pszStringToWrite.
//
// RETURN VALUE:
// Returns TRUE if the PostMessage is successful.
// Returns FALSE if PostMessage fails or Write threaddo
esn't exist.
//
// COMMENTS:
//
// This is a wrapper function so that other modulesdo
n't care that
// Comm writing isdo
ne via PostMessage to a Write thread. Note that
// using PostMessage speeds up response to the UI (very little delay to
// 'write' a string) and provides a natural buffer if the comm is slow
// (ie: the messages just pile up in the message queue).
//
// Note that it is assumed that pszStringToWrite is allocated with
// LocalAlloc, and that if WriteCommData succeeds, its the job of the
// Write thread to LocalFree it. If WriteCommData fails, then
its
// the job of the calling function to free the string.
//
//
function TComm.WriteCommData( pDataToWrite: PChar;
dwSizeofDataToWrite: Word ): Boolean;
var
Buffer: Pointer;
overlappedWrite: TOverLapped;
dwLastError,
dwNumberOfBytesWritten,
dwWhereToStartWriting,
dwHandleSignaled: DWORD;
HandleToWaitFor: THandle;
label WriteError, //写串口错误
WriteOK, //写串口成功
GetOut;
//exit
begin
// Result := False;
// if dwSizeofDataToWrite <= 0 then
exit;
//2001-08-30 修改了在Win2000中无法写串口的错误
FillChar( overlappedWrite, Sizeof(overlappedWrite), 0 );
// Lets put an event in the Write overlapped structure.
overlappedWrite.hEvent := CreateEvent( nil, True, True, nil);
//2001-08-30 modi end
if dwSizeofDataToWrite <= 0 then
goto WriteError;
Buffer := Pointer(LocalAlloc( LPTR, dwSizeofDataToWrite+1 ));
Move( pDataToWrite^, Buffer^, dwSizeofDataToWrite );
dwNumberOfBytesWritten := 0;
dwWhereToStartWriting := 0;
// Start at the begin
ning.
HandleToWaitFor := overlappedWrite.hEvent;
// Keep looping until all characters have been written.
repeat
// Start the overlapped I/O.
if not WriteFile( hCommFile,
pDataToWrite[ dwWhereToStartWriting ],
dwSizeofDataToWrite, dwNumberOfBytesWritten,
@OverlappedWrite ) then

{ if not WriteFile( hCommFile,
pDataToWrite[ dwWhereToStartWriting ],
dwSizeofDataToWrite, dwNumberOfBytesWritten,
nil ) then
} begin
{WriteFile failure}
dwLastError := GetLastError;
// WriteFile failed. Expected;
lets handle it.
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
// if dwLastError = ERROR_INVALID_HANDLE then
Exit;
// if dwLastError <> ERROR_IO_PENDING then
Exit;
if dwLastError = ERROR_INVALID_HANDLE then
goto WriteError;
if dwLastError <> ERROR_IO_PENDING then
goto WriteError;
// This is the expected ERROR_IO_PENDING case.
dwHandleSignaled := WaitForSingleObject(HandleToWaitFor,INFINITE);
case dwHandleSignaled of
WAIT_OBJECT_0: // CloseEvent signaled!
begin
// Time to get the results of the WriteFile
if not GetOverlappedResult(hCommFile,OverlappedWrite,
dwNumberOfBytesWritten, True) then
begin
dwLastError := GetLastError;
// Its possible for this error to occur if the
// service provider has closed the port.
// if dwLastError = ERROR_INVALID_HANDLE then
Exit;
if dwLastError = ERROR_INVALID_HANDLE then
goto WriteError;
end;
end;
else
//Default deal of case
// Exit;
goto WriteError;
end;
{case}
end;
{WriteFile failure}
// Some data was written. Make sure it all got written.
Dec( dwSizeofDataToWrite, dwNumberOfBytesWritten );
Inc( dwWhereToStartWriting, dwNumberOfBytesWritten )
until (dwSizeofDataToWrite <= 0);
// Write the whole thing!
WriteOK:
// Wrote the whole string.
Result := True;
if Assigned(FOnSendDataEmpty) then
FOnSendDataEmpty(Self);
goto GetOut;
WriteError:
Result := False;
if Assigned(FOnCommError) then
FOnCommError(self,ceWrite);
goto GetOut;
//2001-08-30 修改了在Win2000中无法写串口的错误
GetOut:
CloseHandle(overlappedWrite.hEvent);
exit;
//2001-08-30 modi end
end;

end.
 
最近也在搞这些!贴出让大家参考参考!![:)]
怎么修改帖子不行!![:(!]
 
请lyywy给一个能用的实际例子,谢谢
你所说的用例子好象不行,问题出在什么地方
谢谢
 
to 大家
对方好像有问题了。
大家可以去看看,它的接口参数
http://sal006.salnetwork.com:83/lucin/smsmessaging/process.xml
 
租用ISP的网关啊,比如新浪、搜狐等。然后就可以用HTTP控件来进行数据传递。
 
用http的方式比较恶心,人家的网站关门了怎么办?
谁能详细描述一下sms的原理,gateway等等到是比较有兴趣。
 
我有一种设备可以发短信。装上一张手机的卡,再连接到电脑的com口上,就可以编程发短信了。
不过要在你这个手机上按电信的资费标准收费。
想购买的话,可以和我联系。fat_tao@21cn.com
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部