private void DataRead(int controlerIndex) { for (ushort i = 0; i < Controler[controlerIndex].RoomCount; i++) { //读取栏位基本信息 ushort[] a = Master.ReadInputRegisters( BitConverter.GetBytes(Controler[controlerIndex].ModbusAddress)[1], 14, 4); Controler[controlerIndex].Room[i].RoomNum = a[0]; Controler[controlerIndex].Room[i].BatcherCalibration = a[1]; Controler[controlerIndex].Room[i].Threshold = a[2]; Controler[controlerIndex].Room[i].PigsCount = a[3]; //controlerClass. for (ushort d = 0; d < Controler[controlerIndex].Room[i].PigsCount; d++) { ushort[] temp = Master.ReadInputRegisters( BitConverter.GetBytes(Controler[controlerIndex].ModbusAddress)[1], (ushort)(18 + (10 * d)), 10); byte[] p = { 0, BitConverter.GetBytes(temp[0])[0], BitConverter.GetBytes(temp[1])[0], BitConverter.GetBytes(temp[2])[0], BitConverter.GetBytes(temp[3])[0], BitConverter.GetBytes(temp[4])[0], BitConverter.GetBytes(temp[5])[0], BitConverter.GetBytes(temp[6])[0] }; p.Reverse(); Controler[controlerIndex].Room[i].Pig[d].PigSerial = BitConverter.ToInt64(p, 0); Controler[controlerIndex].Room[i].Pig[d].BatcherSum = temp[7]; Controler[controlerIndex].Room[i].Pig[d].WaterSum = temp[8]; Controler[controlerIndex].Room[i].Pig[d].Weight = temp[9]; PigData pigData = new PigData { PigSerial = Controler[controlerIndex].Room[i].Pig[d].PigSerial, Date = DateTime.Now, BatcherSum = Controler[controlerIndex].Room[i].Pig[d].BatcherSum, WaterSum = Controler[controlerIndex].Room[i].Pig[d].WaterSum, Weight = Controler[controlerIndex].Room[i].Pig[d].Weight, DeviceAddress = Controler[controlerIndex].ModbusAddress, RoomNum = i, BatcherCalibration = Controler[controlerIndex].Room[i].BatcherCalibration, Threshold = Controler[controlerIndex].Room[i].Threshold, }; PigDataUpdateEvent(pigData); Console.Write(i.ToString() + "," + d.ToString() + "," + Controler[controlerIndex].Room[i].Pig[d].PigSerial.ToString() + "," + Controler[controlerIndex].Room[i].Pig[d].Weight.ToString() + "\r\n"); } } }
private void Modbus_PigDataUpdateEvent(PigData d) { //throw new NotImplementedException(); _ = Dispatcher.BeginInvoke((Action) delegate() { PigData pig = new PigData(); pig = d; int v = pigDatas.ToList().FindIndex(i => i.PigSerial == d.PigSerial); if (v >= 0) { pigDatas.RemoveAt(v); } pigDatas.Insert(0, pig); //DeveiceList.Items.Refresh(); }); }
// Update is called once per frame void FixedUpdate() { if(endTime > 0 && !_gameOver) { _timer += Time.deltaTime; if(_timer >= 1.0f) { _timeCounter++; _timer = 0.0f; } if(_timeCounter == endTime) { List<BlockData> blocksInScene = new List<BlockData>(); List<PigData> pigsInScene = new List<PigData>(); foreach (Transform child in blocks_parent.transform) { Block b = child.GetComponent<Block>(); BlockData blockData = new BlockData(); blockData.collisionAmount = b.collisionAmount; blockData.rotation = b.transform.rotation.z; blockData.averageVelocity = b.velocity / b.experimentsAmount; blocksInScene.Add(blockData); } foreach (Transform child in pigs_parent.transform) { Block b = child.GetComponent<Block>(); PigData pigData = new PigData(); pigData.collisionAmount = b.collisionAmount; pigData.averageVelocity = b.velocity / b.experimentsAmount; pigsInScene.Add(pigData); } GameData gd = new GameData(); gd.blocks = blocksInScene; gd.pigs = pigsInScene; _gameData.Add(gd); CleanCurrentLevel(); if(_levelCounter < levels.Count) BuildLevel(levels[_levelCounter]); _levelCounter++; _timeCounter = 0; _timer = 0.0f; } if(_levelCounter == levels.Count + 1) { // Save game gata in a xml WriteGameData(); Application.Quit(); _gameOver = true; } } }