/// <summary> /// Enqueues the ASDU to the transmission queue. /// </summary> /// If an active connection exists the ASDU will be sent to the active client immediately. Otherwhise /// the ASDU will be added to the transmission queue for later transmission. /// <param name="asdu">ASDU to be sent</param> public void EnqueueASDU(ASDU asdu) { if (serverMode == ServerMode.SINGLE_REDUNDANCY_GROUP) { asduQueue.EnqueueAsdu(asdu); foreach (ClientConnection connection in allOpenConnections) { if (connection.IsActive) { connection.ASDUReadyToSend(); } } } else { foreach (ClientConnection connection in allOpenConnections) { if (connection.IsActive) { connection.GetASDUQueue().EnqueueAsdu(asdu); connection.ASDUReadyToSend(); } } } }
public void SendACT_CON(ASDU asdu, bool negative) { asdu.Cot = CauseOfTransmission.ACTIVATION_CON; asdu.IsNegative = negative; SendASDU(asdu); }
/// <summary> /// Enqueues the ASDU to the redundancy group specific message queue. /// This function is called by <see cref="lib60870.CS104.Server.EnqueueASDU"/>. If the Server.EnqueuASDU method /// is used this method should not be called! /// </summary> /// <param name="asdu">The ASDU to enqueue.</param> public void EnqueueASDU(ASDU asdu) { if (asduQueue != null) { asduQueue.EnqueueAsdu(asdu); } }
public void TestFrame() { ConnectionParameters para = new ConnectionParameters(); para.LinkAddress = 1; para.SizeOfCA = 2; LinkControlUp lc = new LinkControlUp(); lc.ACD = true; lc.DFC = false; lc.FuncCode = LinkFunctionCodeUp.UserData; T102Frame frame = new T102Frame(lc, para); ASDU asdu = new ASDU(TypeID.M_SP_TA_2, CauseOfTransmission.SPONTANEOUS, false, false, 1, RecordAddress.Total, false); SinglePointInformation sp = new SinglePointInformation(0, true, 0, new CP56Time2b(new DateTime(2007, 8, 18, 6, 21, 1, 520))); asdu.AddInformationObject(sp); asdu.Encode(frame, para); frame.PrepareToSend(); byte[] aa = frame.GetBuffer(); }
public void EnqueueAsdu(ASDU asdu) { lock (enqueuedASDUs) { if (oldestQueueEntry == -1) { oldestQueueEntry = 0; latestQueueEntry = 0; numberOfAsduInQueue = 1; enqueuedASDUs[0].asdu.ResetFrame(); asdu.Encode(enqueuedASDUs[0].asdu, parameters); enqueuedASDUs[0].entryTimestamp = SystemUtils.currentTimeMillis(); enqueuedASDUs[0].state = QueueEntryState.WAITING_FOR_TRANSMISSION; } else { bool enqueue = true; if (numberOfAsduInQueue == maxQueueSize) { if (enqueueMode == EnqueueMode.REMOVE_OLDEST) { } else if (enqueueMode == EnqueueMode.IGNORE) { DebugLog("Queue is full. Ignore new ASDU."); enqueue = false; } else if (enqueueMode == EnqueueMode.THROW_EXCEPTION) { throw new ASDUQueueException("Event queue is full."); } } if (enqueue) { latestQueueEntry = (latestQueueEntry + 1) % maxQueueSize; if (latestQueueEntry == oldestQueueEntry) { oldestQueueEntry = (oldestQueueEntry + 1) % maxQueueSize; } else { numberOfAsduInQueue++; } enqueuedASDUs[latestQueueEntry].asdu.ResetFrame(); asdu.Encode(enqueuedASDUs[latestQueueEntry].asdu, parameters); enqueuedASDUs[latestQueueEntry].entryTimestamp = SystemUtils.currentTimeMillis(); enqueuedASDUs[latestQueueEntry].state = QueueEntryState.WAITING_FOR_TRANSMISSION; } } } DebugLog("Queue contains " + numberOfAsduInQueue + " messages (oldest: " + oldestQueueEntry + " latest: " + latestQueueEntry + ")"); }
private static void TestCurrentTime() { ConnectionParameters para = new ConnectionParameters(); para.LinkAddress = 1; para.SizeOfCA = 2; LinkControlUp lc = new LinkControlUp(); lc.ACD = false; lc.DFC = false; lc.FuncCode = LinkFunctionCodeUp.UserData; T102Frame frame = new T102Frame(lc, para); ASDU asdu = new ASDU(CauseOfTransmission.REQUEST, false, false, 1, RecordAddress.Default, false); CurrentTime ct = new CurrentTime(new CP56Time2b(new DateTime(2007, 8, 18, 6, 21, 1, 520))); asdu.AddInformationObject(ct); asdu.Encode(frame, para); frame.PrepareToSend(); byte[] aa = frame.GetBuffer(); int length = aa[1]; byte linkControl = aa[4]; int linkAddr = aa[5] + aa[6] * 0x100; ASDU na = new ASDU(para, aa, length + 4 + 2); InformationObject io = na.GetElement(0); }
private static void TestReadSyncTime() { ConnectionParameters para = new ConnectionParameters(); para.LinkAddress = 1; para.SizeOfCA = 2; LinkControlDown lc = new LinkControlDown(); lc.FCB = false; lc.FCV = true; lc.FuncCode = LinkFunctionCodeDown.UserData; T102Frame frame = new T102Frame(lc, para); ASDU asdu = new ASDU(CauseOfTransmission.SYNC_TIME, false, false, 1, RecordAddress.Default, false); SyncTime st = new SyncTime(new CP56Time2b(new DateTime(2007, 8, 18, 6, 21, 1, 520))); asdu.AddInformationObject(st); asdu.Encode(frame, para); frame.PrepareToSend(); byte[] aa = frame.GetBuffer(); int length = aa[1]; byte linkControl = aa[4]; int linkAddr = aa[5] + aa[6] * 0x100; //解析 ASDU na = new ASDU(para, aa, length + 4 + 2); InformationObject io = na.GetElement(0); }
public void TestASDUAddInformationObjectsInWrongOrderToSequence() { ConnectionParameters cp = new ConnectionParameters(); ASDU asdu = new ASDU(cp, CauseOfTransmission.PERIODIC, false, false, 0, 1, true); bool encoded = asdu.AddInformationObject(new SinglePointInformation(100, false, new QualityDescriptor())); Assert.IsTrue(encoded); encoded = asdu.AddInformationObject(new SinglePointInformation(101, false, new QualityDescriptor())); Assert.IsTrue(encoded); encoded = asdu.AddInformationObject(new SinglePointInformation(102, false, new QualityDescriptor())); Assert.IsTrue(encoded); encoded = asdu.AddInformationObject(new SinglePointInformation(104, false, new QualityDescriptor())); Assert.IsFalse(encoded); encoded = asdu.AddInformationObject(new SinglePointInformation(102, false, new QualityDescriptor())); Assert.IsFalse(encoded); Assert.AreEqual(3, asdu.NumberOfElements); }
private static void TestManufacture() { ConnectionParameters para = new ConnectionParameters(); para.LinkAddress = 1; para.SizeOfCA = 2; LinkControlUp lc = new LinkControlUp(); lc.ACD = false; lc.DFC = false; lc.FuncCode = LinkFunctionCodeUp.UserData; T102Frame frame = new T102Frame(lc, para); ASDU asdu = new ASDU(CauseOfTransmission.INITIALIZED, false, false, 1, RecordAddress.Default, false); ManufacturerSpec ms = new ManufacturerSpec(0, 1, 0, 0x05040302); asdu.AddInformationObject(ms); asdu.Encode(frame, para); frame.PrepareToSend(); byte[] aa = frame.GetBuffer(); int length = aa[1]; byte linkControl = aa[4]; int linkAddr = aa[5] + aa[6] * 0x100; ASDU na = new ASDU(para, aa, length + 4 + 2); InformationObject io = na.GetElement(0); }
public static void Main(string[] args) { bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Server server = new Server(); server.DebugOutput = true; server.MaxQueueSize = 10; //server.SetLocalPort(2405); server.SetInterrogationHandler(interrogationHandler, null); server.SetASDUHandler(asduHandler, null); if (1 == rfiletable()) { return; } server.Start(); ASDU newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.INITIALIZED, false, false, 0, 1, false); EndOfInitialization eoi = new EndOfInitialization(0); newAsdu.AddInformationObject(eoi); server.EnqueueASDU(newAsdu); int waitTime = 1000; while (running) { Thread.Sleep(1000); if (waitTime > 0) { waitTime -= 100; } else { newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.PERIODIC, false, false, 1, 1, true); ToServer(ref newAsdu); rfile( ); server.EnqueueASDU(newAsdu); waitTime = 1000; } } Console.WriteLine("Stop server"); server.Stop(); }
public void SendACT_TERM(ASDU asdu) { asdu.Cot = CauseOfTransmission.ACTIVATION_TERMINATION; asdu.IsNegative = false; SendASDU(asdu); }
public void TestSendTestFRTimeoutMaster() { ConnectionParameters clientParameters = new ConnectionParameters(); ConnectionParameters serverParameters = new ConnectionParameters(); clientParameters.T3 = 1; Server server = new Server(serverParameters); server.SetLocalPort(20213); server.Start(); Connection connection = new Connection("127.0.0.1", 20213, clientParameters); connection.Connect(); connection.SetRawMessageHandler(testSendTestFRTimeoutMasterRawMessageHandler, null); ASDU asdu = new ASDU(clientParameters, CauseOfTransmission.SPONTANEOUS, false, false, 0, 1, false); asdu.AddInformationObject(new SinglePointInformation(100, false, new QualityDescriptor())); connection.SendASDU(asdu); Assert.AreEqual(2, connection.GetStatistics().SentMsgCounter); /* STARTDT + ASDU */ while (connection.GetStatistics().RcvdMsgCounter < 2) { Thread.Sleep(1); } Assert.AreEqual(2, connection.GetStatistics().RcvdMsgCounter); /* STARTDT_CON + ASDU */ Thread.Sleep(6000); // Expect connection to be closed due to three missing TESTFR_CON responses Assert.IsFalse(connection.IsRunning); ConnectionException ce = null; // Connection is closed. SendASDU should fail try { connection.SendASDU(asdu); } catch (ConnectionException e) { ce = e; } Assert.IsNotNull(ce); Assert.AreEqual("not connected", ce.Message); connection.Close(); server.Stop(); Assert.AreEqual(5, connection.GetStatistics().RcvdMsgCounter); /* STARTDT_CON + ASDU + TESTFR_CON */ Assert.AreEqual(0, connection.GetStatistics().RcvdTestFrConCounter); }
private void SendIECHumi(byte humi_stat) { myUI("SEND: SPONTANEOUS : MeasuredValueNormalized : IOA=302 : CA=1 VALUE=" + humi_stat.ToString(), iec104Box); newAsdu = new ASDU(server.GetConnectionParameters(), CauseOfTransmission.SPONTANEOUS, false, false, 2, 1, false); newAsdu.AddInformationObject(new MeasuredValueNormalized(302, humi_stat, new QualityDescriptor())); server.EnqueueASDU(newAsdu); }
//IEC104 procedures private static bool interrogationHandler(object parameter, ServerConnection connection, ASDU asdu, byte qoi) { //BitArray myBA = new BitArray(BitConverter.GetBytes(parameter).ToArray()); //Console.WriteLine ("Interrogation for group " + qoi); if (qoi == 20) /* Station interrogation */ { connection.SendACT_CON(asdu, false); ConnectionParameters cp = connection.GetConnectionParameters(); // The CS101 allows only information object without timestamps in GI responses! // send information objects ASDU newAsdu = new ASDU(cp, CauseOfTransmission.INTERROGATED_BY_STATION, false, false, 2, 1, false); newAsdu = new ASDU(cp, CauseOfTransmission.INTERROGATED_BY_STATION, false, false, 2, 1, true); /* * myUI("SEND: INTERROGATED_BY_STATION : SinglePointInformation : IOA=200 : CA=1 VALUE=false",iec104Box); * myUI("SEND: INTERROGATED_BY_STATION : SinglePointInformation : IOA=201 : CA=1 VALUE=false",iec104Box); * myUI("SEND: INTERROGATED_BY_STATION : SinglePointInformation : IOA=202 : CA=1 VALUE=false",iec104Box); * myUI("SEND: INTERROGATED_BY_STATION : SinglePointInformation : IOA=203 : CA=1 VALUE=false",iec104Box); * myUI("SEND: INTERROGATED_BY_STATION : SinglePointInformation : IOA=204 : CA=1 VALUE=false",iec104Box); * myUI("SEND: INTERROGATED_BY_STATION : SinglePointInformation : IOA=205 : CA=1 VALUE=false",iec104Box); */ newAsdu.AddInformationObject(new SinglePointInformation(200, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(201, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(202, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(203, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(204, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(205, false, new QualityDescriptor())); connection.SendASDU(newAsdu); newAsdu = new ASDU(cp, CauseOfTransmission.INTERROGATED_BY_STATION, false, false, 2, 1, true); /* myUI("SEND: INTERROGATED_BY_STATION : MeasuredValueNormalized : IOA=302 : CA=1 VALUE=0",iec104Box); * myUI("SEND: INTERROGATED_BY_STATION : MeasuredValueNormalized : IOA=302 : CA=1 VALUE=0",iec104Box); */ newAsdu.AddInformationObject(new MeasuredValueNormalized(301, 0, new QualityDescriptor())); newAsdu.AddInformationObject(new MeasuredValueNormalized(302, 0, new QualityDescriptor())); connection.SendASDU(newAsdu); connection.SendACT_TERM(asdu); } else { connection.SendACT_CON(asdu, true); } return(true); }
public void TestSendTestFRTimeoutSlave() { ConnectionParameters clientParameters = new ConnectionParameters(); ConnectionParameters serverParameters = new ConnectionParameters(); serverParameters.T3 = 1; Server server = new Server(serverParameters); server.SetLocalPort(20213); server.Start(); Connection connection = new Connection("127.0.0.1", 20213, clientParameters); connection.DebugOutput = true; connection.SetRawMessageHandler(testSendTestFRTimeoutSlaveRawMessageHandler, null); connection.Connect(); Assert.AreEqual(1, connection.GetStatistics().SentMsgCounter); /* STARTDT */ while (connection.GetStatistics().RcvdMsgCounter < 1) { Thread.Sleep(1); } Assert.AreEqual(1, connection.GetStatistics().RcvdMsgCounter); /* STARTDT_CON */ Thread.Sleep(6000); // Connection is closed. SendASDU should fail try { ASDU asdu = new ASDU(clientParameters, CauseOfTransmission.SPONTANEOUS, false, false, 0, 1, false); asdu.AddInformationObject(new SinglePointInformation(100, false, new QualityDescriptor())); connection.SendASDU(asdu); } catch (ConnectionException) { } while (connection.IsRunning == true) { Thread.Sleep(10); } connection.Close(); server.Stop(); // Assert.AreEqual (5, connection.GetStatistics ().RcvdMsgCounter); /* STARTDT_CON + ASDU + TESTFR_CON */ // Assert.AreEqual (0, connection.GetStatistics ().RcvdTestFrConCounter); }
/// <summary> /// Send a response ASDU over this connection /// </summary> /// <exception cref="ConnectionException">Throws an exception if the connection is no longer active (e.g. because it has been closed by the other side).</exception> /// <param name="asdu">The ASDU to send</param> public void SendASDU(ASDU asdu) { if (isActive) { SendASDUInternal(asdu); } else { throw new ConnectionException("Connection not active"); } }
public static void Main(string[] args) { bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Server server = new Server(); server.DebugOutput = true; server.MaxQueueSize = 10; server.SetInterrogationHandler(interrogationHandler, null); server.SetASDUHandler(asduHandler, null); server.Start(); SimpleFile file = new SimpleFile(1, 30000, NameOfFile.TRANSPARENT_FILE); byte[] fileData = new byte[1025]; for (int i = 0; i < 1025; i++) { fileData [i] = (byte)(i + 1); } file.AddSection(fileData); SimpleFile file2 = new SimpleFile(1, 30001, NameOfFile.TRANSPARENT_FILE); file2.AddSection(fileData); server.GetAvailableFiles().AddFile(file); server.GetAvailableFiles().AddFile(file2); ASDU newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.INITIALIZED, false, false, 0, 1, false); EndOfInitialization eoi = new EndOfInitialization(0); newAsdu.AddInformationObject(eoi); server.EnqueueASDU(newAsdu); while (running) { Thread.Sleep(100); } Console.WriteLine("Stop server"); server.Stop(); }
private static void TestReadAccountingITWithTimeRangeAndAddressRange() { ConnectionParameters para = new ConnectionParameters(); para.LinkAddress = 1; para.SizeOfCA = 2; LinkControlDown lc = new LinkControlDown(); lc.FCB = false; lc.FCV = true; lc.FuncCode = LinkFunctionCodeDown.UserData; T102Frame frame = new T102Frame(lc, para); ASDU asdu = new ASDU(CauseOfTransmission.ACTIVATION, false, false, 1, RecordAddress.Default, false); ReadAccountingITWithTimeRangeAndAddressRange at = new ReadAccountingITWithTimeRangeAndAddressRange( 3, 7, new CP40Time2b(new DateTime(2007, 8, 18, 0, 0, 0)), new CP40Time2b(new DateTime(2007, 8, 19, 0, 0, 0))); asdu.AddInformationObject(at); asdu.Encode(frame, para); frame.PrepareToSend(); //todo 书上实例报文,记录地址是03,这个有点扯。。。 byte[] aa = frame.GetBuffer(); int length = aa[1]; byte linkControl = aa[4]; int linkAddr = aa[5] + aa[6] * 0x100; //解析 ASDU na = new ASDU(para, aa, length + 4 + 2); InformationObject sp = na.GetElement(0); //无数据 na.Cot = CauseOfTransmission.NO_RECORD; LinkControlUp lc2 = new LinkControlUp(); lc2.ACD = false; lc2.DFC = false; lc2.FuncCode = LinkFunctionCodeUp.NoData; T102Frame frame2 = new T102Frame(lc2, para); na.Encode(frame2, para); frame2.PrepareToSend(); //镜像报文 byte[] bb = frame2.GetBuffer(); }
private static void TestReadSinglePointWithRange() { ConnectionParameters para = new ConnectionParameters(); para.LinkAddress = 1; para.SizeOfCA = 2; LinkControlDown lc = new LinkControlDown(); lc.FCB = false; lc.FCV = true; lc.FuncCode = LinkFunctionCodeDown.UserData; T102Frame frame = new T102Frame(lc, para); ASDU asdu = new ASDU(CauseOfTransmission.ACTIVATION, false, false, 1, RecordAddress.Period_Week_1, false); ReadSinglePointWithTimeRange sptr = new ReadSinglePointWithTimeRange( new CP40Time2b(new DateTime(2007, 9, 2, 1, 0, 0)), new CP40Time2b(new DateTime(2007, 9, 3, 1, 0, 0))); asdu.AddInformationObject(sptr); asdu.Encode(frame, para); frame.PrepareToSend(); //todo: 记录类型书上表的是0x31,这里测试结果是十进制31,根据国外标准,31应该是十进制。具体情况具体分析把。。 byte[] aa = frame.GetBuffer(); int length = aa[1]; byte linkControl = aa[4]; int linkAddr = aa[5] + aa[6] * 0x100; //解析 ASDU na = new ASDU(para, aa, length + 4 + 2); InformationObject sp = na.GetElement(0); //无数据 na.Cot = CauseOfTransmission.NO_RECORD; LinkControlUp lc2 = new LinkControlUp(); lc2.ACD = false; lc2.DFC = false; lc2.FuncCode = LinkFunctionCodeUp.NoData; T102Frame frame2 = new T102Frame(lc2, para); na.Encode(frame2, para); frame2.PrepareToSend(); //镜像报文 byte[] bb = frame2.GetBuffer(); }
public static void Main(string[] args) { bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Server server = new Server(); server.DebugOutput = true; server.MaxQueueSize = 10; server.EnqueueMode = EnqueueMode.REMOVE_OLDEST; server.SetInterrogationHandler(interrogationHandler, null); server.SetASDUHandler(asduHandler, null); server.Start(); ASDU newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.INITIALIZED, false, false, 0, 1, false); EndOfInitialization eoi = new EndOfInitialization(0); newAsdu.AddInformationObject(eoi); server.EnqueueASDU(newAsdu); int waitTime = 1000; while (running) { Thread.Sleep(100); if (waitTime > 0) { waitTime -= 100; } else { newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.PERIODIC, false, false, 0, 1, false); newAsdu.AddInformationObject(new MeasuredValueScaled(110, -1, new QualityDescriptor())); server.EnqueueASDU(newAsdu); waitTime = 1000; } } Console.WriteLine("Stop server"); server.Stop(); }
public static void Main(string[] args) { bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Server server = new Server(); //server.SetLocalAddress ("0.0.0.0"); server.ServerMode = ServerMode.CONNECTION_IS_REDUNDANCY_GROUP; server.MaxQueueSize = 10; server.MaxOpenConnections = 2; server.SetConnectionRequestHandler(connectionRequestHandler, null); server.SetConnectionEventHandler(connectionEventHandler, null); server.SetInterrogationHandler(interrogationHandler, null); server.SetASDUHandler(asduHandler, null); server.Start(); int waitTime = 1000; while (running) { Thread.Sleep(100); if (waitTime > 0) { waitTime -= 100; } else { ASDU newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.PERIODIC, false, false, 2, 1, false); newAsdu.AddInformationObject(new MeasuredValueScaled(110, -1, new QualityDescriptor())); server.EnqueueASDU(newAsdu); waitTime = 1000; } } Console.WriteLine("Stop server"); server.Stop(); }
public static void Main(string[] args) { Test102.TestFrame(); return; bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Server server = new Server(); if (BitConverter.IsLittleEndian) { Console.WriteLine("Platform is little endian"); } server.MaxQueueSize = 10; server.SetInterrogationHandler(interrogationHandler, null); server.SetASDUHandler(asduHandler, null); server.Start(); int waitTime = 1000; while (running) { Thread.Sleep(100); if (waitTime > 0) { waitTime -= 100; } else { ASDU newAsdu = new ASDU(CauseOfTransmission.PERIODIC, false, false, 2, 1, false); newAsdu.AddInformationObject(new MeasuredValueScaled(110, -1, new QualityDescriptor())); server.EnqueueASDU(newAsdu); waitTime = 1000; } } Console.WriteLine("Stop server"); server.Stop(); }
private void SendASDUInternal(ASDU asdu) { if (isActive) { lock (waitingASDUsHighPrio) { BufferFrame frame = new BufferFrame(new byte[256], 6); asdu.Encode(frame, alParameters); waitingASDUsHighPrio.Enqueue(frame); } SendWaitingASDUs(); } }
public static void Main(string[] args) { bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Server server = new Server(); Connection con = new Connection("127.0.0.1"); con.DebugOutput = true; server.DebugOutput = true; server.MaxQueueSize = 10; server.SetInterrogationHandler(interrogationHandler, null); server.Start(); Thread.Sleep(1000); ASDU asdu = new ASDU(con.Parameters, CauseOfTransmission.SPONTANEOUS, false, false, 0, 1, false); asdu.AddInformationObject(new ParameterNormalizedValue(102, 0.1f, 0)); con.Connect(); con.SendTestCommand(1); //con.SendInterrogationCommand (CauseOfTransmission.ACTIVATION, 1, 20); con.SendASDU(asdu); con.SendASDU(asdu); con.SendASDU(asdu); con.SendASDU(asdu); con.SendASDU(asdu); con.SendASDU(asdu); con.SendASDU(asdu); con.SendASDU(asdu); while (running) { con.SendASDU(asdu); //Thread.Sleep(100); } Console.WriteLine("Stop server"); con.Close(); server.Stop(); }
private static void TestReadCurrentTime() { ConnectionParameters para = new ConnectionParameters(); para.LinkAddress = 1; para.SizeOfCA = 2; LinkControlDown lc = new LinkControlDown(); lc.FCB = true; lc.FCV = true; lc.FuncCode = LinkFunctionCodeDown.UserData; T102Frame frame = new T102Frame(lc, para); ASDU asdu = new ASDU(TypeID.C_TI_NA_2, CauseOfTransmission.REQUEST, false, false, 1, RecordAddress.Default, false); asdu.Encode(frame, para); frame.PrepareToSend(); byte[] aa = frame.GetBuffer(); int length = aa[1]; byte linkControl = aa[4]; int linkAddr = aa[5] + aa[6] * 0x100; //解析 ASDU na = new ASDU(para, aa, length + 4 + 2); //无ASDU na.Cot = CauseOfTransmission.NO_ASDU_TYPE; LinkControlUp lc2 = new LinkControlUp(); lc2.ACD = false; lc2.DFC = false; lc2.FuncCode = LinkFunctionCodeUp.NoData; T102Frame frame2 = new T102Frame(lc2, para); na.Encode(frame2, para); frame2.PrepareToSend(); //镜像报文 byte[] bb = frame2.GetBuffer(); }
private bool ReceivedASDUsTryDequeue(out ASDU asdu) { lock (_lockReceivedASDUs) { if (receivedASDUs.Count > 0) { asdu = receivedASDUs.Dequeue(); return(true); } else { asdu = null; return(false); } } }
private static bool interrogationHandler(object parameter, ServerConnection connection, ASDU asdu, byte qoi) { Console.WriteLine("Interrogation for group " + qoi); connection.SendACT_CON(asdu, false); // send information objects ASDU newAsdu = new ASDU(CauseOfTransmission.INTERROGATED_BY_STATION, false, false, 2, 1, false); newAsdu.AddInformationObject(new MeasuredValueScaled(100, -1, new QualityDescriptor())); newAsdu.AddInformationObject(new MeasuredValueScaled(101, 23, new QualityDescriptor())); newAsdu.AddInformationObject(new MeasuredValueScaled(102, 2300, new QualityDescriptor())); connection.SendASDU(newAsdu); newAsdu = new ASDU(CauseOfTransmission.INTERROGATED_BY_STATION, false, false, 3, 1, false); newAsdu.AddInformationObject(new MeasuredValueScaledWithCP56Time2a(103, 3456, new QualityDescriptor(), new CP56Time2a(DateTime.Now))); connection.SendASDU(newAsdu); newAsdu = new ASDU(CauseOfTransmission.INTERROGATED_BY_STATION, false, false, 2, 1, false); newAsdu.AddInformationObject(new SinglePointWithCP56Time2a(104, true, new QualityDescriptor(), new CP56Time2a(DateTime.Now))); connection.SendASDU(newAsdu); // send sequence of information objects newAsdu = new ASDU(CauseOfTransmission.INTERROGATED_BY_STATION, false, false, 2, 1, true); newAsdu.AddInformationObject(new SinglePointInformation(200, true, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(201, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(202, true, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(203, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(204, true, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(205, false, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(206, true, new QualityDescriptor())); newAsdu.AddInformationObject(new SinglePointInformation(207, false, new QualityDescriptor())); connection.SendASDU(newAsdu); connection.SendACT_TERM(asdu); return(true); }
public static int ToServer(ref ASDU newAsdu) { string key; float vl; int adr; for (int i = 0; i < listOfAdress.Count; i++) { //key=listOfAdress[i].Name; adr = listOfAdress[i].Cvalif; vl = listOfAdress[i].Value; //Console.WriteLine("ToServer=" + key + "=" + vl.ToString()); //newAsdu.AddInformationObject(new MeasuredValueShortWithCP56Time2a(adr, (float)vl, new QualityDescriptor(), new CP56Time2a(DateTime.Now))); newAsdu.AddInformationObject(new MeasuredValueShort(adr, (float)vl, new QualityDescriptor())); } return(0); }
public void TestASDUAddInformationObjects() { ASDU asdu = new ASDU(CauseOfTransmission.PERIODIC, false, false, 0, 1, false); asdu.AddInformationObject(new SinglePointInformation(100, false, new QualityDescriptor())); asdu.AddInformationObject(new SinglePointInformation(101, false, new QualityDescriptor())); // wrong InformationObject type expect exception ArgumentException ae = null; try { asdu.AddInformationObject(new DoublePointInformation(102, DoublePointValue.ON, new QualityDescriptor())); } catch (ArgumentException e) { ae = e; } Assert.NotNull(ae); }
public void TestEncodeDecodeSetpointCommandNormalized() { Server server = new Server(); server.SetLocalPort(20213); float recvValue = 0f; float sendValue = 1.0f; bool hasReceived = false; server.SetASDUHandler(delegate(object parameter, ServerConnection con, ASDU asdu) { if (asdu.TypeId == TypeID.C_SE_NA_1) { SetpointCommandNormalized spn = (SetpointCommandNormalized)asdu.GetElement(0); recvValue = spn.NormalizedValue; hasReceived = true; } return(true); }, null); server.Start(); Connection connection = new Connection("127.0.0.1", 20213); connection.Connect(); ASDU newAsdu = new ASDU(CauseOfTransmission.ACTIVATION, false, false, 0, 1, false); newAsdu.AddInformationObject(new SetpointCommandNormalized(100, sendValue, new SetpointCommandQualifier(false, 0))); connection.SendASDU(newAsdu); while (hasReceived == false) { Thread.Sleep(50); } connection.Close(); server.Stop(); Assert.AreEqual(sendValue, recvValue, 0.001); }