A method to identify and control a mechatronic system in real time environment is presented in this paper. Acquiring the system's transfer function accurately can be difficult if some parameters are missing. This causes a great difficulty in the system modeling and control. Therefore in this research, a method based on Hardware-in-the-Loop (HIL) concept has been employed to identify and control dynamic systems. Online identification using Auto Regressive Moving Average (ARMA) models is used to identify the transfer function of the system; the controller is tuned on the identified model in a simulation environment and applied to the real system. The advantages of the proposed method are high accuracy in the identified system, simplicity, and low cost. Two case studies, namely, a DC motor and servo-pneumatic system, were used to verify the proposed method.