  | 
				VFP 愛用者社區 本討論區為 Visual Foxpro 愛用者經驗交流的地方, 請多多利用"搜尋"的功能, 先查看看有無前例可循, 如果還有不懂的再發問. 部份主題有附加檔案, 須先註冊成為社區居民才可以下載.   
				 | 
			 
		 
		 
	
		| 上一篇主題 :: 下一篇主題   | 
	 
	
	
		| 發表人 | 
		內容 | 
	 
	
		chuander
 
 
  註冊時間: 2003-08-25 文章: 16
 
  第 1 樓
  | 
		
			
				 發表於: 星期三 四月 10, 2013 5:58 pm    文章主題: 請問有關MSComm物件問題 | 
				     | 
			 
			
				
  | 
			 
			
				公司有套用VFP3寫的程式
 
 
因為客戶端的電腦系統改為XP(原先是Win98)
 
 
XP無法安裝VFP3,所以改用VFP6重新編譯
 
 
但發覺MSComm(com port)物件無法正常運作
 
 
請教各位先進,想問VFP3跟VFP6的MSComm物件有差別嗎? | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		chuander
 
 
  註冊時間: 2003-08-25 文章: 16
 
  第 2 樓
  | 
		
			
				 發表於: 星期一 四月 15, 2013 9:46 am    文章主題:  | 
				     | 
			 
			
				
  | 
			 
			
				從VFP3轉換至VFP6程式完全沒有變更的情況下~~
 
 
納悶的是查門禁主機內有幾筆子卡總數的指令(card_sum)又沒問題。
 
 
但開門指令(OpenDoor)或其他指令幾乎都有問題?
 
 
 
Oleobject = C:\Windows\system32\MSCOMM32.OCX
 
BaseControl:OleControl
 
OleClass:MSCOMMLib.MSComm.1
 
 
 
 
 
* 執行開門動作 ***********************************************
 
Procedure OpenDoor
 
Lparameters nAddr_No, nDoor_No
 
Private cCmd, cData
 
 
cCmd = 'MM1' + Alltrim( Str( nDoor_No))
 
If .Not. This.PortOpen
 
	This.PortOpen = .T.
 
Endif
 
This.Send( nAddr_No, cCmd)
 
cData = This.Recv()
 
This.PortOpen = .F.
 
 
If cData # 'G'
 
	= Messagebox( '對 SC-202A 通訊有問題,請查明。' + Chr( 13) + '錯誤說明:' + cData, 64, '錯誤')
 
	Return .F.
 
Endif
 
Return .T.
 
Endproc
 
 
 
 
 
* 取得已設定之子卡總數 ***********************************************
 
Procedure card_sum
 
Lparameters nAddr_No
 
 
Private cCmd, cData
 
 
cCmd = 'BB1'
 
 
If .Not. This.PortOpen
 
	This.PortOpen = .T.
 
Endif
 
This.Send( nAddr_No, cCmd)
 
cData = This.Recv()
 
This.PortOpen = .F.
 
 
If Len( cData) # 4
 
	= Messagebox( '對 SC-202A 通訊有問題,請查明。' + Chr( 13) + '錯誤說明:' + cData, 64, '錯誤')
 
Endif
 
Return cData
 
Endproc
 
 
 
 
 
* 傳送門禁指令 ***********************************************
 
Procedure Send
 
Lparameters nId, cStr
 
 
Private cAdr1, cAdr2
 
Private nLen, nLoop
 
Private nTmp, cBuf
 
Private nChkSum1, nChkSum2
 
 
cBuf = cStr
 
 
&& 設定欲傳送之門禁工作站編號
 
cAdr1 = Chr( nId)
 
cAdr2 = Chr( nId + 64)
 
 
&& 資料轉碼
 
nLen = Len( cBuf)
 
For nLoop = 1 To nLen
 
	nTmp = Asc( Substr( cBuf, nLoop, 1))
 
	nTmp = nTmp + 96
 
	cBuf = Stuff( cBuf, nLoop, 1, Chr( nTmp))
 
Next
 
cBuf = cAdr1 + cAdr2 + cBuf
 
 
&& 計算檢查碼
 
nLen = Len( cBuf)
 
nChkSum1 = Asc( Left( cBuf, 1))
 
For nLoop = 2 To nLen
 
	nChkSum2 = Asc( Substr( cBuf, nLoop, 1))
 
	nChkSum1 = Bitxor( nChkSum1, nChkSum2)
 
Next
 
 
nChkSum1 = Bitand( nChkSum1, 63)
 
nChkSum1 = Bitor( nChkSum1, 192)
 
 
&& 組合傳送字串
 
