public uMCPInoPort(string portName, BaudRate baudrate, bool selectDefaultState, uint selectIntMs, uint timeoutIntMs, byte packetSize, byte maxMessages) { timer = new PrecisionTimer(); timer.Period = 100; timer.Mode = Mode.Periodic; timer.Tick += (o, e) => { node.OnTick(); }; node = new uMCPInoNode(selectDefaultState, timeoutIntMs, selectIntMs, packetSize, maxMessages); node.OnSTATEChangedEventHandler += (o, e) => { OnStateChangedEventHandler.Rise(this, e); }; node.OnSELECTChangedEventHandler += (o, e) => { OnSelectChangedEventHandler.Rise(this, e); }; node.OnDataBlockAcknowledgedEventHandler += (o, e) => { OnDataBlockAcknowledgedEventHandler.Rise(this, e); }; node.OnDataBlockReceivedEventHandler += (o, e) => { OnDataBlockReceivedEventHandler.Rise(this, e); }; node.OnActionInfoEventHandler += (o, e) => { OnActionInfoEventHandler.Rise(this, e); }; node.OnTransmitterEmptyEventHandler += (o, e) => { OnTransmitterEmptyEventHandler.Rise(this, e); }; node.OnOutcomingEventHandler += new EventHandler <uMCPInoDataEventArgs>(node_OnOutcomingEventHandler); port = new SerialPort(portName, (int)baudrate); port.DataReceived += (o, e) => { var data = new byte[port.BytesToRead]; port.Read(data, 0, data.Length); node.OnIncomingData(data); }; port.ErrorReceived += (o, e) => { PortErrorEventHandler.Rise(this, e); }; }
private void SendPacket(uMCPPacket packet, bool isStartTimer) { OnActionInfoEventHandler.Rise(this, new uMCPActionInfoEventArgs(string.Format("Sending: {0}", packet.ToString()))); var serializedPacket = packet.Serialize(); OnOutcomingEventHandler.Rise(this, new uMCPDataEventArgs(serializedPacket)); sentPacket = packet; isTimerStartPendingOnTxFinish = isStartTimer; txFinishedInterval = Convert.ToUInt32(1000 * (fixedTxDelayS + serializedPacket.Length * 8 / baudRateBps)); IsTxFinishedTimerRunning = true; SELECT = (packet.PTYPE == uMCPPacketType.DTA); // if (packet.PTYPE != uMCPPacketType.DTA) // sending SELECT flag // SELECT = false; if (packet.PTYPE == uMCPPacketType.ACK) { ACKSent++; } else if ((packet.PTYPE == uMCPPacketType.DTA) || (packet.PTYPE == uMCPPacketType.DTE)) { DTADTESent++; } else if (packet.PTYPE == uMCPPacketType.REP) { REPSent++; } }
public void OnIncomingData(byte[] data) { if (receiver.InputDataProcess(data) > 0) { var packets = receiver.ReadAllPackets(); foreach (var packet in packets) { if ((packet.TID == ID) && (onIncomingPacket.ContainsKey(packet.PTYPE))) { OnActionInfoEventHandler.Rise(this, new uMCPActionInfoEventArgs(string.Format("Received: {0}", packet.ToString()))); onIncomingPacket[packet.PTYPE](packet); } } } }
private void OnTimeout() { OnActionInfoEventHandler.Rise(this, new uMCPActionInfoEventArgs("TIMEOUT")); if (state == uMCPState.ISTART) { SendPacket(new uMCPSTPacket(uMCPPacketType.STR, ID, TID), true); } else if (state == uMCPState.ASTART) { SendPacket(new uMCPSTPacket(uMCPPacketType.STA, ID, TID), true); } else if (state == uMCPState.RUNNING) { SREP = true; PerformProtocol(); } }