Hi,
I am working on .net application that listens on Ethernet from three PLCs witch are connected via switch to PC. I am using .net driver from the Unitronics web site (I used Listener Example solution).
Application should write incoming data in SQL database. I listen on port = 20256, I am getting data from one PLC successfully when one PLC is connected on the switch, but when I connect another one then I can't read from the other and also OnDisconnect event fires.
So:
- How to solve problem when more PLCs try to connect my application, and how to read from all of them at the same time?
- How to solve concurrent access in Database ?
In attachment are two .vlp files from the two PLCs.
Here is a part of the code:
private void ButtonListenOnPort_Click(object sender, EventArgs e)
{
EthernetListener listener = PLCFactory.GetChannel(LocalPort) ?? new EthernetListener(LocalPort, 3, 3000);
listener.OnListenerConnectionAccepted += OnConnect;
listener.OnListenerConnectionClosed += OnDisconnect;
PLCFactory.GetPLC(listener);
}
private void OnConnect(PLC plcfromlistener)
{
_plc = plcfromlistener;
var rw = new ReadWriteRequest[1];
rw[0] = new ReadOperands
{
OperandType = OperandTypes.MI,
NumberOfOperands = 9,
StartAddress = 0
};
try
{
_plc.ReadWrite(ref rw, AsyncReply);
}
catch
{
MessageBox.Show("bla bla");
}
}
private void AsyncReply(IAsyncResult ar)
{
var async = (AsyncResult)ar;
var del = (ReadWriteOperandsDelegate)async.AsyncDelegate;
var rw = new ReadWriteRequest[0];
try
{
del.EndInvoke(ref rw, ar);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
var values = (object[])(rw[0].ResponseValues);
MethodInvoker mi = delegate
{
var success= new ClassSuccess();
success.Successful = 1;
StoreInClassSuccess(success);
};
UpdateUI(mi);
}
// store in database
public bool StoreInClassSuccess(ClassSuccess entry)
{
try
{
using (var context = new DBEntities())
{
context.ClassSuccess .Add(entry);
context.SaveChanges();
}
}
catch (Exception ex)
{
throw ex;
}
return true;
}
netv570.vlp
netv570n.vlp