Exemplo usando JNA na implementacao da versao da dll.
? public class CLP
{
#region Variáveis Globais
public int hCom=0; // Handle para o canal serial
public int Erro=0; // Codigo do ultimo erro ocorrido
private int timeout=0; // Timeout para cada comunicação - em milissegundos
private int Address=0; // Numero do clp na rede Atos
private string TXBuffer=""; // Retorna o frame enviado para o clp
private string RXBuffer=""; // Retorna o frame recebido do clp
public byte ValorB=0; // Byte utilizado para operações de liga/desliga estados internos
public UInt16 ValorM = 0; // Área de 2 bytes de memória para operações de envia/pede variável de maneira genérica
public byte[] ValorMB = { 0,0,0,0,0,0,0,0 }; // Área de 8 bytes de memória para operações de envia/pede bloco
public int ValorI=0; // Número inteiro para enviar/receber variável do clp em formato binário
public int ValorIBCD=0; // Número inteiro para enviar/receber variáveis do clp no seu formato default (BCD)
public int CanalOk=0; // Vai indicar se o canal serial foi aberto com sucesso
private byte[] Versao = {0,0,0}; // Recebe a versao da dll
#endregion
#region Mapeamento das funções em código não gerenciado
/* Mapeamento das funções em código não gerenciado */
[DllImport("atos2pc.dll")]
private static extern void GetDllVersion(byte[] Versao);
[DllImport("atos2pc.dll")]
private static extern int GetChannel(ref int hCom, ref int Erro, int Canal, int Timeout, int BaudRate, int StopBits, int DataBits, byte Paridade);
[DllImport("atos2pc.dll")]
private static extern int ReleaseChannel(int hCom);
[DllImport("atos2pc.dll")]
private static extern int EnviaAck(int PlcAddress, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int PedeByte(int PlcAddress, int MemoryAddress, ref byte ValorB,int hCom,int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int EnviaByte(int PlcAddress, int MemoryAddress, byte ValorB, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int PedeVar(int PlcAddress, int MemoryAddress, ref UInt16 ValorM, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int EnviaVar(int PlcAddress, int MemoryAddress, byte[] ValorM, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int PedeVarInt(int PlcAddress, int MemoryAddress, ref int ValorI, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int EnviaVarInt(int PlcAddress, int MemoryAddress, int ValorI, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int PedeVarIntBcd(int PlcAddress, int MemoryAddress, ref int ValorIBcd, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int EnviaVarIntBcd(int PlcAddress, int MemoryAddress, int ValorIBcd, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int PedeBloc(int PlcAddress, int MemoryAddress, int Quantidade, ref byte[] ValorMB, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
[DllImport("atos2pc.dll")]
private static extern int EnviaBloc(int PlcAddress, int MemoryAddress, int Quantidade, byte[] ValorMB, int hCom, int Timeout, ref int Erro, string TXBuffer, string RXBuffer);
#endregion
#region Métodos expostos pela classe CLP
public CLP(Serial SerialPort, int DefaultTimeout, int Address)
{
// PLC address
this.Address = Address;
// Timeout for PLC communications
this.timeout = DefaultTimeout;
this.CanalOk = GetChannel(ref this.hCom, ref this.Erro, (int)SerialPort.Channel, this.timeout,(int)SerialPort.BaudRate, (int)SerialPort.StopBits, (int)SerialPort.DataBits, (byte)SerialPort.Parity);
}
~CLP()
{
ReleaseChannel(this.hCom);
}
public string GetDLLVersion()
{
ASCIIEncoding ascii = new ASCIIEncoding();
GetDllVersion(this.Versao);
return (ascii.GetString(this.Versao));
}
/// <summary>
/// Testa comunicação com o CLP
/// </summary>
/// <returns>TRUE=Comunicação OK / FALSE=Falha na comunicação</returns>
public bool SendAck()
{
try
{
if (1 == EnviaAck(this.Address, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
catch
{
return false;
}
}
public bool GetByte(int MemoryAddress)
{
if (1 == PedeByte(this.Address, MemoryAddress, ref this.ValorB, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
public bool SendByte(byte B, int MemoryAddress)
{
if (1 == EnviaByte(this.Address, MemoryAddress, B, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
public bool GetVar(int MemoryAddress)
{
byte[] var = {0,0};
byte[] inv_var = { 0, 0 };
UInt16 val = 0;
if (1 == PedeVar(this.Address, MemoryAddress, ref val, this.hCom, timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
var = BitConverter.GetBytes(val);
inv_var[0] = var[1]; inv_var[1] = var[0];
this.ValorM = BitConverter.ToUInt16(inv_var, 0);
return true;
}
else
return false;
}
public bool SendVar(ushort Var, int MemoryAddress)
{
byte[] var = { 0, 0 };
byte[] inv_var = { 0, 0 };
var = BitConverter.GetBytes(Var);
inv_var[0] = var[1];
inv_var[1] = var[0];
if (1==EnviaVar(this.Address, MemoryAddress, inv_var, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
} else
return false;
}
public bool GetIntVar(int MemoryAddress)
{
if (1 == PedeVarInt(this.Address, MemoryAddress, ref this.ValorI, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
public bool SendIntVar(int Var, int MemoryAddress)
{
if (1 == EnviaVarInt(this.Address, MemoryAddress, Var, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
public bool GetBCDIntVar(int MemoryAddress)
{
if (1 == PedeVarIntBcd(this.Address, MemoryAddress, ref this.ValorIBCD, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
public bool SendBCDIntVar(int Var, int MemoryAddress)
{
if (1 == EnviaVarIntBcd(this.Address, MemoryAddress, Var, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
public bool GetBlock(int MemoryAddress, int Length)
{
if (1 == PedeBloc(this.Address, MemoryAddress, Length, ref ValorMB, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
return false;
}
public bool SendBlock(int MemoryAddress, byte[] Block, int Length)
{
if (1 == EnviaBloc(this.Address, MemoryAddress, Length, Block, this.hCom, this.timeout, ref this.Erro, this.TXBuffer, this.RXBuffer))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Liga ou desliga um EI (estado interno) do CLP.
/// </summary>
/// <param name="MemoryAddress">Endereço do EI</param>
/// <param name="on">TRUE=ligado / FALSE=desligado</param>
/// <returns></returns>
Gostaria da implementacao de apenas um metodo.