Building C sharp Modbus simply needs to return watt hours consumption

S

Thread Starter

Sean Allen

Trying to create an application in c sharp to return the watt hours consumed. The meter is a DMMs 350 and the registry is 40435 which I was told will return the value needed once translated from BCD to decimal.
 
S
another note is, connecting via TCP. The examples I have tried from codeproject, just seems to be so much overhead on the code.
I connect without issue><pre>
<code>
private static ushort _timeout = 500;
private static ushort _refresh = 10;
private static bool _connected = false;

private Socket tcpAsyCl;
private byte[] tcpAsyClBuffer = new byte[2048];

private Socket tcpSynCl;
private byte[] tcpSynClBuffer = new byte[2048];


public void connect(string ip, ushort port)
{
try
{
IPAddress _ip;
if (IPAddress.TryParse(ip, out _ip) == false)
{
IPHostEntry hst = Dns.GetHostEntry(ip);
ip = hst.AddressList[0].ToString();
}
// ----------------------------------------------------------------
// Connect asynchronous client
tcpAsyCl = new Socket(IPAddress.Parse(ip).AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tcpAsyCl.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
tcpAsyCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout);
tcpAsyCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout);
tcpAsyCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
// ----------------------------------------------------------------
// Connect synchronous client
tcpSynCl = new Socket(IPAddress.Parse(ip).AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tcpSynCl.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout);
tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout);
tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
_connected = true;
}
catch (System.IO.IOException error)
{
_connected = false;
throw (error);
}
}
</code></pre>
 
S
Received an email about changing my registery number.


so, I did and the values I am receive are not right. What would be the correct math

Here is code using to get return values.
Current values are all 1####, and the current Watt Hours is 30354###.
Several of the returned numbers are 12336.

Final result working on getting is total Watt Hours, current consumed.<pre>
<code>
Int16[] Registers = new Int16[10];
WSMBT.Result Result;
Result = wsmbtControl1.ReadHoldingRegisters(1, 435, 10, Registers);
if (Result == WSMBT.Result.SUCCESS)
{
string DataString = "";
string str = "";
decimal d = 0;
for (int i = 0; i < 10; i++)
{

str = string.Format("{0:D}", Registers);
DataString = DataString + str + "\r\n";

}

Data.Text = DataString.ToString();
}
else
{
MessageBox.Show(wsmbtControl1.GetLastErrorString());
}

</code></pre>
 
this solved my issue<pre>
WSMBT.Result Result;

Int16[] registers = new Int16[10];

WSMBT.WSMBTControl modbus = new WSMBT.WSMBTControl();

modbus.Mode = WSMBT.Mode.TCP_IP;
modbus.ConnectTimeout = 1000;
modbus.ResponseTimeout = 1000;
Result = modbus.Connect(IP, 502);

if (Result == WSMBT.Result.SUCCESS)
{
modbus.ReadHoldingRegisters(1, 434, 10, registers);


string DataString = "";
try
{
for (int i = 0; i < 10; i++)
{
byte[] b = null;

b = BitConverter.GetBytes(registers);
string[] c = BitConverter.ToString(b).Split('-');
string value =
DataString = DataString + c[1].Substring(1, 1) + c[0].Substring(1, 1);
}
string f = DataString.ToString().TrimStart('0');
f = String.Format("{0:#,##0}", int.Parse(f.Remove(f.Length - 7)));
Console.Write(f);

}
catch { }
}</pre>
 
Top