实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region "Processing of Open button"
private void btn_Open_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
int iLogicalStationNumber; //LogicalStationNumber for ActUtlType
//Displayed output data is cleared.
ClearDisplay();
//
//Processing of Open method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//Set the value of 'UnitType' to the property(UNIT_QNUSB).
axActProgType1.ActUnitType = 0x16;
//Set the value of 'ProtocolType' to the property(PROTOCOL_USB).
axActProgType1.ActProtocolType = 0x0D;
//Set the value of 'Password'.
axActProgType1.ActPassword = txt_Password.Text;
//The Open method is executed.
iReturnCode = axActProgType1.Open();
//When the Open method is succeeded, make the EventHandler of ActProgType Controle.
if (iReturnCode == 0)
{
txt_LogicalStationNumber.Enabled = false;
}
}
//When ActUtlType is selected by the radio button,
else
{
//Check the 'LogicalStationNumber'.(If succeeded, the value is gotten.)
if (GetIntValue(txt_LogicalStationNumber, out iLogicalStationNumber) != true)
{
//If failed, this process is end.
return;
}
//Set the value of 'LogicalStationNumber' to the property.
axActUtlType1.ActLogicalStationNumber = iLogicalStationNumber;
//Set the value of 'Password'.
axActUtlType1.ActPassword = txt_Password.Text;
//The Open method is executed.
iReturnCode = axActUtlType1.Open();
//When the Open method is succeeded, disable the TextBox of 'LogocalStationNumber'.
//When the Open method is succeeded, make the EventHandler of ActUtlType Controle.
if (iReturnCode == 0)
{
txt_LogicalStationNumber.Enabled = false;
}
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message,
Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode);
}
#endregion
#region "Processing of Close button"
private void btn_Close_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
//Displayed output data is cleared.
ClearDisplay();
//
//Processing of Close method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//The Close method is executed.
iReturnCode = axActProgType1.Close();
}
//When ActUtlType is selected by the radio button,
else
{
//The Close method is executed.
iReturnCode = axActUtlType1.Close();
//When The Close method is succeeded, enable the TextBox of 'LogocalStationNumber'.
if (iReturnCode == 0)
{
txt_LogicalStationNumber.Enabled = true;
}
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message,
Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode);
}
#endregion
#region "Processing of ReadDeviceRandom2 button"
private void btn_ReadDeviceRandom2_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
String szDeviceName = ""; //List data for 'DeviceName'
int iNumberOfData = 0; //Data for 'DeviceSize'
short[] arrDeviceValue; //Data for 'DeviceValue
int iNumber; //Loop counter
System.String[] arrData; //Array for 'Data'
//Displayed output data is cleared.
ClearDisplay();
//Get the list of 'DeviceName'.
// Join each line(StringType array) of 'DeviceName' by the separator '\n',
// and create a joined string data.
szDeviceName = String.Join("\n", txt_DeviceNameRandom.Lines);
//Check the 'DeviceSize'.(If succeeded, the value is gotten.)
if (!GetIntValue(txt_DeviceSizeRandom, out iNumberOfData))
{
//If failed, this process is end.
return;
}
//Assign the array for 'DeviceValue'.
arrDeviceValue = new short[iNumberOfData];
//
//Processing of ReadDeviceRandom2 method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//The ReadDeviceRandom2 method is executed.
iReturnCode = axActProgType1.ReadDeviceRandom2(szDeviceName,
iNumberOfData,
out arrDeviceValue[0]);
}
//When ActUtlType is selected by the radio button,
else
{
//The ReadDeviceRandom2 method is executed.
iReturnCode = axActUtlType1.ReadDeviceRandom2(szDeviceName,
iNumberOfData,
out arrDeviceValue[0]);
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message, Name,
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8}", iReturnCode);
//
//Display the read data
//
//When the ReadDeviceRandom2 method is succeeded, display the read data.
if (iReturnCode == 0)
{
//Assign the array for the read data.
arrData = new System.String[iNumberOfData];
//Copy the read data to the 'arrData'.
for (iNumber = 0; iNumber < iNumberOfData; iNumber )
{
arrData[iNumber] = arrDeviceValue[iNumber].ToString();
}
//Set the read data to the 'Data', and display it.
txt_Data.Lines = arrData;
}
}
#endregion
#region "Processing of WriteDeviceRandom2 button"
private void btn_WriteDeviceRandom2_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
String szDeviceName = ""; //List data for 'DeviceName'
int iNumberOfData = 0; //Data for 'DeviceSize'
short[] arrDeviceValue; //Data for 'DeviceValue'
int iNumber; //Loop counter
//Displayed output data is cleared.
ClearDisplay();
//Get the list of 'DeviceName'.
// Join each line(StringType array) of 'DeviceName' by the separator '\n',
// and create a joined string data.
szDeviceName = String.Join("\n", txt_DeviceNameRandom.Lines);
//Check the 'DeviceSize'.(If succeeded, the value is gotten.)
if (!GetIntValue(txt_DeviceSizeRandom, out iNumberOfData))
{
//If failed, this process is end.
return;
}
//Check the 'DeviceValue'.(If succeeded, the value is gotten.)
arrDeviceValue = new short[iNumberOfData];
if (!GetShortArray(txt_DeviceDataRandom, out arrDeviceValue))
{
//If failed, this process is end.
return;
}
//Set the 'DeviceVale'.
for (iNumber = 0; iNumber < iNumberOfData; iNumber )
{
arrDeviceValue[iNumber] = arrDeviceValue[iNumber];
}
//
//Processing of WriteDeviceRandom2 method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//The WriteDeviceRandom2 method is executed.
iReturnCode = axActProgType1.WriteDeviceRandom2(szDeviceName,
iNumberOfData,
ref arrDeviceValue[0]);
}
//When ActUtlType is selected by the radio button,
else
{
//The WriteDeviceRandom2 method is executed.
iReturnCode = axActUtlType1.WriteDeviceRandom2(szDeviceName,
iNumberOfData,
ref arrDeviceValue[0]);
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message, Name,
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8}", iReturnCode);
}
#endregion
#region "Processing of ReadDeviceBlock2 button"
private void btn_ReadDeviceBlock2_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
String szDeviceName = ""; //List data for 'DeviceName'
int iNumberOfData = 0; //Data for 'iNumberOfData'
short[] arrDeviceValue; //Data for 'DeviceValue'
int iNumber; //Loop counter
System.String[] arrData; //Array for 'Data'
//Displayed output data is cleared.
ClearDisplay();
//Get the list of 'DeviceName'.
// Join each line(StringType array) of 'DeviceName' by the separator '\n',
// and create a joined string data.
szDeviceName = String.Join("\n", txt_DeviceNameBlock.Lines);
//Check the 'DeviceSize'.(If succeeded, the value is gotten.)
if (!GetIntValue(txt_DeviceSizeBlock, out iNumberOfData))
{
//If failed, this process is end.
return;
}
//Assign the array for 'DeviceValue'.
arrDeviceValue = new short[iNumberOfData];
//
//Processing of ReadDeviceBlock2 method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//The ReadDeviceBlock2 method is executed.
iReturnCode = axActProgType1.ReadDeviceBlock2(szDeviceName,
iNumberOfData,
out arrDeviceValue[0]);
}
//When ActUtlType is selected by the radio button,
else
{
//The ReadDeviceBlock2 method is executed.
iReturnCode = axActUtlType1.ReadDeviceBlock2(szDeviceName,
iNumberOfData,
out arrDeviceValue[0]);
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message, Name,
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8}", iReturnCode);
//
//Display the read data
//
//When the ReadDeviceBlock2 method is succeeded, display the read data.
if (iReturnCode == 0)
{
//Assign array for the read data.
arrData = new System.String[iNumberOfData];
//Copy the read data to the 'arrData'.
for (iNumber = 0; iNumber < iNumberOfData; iNumber )
{
arrData[iNumber] = arrDeviceValue[iNumber].ToString();
}
//Set the read data to the 'Data', and display it.
txt_Data.Lines = arrData;
}
}
#endregion
#region "Processing of WriteDeviceBlock2 button"
private void btn_WriteDeviceBlock2_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
String szDeviceName = ""; //List data for 'DeviceName'
int iNumberOfData = 0; //Data for 'DeviceSize'
short[] arrDeviceValue; //Data for 'DeviceValue'
int iNumber; //Loop counter
int iSizeOfIntArray; //
//Displayed output data is cleared.
ClearDisplay();
//Get the list of 'DeviceName'.
// Join each line(StringType array) of 'DeviceName' by the separator '\n',
// and create a joined string data.
szDeviceName = String.Join("\n", txt_DeviceNameBlock.Lines);
//Check the 'DeviceSize'.(If succeeded, the value is gotten.)
if (!GetIntValue(txt_DeviceSizeBlock, out iNumberOfData))
{
//If failed, this process is end.
return;
}
//Get size for 'DeviceValue'
iSizeOfIntArray = txt_DeviceDataBlock.Lines.Length;
//Assign the array for 'DeviceValue'.
arrDeviceValue = new short[iNumberOfData];
//Convert the 'DeviceValue'.
for (iNumber = 0; iNumber < iSizeOfIntArray; iNumber )
{
try
{
arrDeviceValue[iNumber]
= Convert.ToInt16(txt_DeviceDataBlock.Lines[iNumber]);
}
//Exception processing
catch (Exception exExcepion)
{
MessageBox.Show(exExcepion.Message,
Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
//
//Processing of WriteDeviceBlock2 method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//The WriteDeviceRandom2 method is executed.
iReturnCode = axActProgType1.WriteDeviceBlock2(szDeviceName,
iNumberOfData,
ref arrDeviceValue[0]);
}
//When ActUtlType is selected by the radio button,
else
{
//The WriteDeviceRandom2 method is executed.
iReturnCode = axActUtlType1.WriteDeviceBlock2(szDeviceName,
iNumberOfData,
ref arrDeviceValue[0]);
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message, Name,
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode);
}
#endregion
#region "Processing of EntryDeviceStatus button"
private void btn_EntryDeviceStatus_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
String szDeviceName = ""; //List data for 'DeviceName'
int iNumberOfData = 0; //Data for 'DeviceSize'
int iMonitorCycle = 0; //Data for 'MonitorCycle'
int[] arrDeviceValue; //Data for 'DeviceValue'
//Displayed output data is cleared.
ClearDisplay();
//Get the list of 'DeviceName'.
// Join each line(StringType array) of 'DeviceName' by the separator '\n',
// and create a joined string data.
szDeviceName = String.Join("\n", txt_DeviceNameStatus.Lines);
//Check the 'DeviceSize'.(If succeeded, the value is gotten.)
if (!GetIntValue(txt_DeviceSizeStatus, out iNumberOfData))
{
//If failed, this process is end.
return;
}
//Check the 'MonitorCycle'.(If succeeded, the value is gotten.)
if (!GetIntValue(txt_MonitorCycleStatus, out iMonitorCycle))
{
//If failed, this process is end.
return;
}
//Check the 'DeviceValue'.(If succeeded, the value is gotten.)
arrDeviceValue = new int[iNumberOfData];
if (!GetIntArray(txt_DeviceDataStatus, out arrDeviceValue))
{
//If failed, this process is end.
return;
}
//
//Processing of EntryDeviceStatus method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//The EntryDeviceStatus method is executed.
iReturnCode = axActProgType1.EntryDeviceStatus(szDeviceName,
iNumberOfData,
iMonitorCycle,
ref arrDeviceValue[0]);
}
//When ActUtlType is selected by the radio button,
else
{
//The EntryDeviceStatus method is executed.
iReturnCode = axActUtlType1.EntryDeviceStatus(szDeviceName,
iNumberOfData,
iMonitorCycle,
ref arrDeviceValue[0]);
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message, Name,
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode);
}
#endregion
#region "Processing of FreeDeviceStatus button "
private void btn_FreeDeviceStatus_Click(object sender, EventArgs e)
{
int iReturnCode; //Return code
//Displayed output data is cleared.
ClearDisplay();
//
//Processing of FreeDeviceStatus method
//
try
{
//When ActProgType is selected by the radio button,
if (rad_ActProgType.Checked)
{
//The FreeDeviceStatus method is executed.
iReturnCode = axActProgType1.FreeDeviceStatus();
}
//When ActUtlType is selected by the radio button,
else
{
//The FreeDeviceStatus method is executed.
iReturnCode = axActUtlType1.FreeDeviceStatus();
}
}
//Exception processing
catch (Exception exception)
{
MessageBox.Show(exception.Message,
Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode);
}
#endregion
#region "Processing of OnDeviceStatus for ActUtlType Controle"
private void ActUtlType1_OnDeviceStatus(System.Object sender, AxActUtlTypeLib._IActUtlTypeEvents_OnDeviceStatusEvent e)
{
System.String[] arrData;
//Assign the array for editing the data of 'Data'.
arrData = new System.String[txt_Data.Lines.Length 1];
//Set the lateset data of 'Data' to arrData.
Array.Copy(txt_Data.Lines, arrData, txt_Data.Lines.Length);
//Add the content of new event to arrData.
arrData[txt_Data.Lines.Length]
= String.Format("OnDeviceStatus event by ActUtlType [{0}={1}]", e.szDevice, e.lData);
//The new 'Data' is displayed.
txt_Data.Lines = arrData;
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8}", e.lReturnCode);
}
#endregion
#region "Processing of OnDeviceStatus for ActProgType Controle"
private void ActProgType1_OnDeviceStatus(System.Object sender, AxActProgTypeLib._IActProgTypeEvents_OnDeviceStatusEvent e)
{
System.String[] arrData;
//Assign the array for editing the data of 'Data'.
arrData = new System.String[txt_Data.Lines.Length 1];
//Set the lateset data of 'Data' to arrData.
Array.Copy(txt_Data.Lines, arrData, txt_Data.Lines.Length);
//Add the content of new event to arrData.
arrData[txt_Data.Lines.Length]
= String.Format("OnDeviceStatus event by ActProgType [{0}={1}]", e.szDevice, e.lData);
//The new 'Data' is displayed.
txt_Data.Lines = arrData;
//The return code of the method is displayed by the hexadecimal.
txt_ReturnCode.Text = String.Format("0x{0:x8}", e.lReturnCode);
}
#endregion
#region "Processing of getting ShortType array from StringType array of multiline TextBox"
private bool GetShortArray(TextBox lptxt_SourceOfShortArray, out short[] lplpshShortArrayValue)
{
int iSizeOfShortArray; //Size of ShortType array
int iNumber; //Loop counter
//Get the size of ShortType array.
iSizeOfShortArray = lptxt_SourceOfShortArray.Lines.Length;
lplpshShortArrayValue = new short[iSizeOfShortArray];
//Get each element of ShortType array.
for (iNumber = 0; iNumber < iSizeOfShortArray; iNumber )
{
try
{
lplpshShortArrayValue[iNumber]
= Convert.ToInt16(lptxt_SourceOfShortArray.Lines[iNumber]);
}
//When the value is nothing or out of the range, the exception is processed.
catch (Exception exExcepion)
{
MessageBox.Show(exExcepion.Message,
Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
//Normal End
return true;
}
#endregion
#region "Processing of getting IntType array from StringType array of multiline TextBox"
private bool GetIntArray(TextBox lptxt_SourceOfIntArray, out int[] lplpiIntArrayValue)
{
int iSizeOfIntArray; //Size of IntType array
int iNumber; //Loop counter
//Get the size of IntType array.
iSizeOfIntArray = lptxt_SourceOfIntArray.Lines.Length;
lplpiIntArrayValue = new int[iSizeOfIntArray];
//Get each element of IntType array.
for (iNumber = 0; iNumber < iSizeOfIntArray; iNumber )
{
try
{
lplpiIntArrayValue[iNumber]
= Convert.ToInt32(lptxt_SourceOfIntArray.Lines[iNumber]);
}
//When the value is nothing or out of the range, the exception is processed.
catch (Exception exExcepion)
{
MessageBox.Show(exExcepion.Message,
Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
//Normal End
return true;
}
#endregion
#region "Processing of getting 32bit integer from TextBox"
private bool GetIntValue(TextBox lptxt_SourceOfIntValue, out int iGottenIntValue)
{
iGottenIntValue = 0;
//Get the value as 32bit integer from a TextBox
try
{
iGottenIntValue = Convert.ToInt32(lptxt_SourceOfIntValue.Text);
}
//When the value is nothing or out of the range, the exception is processed.
catch (Exception exExcepion)
{
MessageBox.Show(exExcepion.Message,
Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
//Normal End
return true;
}
#endregion
#region "Processing of clear displayed output data"
private void ClearDisplay()
{
//Clear TextBox of 'ReturnCode','Data'
txt_ReturnCode.Text = "";
txt_Data.Text = "";
}
#endregion
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论