104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
using RobotNet10.Script.IO;
|
|
using System.Net.Sockets;
|
|
|
|
namespace RobotNet10.ScriptEngine.IO;
|
|
|
|
/// <summary>
|
|
/// Implementation of CC-Link IE connection.
|
|
/// Note: This is a basic implementation. Full CC-Link IE support may require additional libraries.
|
|
/// </summary>
|
|
public class CcLinkIeConnection : ICcLinkIeConnection
|
|
{
|
|
private TcpClient? _tcpClient;
|
|
private NetworkStream? _stream;
|
|
private bool _disposed;
|
|
|
|
public string IpAddress { get; }
|
|
public int StationNumber { get; }
|
|
public bool IsConnected { get; private set; }
|
|
|
|
public CcLinkIeConnection(string ipAddress, int stationNumber = 1)
|
|
{
|
|
IpAddress = ipAddress;
|
|
StationNumber = stationNumber;
|
|
IsConnected = false;
|
|
}
|
|
|
|
public async Task ConnectAsync()
|
|
{
|
|
if (IsConnected)
|
|
return;
|
|
|
|
try
|
|
{
|
|
_tcpClient = new TcpClient();
|
|
await _tcpClient.ConnectAsync(IpAddress, 5007); // Standard CC-Link IE port
|
|
_stream = _tcpClient.GetStream();
|
|
IsConnected = true;
|
|
|
|
// TODO: Implement CC-Link IE handshake protocol
|
|
// This requires implementing the CC-Link IE protocol stack
|
|
}
|
|
catch
|
|
{
|
|
Disconnect();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Task DisconnectAsync()
|
|
{
|
|
Disconnect();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void Disconnect()
|
|
{
|
|
IsConnected = false;
|
|
_stream?.Close();
|
|
_stream = null;
|
|
_tcpClient?.Close();
|
|
_tcpClient?.Dispose();
|
|
_tcpClient = null;
|
|
}
|
|
|
|
public async Task<ushort[]> ReadAsync(int address, int length)
|
|
{
|
|
EnsureConnected();
|
|
|
|
// TODO: Implement CC-Link IE read operation
|
|
// This requires implementing the CC-Link IE protocol
|
|
await Task.CompletedTask;
|
|
throw new NotImplementedException("CC-Link IE read operation is not yet fully implemented. Full CC-Link IE support requires additional protocol implementation.");
|
|
}
|
|
|
|
public async Task WriteAsync(int address, ushort[] data)
|
|
{
|
|
EnsureConnected();
|
|
|
|
// TODO: Implement CC-Link IE write operation
|
|
// This requires implementing the CC-Link IE protocol
|
|
await Task.CompletedTask;
|
|
throw new NotImplementedException("CC-Link IE write operation is not yet fully implemented. Full CC-Link IE support requires additional protocol implementation.");
|
|
}
|
|
|
|
private void EnsureConnected()
|
|
{
|
|
if (!IsConnected || _stream == null)
|
|
{
|
|
throw new InvalidOperationException("CC-Link IE connection is not connected. Call ConnectAsync() first.");
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
Disconnect();
|
|
_disposed = true;
|
|
}
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|
|
|