Professional Documents
Culture Documents
C# Melsec MxComponent V4 #3 Coding (Sending and Receiving in Double Word Units)
C# Melsec MxComponent V4 #3 Coding (Sending and Receiving in Double Word Units)
home economy cryptocurrency C# PLC & MCU game writing related Encyclop
C# Melsec MxComponent V4 #3
Coding (Sending and receiving i...
sheepone 2021. 7. 6. 10:02
See all categories
C#
technology development
Vision
WPF
Xamarin
MCU & PLC
writing related
cryptocurrency
economy
game
Odin: Valhalla Rising
Axie Infinity
POE
Diablo 2 Resurrection
Encyclopedia
worth reading
python
backpacking
equipment
restaurant
〈 2023. 01 〉
Workmonthfury Numberneck gold saturday
One 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Total
288,526
Today : 2
Yesterday: 810
namespace MxComponentV4
{
public partial class Form1 : Form
{
public static Plc_MxComponentV4 mxComponent ;
public Form1()
{
InitializeComponent();
int iNo = int.Parse(Option.Plc_MxComponentNo);
mxComponent = new Plc_MxComponentV4(iNo);
}
}
}
Configure the MxComponent module
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace MxComponentV4
{
//ActProgTypeLib 로 하면 Communication Setup Utility 안쓰고 오픈 할수 있다
//ActUtlTypeLib 로 하면 Communication Setup Utility 로 등록하고 써야 한다 0~
public class Plc_MxComponentV4
{
public enum CpuStsType
{
RUN = 0,
STOP = 1,
PAUSE = 2
}
this.iLogicalStationNumber = iLogicalStationNumber ;
timer.Interval = 1000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elasp
timer.Enabled = true ;
if (!IsOnline())
{
Debug.WriteLine("!IsOnline() ReOpen Stt");
Open(iLogicalStationNumber);
Debug.WriteLine("!IsOnline() ReOpen End");
}
timer.Enabled = true;
}
/// <summary>
/// Get double word //2Word 4Byte 32bit
/// </summary>
/// <param name="device"></param>
/// <param name="data"></param>
/// <returns></returns>
public int GetDoubleDevice(string device)
{
int[] data = new int[2];
int Yint = -100000;
try
{
actUtlType.ReadDeviceBlock(device, 2, out data[0]);
string value1 = Convert.ToString(data[0], 2);
string value2 = Convert.ToString(data[1], 2);
string bitString = value2 + value1;
Yint = Convert.ToInt32(bitString, 2);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return Yint;
}
/// <summary>
/// Set double word //2Word 4Byte 32bit
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetDoubleDevice(string address, int value)
{
Int32 n = -2;
string nt = Convert.ToString(n, 2);
// Int32 n1 = Convert.ToInt32(nt, 2);
UInt32 n2 = Convert.ToUInt32(nt, 2);
//--------------------------------------------------------------------
//비전 폴더 이름 넘기고 받기
//--------------------------------------------------------------------
//int [] iInt = new int [100];
short[] sInt = new short[100];
if (str.Length % 2 == 0)
{
str_temp = new string[str.Length / 2];
sInt = new short [str_temp.Length];
}
public bool ReadDeviceBlock(string _sAdd, out string _str)
{
sInt = new short[10];
Array.Clear(sInt, 0, sInt.Length);
bool bCheck = ReadDeviceBlock2(_sAdd, sInt.Length, out sInt[0]);
_str = "";
if (bCheck)
{
for (int i = 0; i < sInt.Length; i++)
{
byte[] bytes = BitConverter.GetBytes(sInt[i]);
_str += Encoding.Default.GetString(bytes);
}
}
return bCheck ;
//_str = "" ;
//Array.Clear(sInt, 0, sInt.Length);
//bool bCheck = ReadDeviceBlock2(_sAdd, sInt.Length,out sInt[0]);
//for (int i = 0; i < sInt.Length; i++)
//{
// if(sInt[i] != 0) _str += Convert.ToChar(sInt[i]);
//}
//return bCheck ;
}
return 0;
}
lock (thisLock)
{
try
{
iRst = actUtlType.GetDevice("SM400" , out iData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
bConnected = false ;
return false;
}
this.iLogicalStationNumber = iLogicalStationNumber;
actUtlType.ActLogicalStationNumber = this.iLogicalStationNumber;
actUtlType.ActPassword = "";
Debug.WriteLine("!IsOnline() ReOpen 1");
lock (thisLock)
{
try
{
//ActMLProgType.ActUnitType = iact
//ActMLProgType.Open();
actUtlType.Close();
iRst = actUtlType.Open();
}
catch (Exception ex)
{
Debug.WriteLine("!IsOnline() ReOpen 2");
MessageBox.Show(ex.Message);
}
}
Debug.WriteLine("!IsOnline() ReOpen 3");
if (iRst == 0)
{
//int[] arrDeviceValue = { 1 };
//EntryDeviceStatus("SM400",1,1,ref arrDeviceValue[0]);
//actUtlType.OnDeviceStatus += new ActUtlTypeLib._IActUtlTypeE
return true;
}
else
{
//SetError(iRst);
return false;
}
}
SetError(iRst);
return false ;
}
/// <summary>
/// 디바이스 1 점을 설정합니다 .
/// </summary>
/// <param name="szDevice"></param>
/// <param name="lData"></param>
/// <returns></returns>
public bool SetDevice(string sDevice, int iData)
{
if(!bConnected) return false ;
int iRst = -1;
lock (thisLock)
{
try
{
iRst = actUtlType.SetDevice(sDevice, iData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (iRst == 0) return true;
SetError(iRst);
return false;
}
lock (thisLock)
{
try
{
iRst = actUtlType.GetDevice(sDevice, out lplData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (iRst == 0) return true;
SetError(iRst);
return false;
}
/// <summary>
/// 2 바이트 데이터로 디바이스 1 점을 설정합니다 .
/// </summary>
/// <param name="szDevice"></param>
/// <param name="lData"></param>
/// <returns></returns>
public bool SetDevice2(string szDevice, short lData)
{
if (!bConnected) return false ;
SetError(iRst);
return false;
}
SetError(iRst);
return false ;
}
lock (thisLock)
{
try
{
iRst = actUtlType.ReadDeviceBlock(szDevice, iSize, out lpl
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (iRst == 0) return true;
SetError(iRst);
return false ;
}
/// <summary>
/// 디바이스의 일괄 쓰기를 실행합니다 .
/// </summary>
/// <param name="szDevice">디바이스 명</param>
/// <param name="iSize">쓰기 점수</param>
/// <param name="iData">쓰기 디바이스 값</param>
/// <returns></returns>
public bool WriteDeviceBlock(string szDevice, int iSize, ref int iData
{
if (!bConnected) return false ;
lock (thisLock)
{
try
{
iRst = actUtlType.WriteDeviceBlock(szDevice, iSize, ref iD
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (iRst == 0) return true;
SetError(iRst);
return false ;
}
SetError(iRst);
return false ;
}
/// <summary>
/// 2 바이트 데이터로 디바이스의 일괄 쓰기를 실행합니다 .
/// </summary>
/// <param name="szDevice"></param>
/// <param name="iSize"></param>
/// <param name="iData"></param>
/// <returns></returns>
public bool WriteDeviceBlock2(string szDevice, int iSize, ref short iD
{
if (!bConnected) return false ;
lock (thisLock)
{
try
{
iRst = actUtlType.WriteDeviceBlock2(szDevice, iSize, ref i
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (iRst == 0) return true;
SetError(iRst);
return false ;
}
public bool ReadBuffer(int lStartIO, int lAddress, int lSize, out shor
{
lplData = 0;
lock (thisLock)
{
try
{
iRst = actUtlType.ReadBuffer(lStartIO, lAddress, lSize, ou
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (iRst == 0) return true;
SetError(iRst);
return false ;
}
/// <summary>
/// 특수 기능 모듈 버퍼메모리의 값을 씁니다 .
/// </summary>
/// <param name="lStartIO"></param>
/// <param name="lAddress"></param>
/// <param name="lSize"></param>
/// <param name="lpsData"></param>
/// <returns></returns>
public bool WriteBuffer(int lStartIO, int lAddress, int lSize, ref sho
{
if (!bConnected) return false ;
lock (thisLock)
{
try
{
iRst = actUtlType.WriteBuffer(lStartIO, lAddress, lSize, r
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (iRst == 0) return true;
SetError(iRst);
return false ;
lock (thisLock)
{
try
{
iRst = actUtlType.SetCpuStatus(iOperation);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
SetError(iRst);
return false ;
/// <summary>
/// EntryDeviceStatus FreeDeviceStatus OnDeviceStatusEvent 셋트
/// </summary>
/// <param name="szDeviceList"></param>
/// <param name="lSize"></param>
/// <param name="lMonitorCycle">• iMonitorCycle 은 1 초 ~1 시간의 범위
/// <param name="lplData"></param>
/// <returns></returns>
public bool EntryDeviceStatus(string szDeviceList, int lSize, int lMon
{
if (!bConnected) return false ;
lock (thisLock)
{
try
{
iRst = actUtlType.EntryDeviceStatus(szDeviceList, lSize, l
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
SetError(iRst);
return false ;
}
lock (thisLock)
{
try
{
iRst = actUtlType.FreeDeviceStatus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
SetError(iRst);
return false ;
//--------------------------------------------------------------------
//필요할까 해서 해놧더니 필요 없네
public string StringToHex(string str)
{
var sb = new StringBuilder();
var bytes = Encoding.Unicode.GetBytes(str);
foreach(var t in bytes)
{
sb.Append(t.ToString("X2"));
}
return sb.ToString();
}
return Encoding.Unicode.GetString(bytes);
}
2 Subscribe
Related article
Memory size and range of values XG5000 LS Industrial Systems PLC C# Melsec MxComponent V4 #2
for PC and PLC data types Reversal Contact Understanding... Coding (Including sending,...
2022.05.02 2021.10.28 2021.07.05
1 comment
name password