cBuf = cBuf + Chr( nChkSum1)
 
 
&& 開始傳送
 
This.Output = cBuf
 
Return .T.
 
Endproc
 
 
 
 
* 接收門禁主機回傳資料 **************************************
 
Procedure Recv
 
Private nSec
 
Private cBuf
 
Private nTmp
 
Private nLen
 
Private cData
 
Private nLoop
 
Private nChkSum1, nChkSum2
 
 
nSec = Seconds()
 
cBuf = Space(0)
 
 
nTmp = 0
 
Do While .T.
 
	cBuf = cBuf + This.Input
 
	nLen = Len( cBuf)
 
	For nLoop = nLen To 1 Step -1
 
		If Substr( cBuf, nLoop, 1) == Chr( 13) .And. nLoop < nLen
 
			nTmp = nLoop
 
			Exit
 
		Endif
 
	Next
 
	If nTmp # 0
 
		cBuf = Substr( cBuf, 1, nTmp + 1)
 
		Exit
 
	Endif
 
	If Seconds() - nSec = 2			&& 設定讀取 Time Out 時間為 2 秒
 
		Return 'TIME OUT'
 
	Endif
 
Enddo
 
 
&& 尋找第一個 SOH -- CHR( 1)
 
nTmp = 0
 
nLen = Len( cBuf)
 
For nLoop = 1 To nLen
 
	If Substr( cBuf, nLoop, 1) == Chr( 1)
 
		nTmp = nLoop
 
		Exit
 
	Endif
 
Next
 
If nTmp = 0
 
	Return 'SOH ERROR 1'
 
Endif
 
 
cBuf = Substr( cBuf, nTmp)
 
If Left( cBuf, 3) # Chr( 1) + Chr( 1) + Chr( 1)
 
	Return 'SOH ERROR 3'
 
Endif
 
 
nTmp = Len( cBuf)
 
cData = Substr( cBuf, 4, nTmp - 5)	&& 取出資料內容
 
 
&& 計算檢查碼
 
nChkSum1 = Asc( Left( cData, 1))
 
nTmp = Len( cData)
 
For nLoop = 2 To nTmp
 
	nChkSum2 = Asc( Substr( cData, nLoop, 1))
 
	nChkSum1 = Bitxor( nChkSum1, nChkSum2)
 
Next
 
nChkSum1 = Bitxor( nChkSum1, 13)
 
nChkSum2 = Asc( Right( cBuf, 1))
 
 
If nChkSum1 # nChkSum2
 
	Return 'CHECK SUM ERROR'
 
Endif
 
Return cData
 
Endproc | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		garfield Site Admin
  
  註冊時間: 2003-01-30 文章: 2160
 
  第 3 樓
  | 
		
			
				 發表於: 星期一 四月 15, 2013 1:59 pm    文章主題:  | 
				     | 
			 
			
				
  | 
			 
			
				會不會是電腦運算速度變快, 而你送出RS232指令後太快去檢查是否有回送訊息的關係,
 
你試著加一個 inkey(3) 等3秒後才去執行下一個動作
 
例:
 
This.Send( nAddr_No, cCmd)
 
inkey(3)      &&--可調整增加等待時間不一定要3秒
 
cData = This.Recv() _________________ 利用>>搜尋<<的功能會比問的還要快得到答案. | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		chuander
 
 
  註冊時間: 2003-08-25 文章: 16
 
  第 4 樓
  | 
		
			
				 發表於: 星期三 四月 17, 2013 11:18 am    文章主題:  | 
				     | 
			 
			
				
  | 
			 
			
				 	  | garfield 寫到: | 	 		  | 會不會是電腦運算速度變快 | 	  
 
有試著在舊主機Win98上安裝VFP6(其實是將開發程式用複製方式到主機),但結果還是一樣~
 
 
 	  | garfield 寫到: | 	 		  | 你試著加一個 inkey(3) 等3秒後才去執行下一個動作 | 	  
 
 
這個方式我同樣也有做過了,但結果還是一樣Time Out   
 
 
另外在Recv有延長時間∼
 
Seconds() - nSec = 2秒 改成 5秒
 
發生當機∼   
 
 
 
可以請教DOEVENTS使用時機嗎? | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
  	 
	    
  	   | 
 	
您 無法 在這個版面發表文章 您 無法 在這個版面回覆文章 您 無法 在這個版面編輯文章 您 無法 在這個版面刪除文章 您 無法 在這個版面進行投票 您 無法 在這個版面附加檔案 您 無法 在這個版面下載檔案
  | 
   
  
		 |