Delphi 原生對串口支持有限,而 VB 的 MSComm 控件 功能豐富,提供事件驅動、全雙工通訊。本文介紹將 MSComm 作為 ActiveX 控件導入 Delphi 的完整步驟。
Mscomm.regMscomm32.ocxMscomm32.depregsvr32 註冊 OCX:
regsvr32 C:\Windows\System32\Mscomm32.ocx
Mscomm.reg,在 HKEY_CLASSES_ROOT\Licenses 下建立鍵值:
[HKEY_CLASSES_ROOT\Licenses\4250E830-6AC2-11cf-8ADB-00AA00C00905]
@="kjljvjjjoquqmjjjvpQQkqmqykypoqjquoun"
OnComm 處理接收數據、錯誤等。PortOpen = True 前需配置 Settings(如 "9600,n,8,1")、CommPort、InputMode 等。Output 屬性,接收用 Input 或 InputLen。MSCom 是 32 位 ActiveX,僅適用於 32 位 Delphi 應用。64 位程序需改用原生 API(CreateFile/ReadFile/WriteFile)或第三方庫(如 ComPort)。