/// <summary> /// 修改设备编号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveNewDeviceNo_Click(object sender, EventArgs e) { if (labId.Text == "-1") { MessageBox.Show("请先选择病人"); return; } int currDeviceNo = Convert.ToInt32(labDeviceNo.Text); int deviceNo = Convert.ToInt32(txtNewDeviceNo.Text ?? "-1"); if (deviceNo <= 0 || deviceNo >= 65535) { MessageBox.Show("设备号在1到65534之间"); return; } if (MedicineLogic.IsDeviceExistbyDeviceNo(deviceNo)) { MessageBox.Show("该设备已存在"); return; } Device device = MedicineLogic.QueryDeviceByDeviceNo(currDeviceNo); MedicineLogic.UpdateDeviceNo(device.Id, deviceNo); byte[] zero = { 0x00 }; byte[] funcNo = { 0x01 }; byte[] oldDeviceNo = currDeviceNo > 255 ? Common.IntToHexByte(currDeviceNo) : zero.Concat(Common.IntToHexByte(currDeviceNo)).ToArray(); byte[] newDeviceNo = deviceNo > 255 ? Common.IntToHexByte(deviceNo) : zero.Concat(Common.IntToHexByte(deviceNo)).ToArray(); byte[] flg = Common.BuildFlg(); byte[] data = oldDeviceNo.Concat(funcNo).Concat(newDeviceNo).Concat(flg).ToArray(); AutoSendData(device.DeviceIP, data, flg); labDeviceNo.Text = deviceNo.ToString(); }
/// <summary> /// 处理接收的数据 /// </summary> /// <param name="obj"></param> private void HandleRecvDate(object obj) { IPEndPoint client = new IPEndPoint(IPAddress.Any, 0); EndPoint remote = (EndPoint)client; byte[] data = new byte[128]; while (true) { try { var recv = sokListen.ReceiveFrom(data, ref remote); LogService.WriteLog($"收到数据:{Common.ByteToHexStr(data, recv)}"); if (Common.IsDataComplete(data, recv)) { string strData = Common.GetData(Common.ByteToHexStr(data, recv)); RecvMsg recvMsg = new RecvMsg { DeviceNo = Convert.ToInt32(strData.Substring(0, 4), 16), FuncNo = strData.Substring(4, 2), Data = strData.Substring(6, strData.Length - 12), Flg = Common.HexStringToByte(strData.Substring(strData.Length - 6, 4)), }; switch (recvMsg.FuncNo) { case "04": //开机 SendTime(recvMsg.DeviceNo, remote); if (MedicineLogic.IsDeviceExistbyDeviceNo(recvMsg.DeviceNo)) { Device device = MedicineLogic.QueryDeviceByDeviceNo(recvMsg.DeviceNo); MedicineLogic.UpdateDeviceIp(device.Id, remote.ToString()); } else { MedicineLogic.AddDevice(new Device { DeviceNo = recvMsg.DeviceNo, DeviceIP = remote.ToString() }); } break; case "06": //服药情况 { UpdateTakeTimeStatus(recvMsg); int id = MedicineLogic.QueryPatientIdByDeviceNo(recvMsg.DeviceNo); UpdateUi(id); } break; case "07": //开药仓反馈 { int id = MedicineLogic.QueryPatientIdByDeviceNo(recvMsg.DeviceNo); int dispenId = Convert.ToInt32(Common.ByteToHexStr(recvMsg.Flg, 2), 16); MedicineLogic.UpdateIsTake(dispenId); //SetIsTakeOnUi(dispenId); UpdateUi(id); SendDispensing(id); } break; default: //命令响应 lock (msgList) { int index = msgList.FindIndex(n => ByteEquals(n.Flg, recvMsg.Flg)); if (index >= 0) { msgList.RemoveAt(index); } } break; } } } catch (Exception ex) { LogService.WriteLog(ex, "数据处理异常"); } } }