Nice to meet you.
This is Shenzhen Zmotion Technology Co., Ltd .
Let’s do heartbeat detection in C# based one network motion control card.
1. Basic Introduction & Principle
(1) Hardware Motion Control Card Introduction
(2) Why to Do Heartbeat Detection
2. Heartbeat Detection Programming with One Routine
(1) Before C# Development: Zmotion PC C# Library File & Commands
A. How to Use Zmotion PC C# Library Files?
B. Zmotion PC Programming Commands--
(2) One C# Heartbeat Detection Routine Development & Usage
A. Routine PART ONE: Heartbeat Detection Script by BASIC
B. Routine PART TWO: Heartbeat Detection Script by C#
1. Basic Introduction & Principle
The heartbeat detection function here mainly belongs to motion control area. For Zmotion, which focuses on “general motion control technology” over 10 years, there are many kinds of motion controllers and cards. Therefore, today, use our ECI series cards as the example, specifically, ECI2618B .
(1) Hardware Motion Control Card “ECI2618B” Introduction
ECI2618B is one economical multi-axis pulse motion control card. Itself supports 6 pulse axes at most, but it can be expanded to 12 axes.
It supports all kinds of simple trajectory control, like, linear interpolation, any circular interpolation, space arc, helical interpolation, electronic cam, electronic gear, synchronous follow, virtual axis, robot, etc., when optimized network communication protocol is used, real-time motion control can be achieved easily.
It can be known it has enough interfaces. Let’s see some corresponding features.
ü motion control of 6 differential pulse axes + 4 single-ended pulse axes (up to 12 axes by expansion module)
ü pulse output mode: pulse / direction or dual pulses
ü AXIS interface supports encoder position measurement, which can be configured as handwheel input mode
ü one specialized handwheel “Handwheel” input interface
ü maximum pulse frequency output of each axis: 10MHZ
ü 256 isolation inputs & 256 isolation outputs can be extended at most through CAN bus.
ü axis position limit signal / origin signal port can be configured as any input at will.
ü the maximum output current of general digital outputs can reach 500mA, which can directly drive some kinds of solenoid valves.
ü multi-file and multi-task programming in ZBasic (RTBasic).
ü a variety of program encryption methods to protect your intellectual property rights.
ECI2000 series economical multi-axis motion control card can be used in those pulse applications within 12 axes, such as, electronic semiconductor equipment (testing equipment, assembly equipment, locking equipment, soldering machine), dispensing equipment, assembly line, etc.
About programming, it can be edited under all kinds of windows, Window, Linux, Mac, Android, Wince. And provide main PC languages DLL library, C++, C#, labview, python, VC, vb.net, etc., shown in above.
More information, please check “Zmotion PC Programming Manual” and corresponding examples .
(2) Why to Do Heartbeat Detection
Use ECI2618B motion control card to do heartbeat detection, so what it can do?
*Real-Time Monitor*
*Keep Stable*
*Full Data*
*Help Diagnosis*
It can monitor whether the host computer is normally connected and communicated through communication state between host computer and slave computer. When it powered down, system can know and solve immediately. In this way, it can avoid communication interrupt / data lost caused by power-down. Then, the stability of system can be ensured.
2. Heartbeat Detection Programming with One Routine
To make it clear, here will show one heartbeat detection C# example.
Next, how to program, develop and use it.
(1) Before C# Development: Zmotion PC C# Library File & Commands
A. How to Use Zmotion PC C# Library Files?
Step 1: as we all know, the first step is to create new WinForm project.
Step 2: select “Visual C#” – “.NET Framework 4” – “Windows Application Program”.
Step 3: the most important step is to get Zmotion C# function library.
You can obtain from Zmotion website – Download – Library File / contact us directly.
When obtained, please do followings.
a. copy “zmcaux.cs” file into new built project.
b. copy “zauxdll.dll” & “Zmotion.dll” files into folder bin\debug.
c. open new project in VS, then show all files, find “zmcaux.cs”, click “include in project (J)”.
d. double-click Form 1 of Form1.cs, then code editing interface appears. You write “using cszmcaux” at the beginning and state the controller handle “g_handle”.
B. Zmotion PC Programming Commands
In “zmcaux.cs”, it shows Zmotion all PC commands. You can know the meaning and syntax usage.
Full information, please refer to “Zmotion PC Programming Manual”.
For example, use ZAux_OpenEth to connect to controller and get connection handle.
*Download the Program to Run*
*Set Controller Program Parameters*
(2) One C# Heartbeat Detection Routine Development & Usage
After that, it is time to do development.
At first, there are 2 parts mainly: “Host Computer C# Program -- Modify Variables Cyclically & Controller BASIC Program -- Check Variables Periodically and Emergency Stop when Power Down”.
A. Routine PART ONE: Heartbeat Detection Script by BASIC
Steps are shown above. Specifically, the host computer will modify Heart_Status periodically, then the control program will scan Heart_Status cyclically, at the same time, when it knows the variable is abnormal, it will open the OUT port to simulate “emergency stop in power down”.
That is, we need to define some variables and edit scan program by BASIC in RTSys (RTSys is Zmotion one free IDE development software, in RTSys, it can develop, compile and debug, for Zmotion products, use RTBasic to edit. For RTSys, you can refer to former article: One Free IDE Motion Control Development Software RTSys for You ).
Therefore, here, make two files for variables definition and scanning.
a. create variables
'*************************************define heartbeat related variables************************ Global Heart_StarFlag 'starting communication flag variable Global Heart_Status 'communication flag variable Global Heart_Time 'heartbeat timeout time Global Heart_OutNum 'how many emergency stop OP Global OutList_Address 'emergency stop OUT modbus_long starting address (Ps: below ZMC3XX series, customized variables are 24-bit floating, for 32-bit, the precision will be not enough, use modbus_long instead) GLOBAL CONST cVer=CONTROL Heart_StarFlag =-1 Heart_Status = 0 Heart_OutNum = 10 OutList_Address =0 dim i,j
b. scanning
The scanning processes:
Step 1: wait “start to scan” command from host computer.
Step 2: enter different scanning program according to controller signal.
Step 3: set communication flag variable as 1.
Step 4: wait one “timeout” time.
Step 5: judge communication flag. 1 = no communication, in power down state, do power-down protection operation. 0 = with communication, normal, do next scanning.
WHILE TRUE WAIT until Heart_StarFlag = 1 'wait PC software to connect DELAY Heart_Time 'when PC connected, delay one timeout time WHILE TRUE if Heart_Status = 0 then Heart_Status = 1 'wait "modify communication variables by PC" ticks = Heart_Time 'judge timeout, waiting, modify Heart_Flag = 0 WAIT until ticks < 0 or Heart_Status=0 if Heart_Status = 1 THEN RAPIDSTOP(2) WAIT IDLE OP(0,31,MODBUS_LONG(OutList_Address)) 'MODBUS_LONG saves 32-bit integer, control emergency stop IO bit by bit Heart_StarFlag=-1 Heart_Status = 0 EXIT WHILE ELSE OP(0,31,0) Endif WEND WEND END
B. Routine PART TWO: Heartbeat Detection Script by C#
In heartbeat detection, host computer C# has below main functions:
*download and run controller program*
*set scanning parameters*
*ON & OFF heartbeat detection*
*set emergency stop IO*
a. Heartbeat Detection Routine Main Interface
b. Heartbeat Detection Routine C# Description
① connect to controller, get the handle, then PC C# can operate the controller through obtained handle.
//connect to controller, controller default IP is 192.168.0.11 ZauxErr = zmcaux.ZAux openEth("192.168.0.11",out g Handle); if (0 != ZauxErr) { AlmInifFile.Write(DateTime.Now.Tostring("F"), "ZAux openEth Failed, Error Code:” + ZauxErr.ToString(), “Error Info”); }
② download heartbeat BASIC program
Firstly, use OpenFileDialog command to get controller program path.
Secondly, use OpenFile command to judge whether the file is empty or not.
Thirdly, use ZAux_BasDown to download program into controller.
private void btnFile_Click(object sender, EventArgs e) { string m_strOpenFileName; FileStream openFile; OpenFileDialog fileOpen = new OpenFileDialog(); fileOpen.Filter = "Basic Files (*.bas)|*.bas|All Files (*.*)|*.*"; fileOpen.RestoreDirectory = true; fileOpen.FilterIndex = 1; if (fileOpen.ShowDialog() == DialogResult.OK) { m_strOpenFileName = fileOpen.FileName; try{ openFile = new FileStream(fileOpen.FileName, FileMode.Open, FileAccess.Read); openFile.Close(); } catch (Exception ex) { MessageBox.Show("Open Failed!"); return; } ret = zmcaux.ZAux_BasDown(g_handle, m_strOpenFileName, 0); controlReturnQue.Enqueue(ret); if (ret == 0) MessageBox.Show("Heartbeat Program Loaded"); } }
③ open heartbeat detection
Firstly, use SetTimeOutPara command to set scanning parameters.
Secondly, use ZAux_Direct_SetUserVar command to set Heart_StarFlag heartbeat flag variable.
Thirdly, open the controller scanning program, and open PC scanning program.
private void btnStart_Click(object sender, EventArgs e) { SetTimeOutPara(); ret = zmcaux.ZAux_Direct_SetUserVar(g_handle, "Heart_StarFlag", 1); controlReturnQue.Enqueue(ret); timeOut = 0; timer1.Enabled = true; timer1.Interval = int.Parse(tbxUpTimer.Text); tbState.Text = "Opened"; } //host computer scanning program private void timer1_Tick(object sender, EventArgs e) { ret = zmcaux.ZAux_Direct_SetUserVar(g_handle, "Heart_Status", 0); controlReturnQue.Enqueue(ret); if (ret != 0) timeOut++; if (timeOut > 1) { tbState.Text = "Not Opened"; timer1.Stop(); } }
④ check in RTSys
Firstly, connect to controller in RTSys (controller default IP: 192.168.0.11)
Secondly, open OUT interface, you can observe whether it is power down through OUT.