2015年11月2日 星期一

如何抓取中斷 com port被中斷 Delphi + MSComm control serial port transceiver procedures communication

如何抓取中斷  com port被中斷


直接用 API 的話, 
可以參考范逸之、陳立元編著的<>一書
元件的話有微軟附的 MSComm(這個要 Import ActiveX)、SPComm 等
小弟則是用 TurboPower 的 TApdComport 元件


/ / Delphi + MSComm control serial port transceiver procedures 
/ / HotPower@126.com. 

to procedure TForm1.FormCreate (Sender: TObject); 
begin 
if MSComm1.PortOpen then MSComm1.PortOpen: = false ;/ / close the port 
MSComm1.CommPort: = 2 ;/ / set port 2 
Receive buffer MSComm1.InBufferSize: = 256 ;/ / set to 256 bytes 
MSComm1.OutBufferSize: = 256 ;/ / set the transmit buffer is 256 bytes 
MSComm1.Settings: = '9600, n, 8,1 ';/ / 9600 baud rate, no parity, 8 data bits, 1 stop bit 
MSComm1.InputLen: = 0 ;/ / read the entire contents of the buffer (32 bytes) 
MSComm1.InBufferCount: = 0 ;/ / clear the receive buffer 
MSComm1.OutBufferCount: = 0 ;/ / clear to send buffer 
MSComm1.RThreshold: = 32 ;/ / set to receive 32 bytes produce OnComm events 
/ / MSComm1.InputMode: = comInputModeText ;/ / text 
MSComm1.InputMode: = comInputModeBinary ;/ / binary mode 
MSComm1.PortOpen: = true ;/ / open ports 
end; 

procedure TForm1.FormCloseQuery (Sender: TObject; var CanClose: Boolean); 
begin 
if MSComm1.PortOpen then MSComm1.PortOpen: = false; ;/ / close the port 
end; 

to procedure TForm1.MSComm1Comm (Sender: TObject); 
var 
buffer: Olevariant ;/ / MSComm1.InputMode = comInputModeBinary 
str: string ;/ / MSComm1.InputMode = comInputModeText 
i: integer; 
begin 
case MSComm1.CommEvent of 
comEvReceive: / / serial receive event handler 
begin 
the if MSComm1.InputMode = comInputModeText read then / / character mode 
str: = MSComm1.Input / / read out automatically clear the receive buffer, str [1] ~ str [32] 
else / / binary mode to read 
buffer: = MSComm1.Input ;/ / read out automatically clear the receive buffer, buffer [0] to buffer [31] 
Edit3.Text: =''; 
for i: = 0 to MSComm1.RThreshold - 1 do / / 32 bytes Hex conversion 
begin 
if MSComm1.InputMode = comInputModeText then / / one-line character conversion 
Edit3.Text: = Edit3.Text + inttohex (byte (str [i + 1]), 2) + '' 
else / / single-line binary data conversion 
Edit3.Text: = Edit3.Text + inttohex (buffer [i], 2) + ''; 
end; 
Memo2.Lines.Add (Edit3.Text) ;/ / add a line display 
end; 
end; 
end; 

to procedure TForm1.BitBtn1Click (Sender: TObject); 
var 
i: integer; 
begin 
Edit4.Text: =''; 
for i: = 0 to 31 do 
begin 
MSComm1.Output: = char (i) ;/ / send a character 
Edit4.Text: = Edit4.Text + inttohex (i, 2) ;/ / displayed in hexadecimal characters 
end; 
Memo1.Lines.Add (Edit4.Text) ;/ / add a line display 

end;



[Html]
Add -> COM components -> the Mircrosoft Communications Control, version 6.0
The drag mscomm drag the dialog box,
Add variable m_comm
Add an event OnComm
  
void CtestDlg :: InitMsComm (void)
{
m_comm.put_CommPort (1); / / select COM1
m_comm.put_InBufferSize (1024); / / specify the receive buffer size
m_comm.put_OutBufferSize (1024) ;/ / specify the send buffer size
m_comm.put_InputLen (0); / / set the current reception area data length is 0, which means that all read
m_comm.put_InputMode (1); / / read and write data in binary mode
m_comm.put_Settings (L "9600, N, 8,1"); / / 9600 baud no parity bit, 8 data bits, 1 stop bit
the m_comm.put_RThreshold (1); / / receive buffer 1 and one or more characters, will lead to receive data OnComm event
try
{
m_comm.put_PortOpen (TRUE);
}
catch (...)
{
AfxMessageBox (L "serial port 1 open failed");
return;
}
m_comm.get_Input (); / / first read-ahead buffer to remove residual data
}
  
void CtestDlg :: OnCommMscomm1 ()
{
/ / =============== Serial data reception process ===================
the if (m_comm.get_CommEvent () == 2)
{
COleSafeArray SafeArray = m_comm. Get_Input (); / / first step
int iLen = SafeArray. GetOneDimSize (); / / The second step
The BYTE revBuf [1024];
for (long i = 0; i <   ILEN; i + +)
{
SafeArray.GetElement (& i, revBuf + i);
}
  
/ / RevBuf the processing
  
}
}
  
/ / ================ Send =================
  
void CtestDlg :: SendCommBuf (BYTE * pSendBuf, int count)
{
CByteArray m_Array;
m_Array.SetSize (count);
for (int i = 0; i <   count; i + +)
{
m_Array.SetAt (i, pSendBuf [i]);
}
m_comm.put_Output (COleVariant (m_Array));
}
  
void CtestDlg :: OnBnClickedButton1 () / / send sample
{
BYTE buf [3] = {0x11, 0x12, 0x13};
The SendCommBuf (buf);
}

沒有留言: