//绑定重量 private void getWeightThread() { while (true) { Thread.Sleep(1500); try { if (isOkCheck == false && isOkBind == true) { if (txtWeight.Text.Trim() == "") { getMessage("获取不到重量请查看电子秤!\r\n"); } else if (txtWeight.Text.Trim() == "0.0g") { getMessage("警告重量为0.0g,请放置重物!\r\n"); } else { string st = txtWeight.Text.Replace("g", ""); float f1 = Convert.ToSingle(st); string sl = lowWeight.Text.Trim(); float low = Convert.ToSingle(st); string sh = highWeight.Text.Trim(); float high = Convert.ToSingle(sh); if (f1 < low) { getMessage("重量不能低于" + low + "g \r\n"); } else if (f1 > high) { getMessage("重量不能高于" + high + "g \r\n"); } else { string result = upsWebAPIs.Get(bindUrl + "/" + sn_code + "/" + txtWeight.Text); ResponseMotorMes bindMes = new ResponseMotorMes(); ResponseMotor2 data = new ResponseMotor2(); Object obj = upsWebAPIs.getObjectByJson(result, bindMes); bindMes = (ResponseMotorMes)obj; if (bindMes != null) { if (bindMes.success == "true") { data = (ResponseMotor2)bindMes.data; getMessage("SN:" + sn_code + " 绑定重量成功 重量为:" + txtWeight.Text); sn_code = null; bindMes = null; isOkCheck = true; isOkBind = false; } } } } } } catch (Exception e) { getMessage(e.Message + "绑定重量失败,请联系后台管理人员!\r\n"); } } }
/// <summary> /// 接收消息 /// </summary> /// <param name="clientSocket"></param> private void ReceiveMessage(object clientSocket) { Socket myClientSocket = (Socket)clientSocket; while (true) { try { //通过clientSocket接收数据 byte[] buffer = new byte[1024 * 1024 * 3]; int receiveNumber = myClientSocket.Receive(buffer); if (receiveNumber == 0) { break; } string strMessage = Encoding.ASCII.GetString(buffer, 0, receiveNumber); if (strMessage != null && !strMessage.Equals("")) { upsWebAPIs ups = new upsWebAPIs(); if (strMessage == "706") { if (motor.serialValue == null) { Object obj = ups.getMotorMes("0201"); if (obj != null) { motor = (ResponseMotor2)obj; if (motor.serialValue != null) { myClientSocket.Send(Encoding.ASCII.GetBytes(motor.serialValue)); string mes = "舵机SN:" + motor.serialValue + "\r\n"; getMessage(mes); motor.serialValue = null; } else { getMessage("舵机计划已经为空或者获取舵机SN信息失败!\r\n"); } } else if (obj == null) { getMessage("舵机计划已经为空或者获取舵机SN信息失败!\r\n"); } } else { myClientSocket.Send(Encoding.ASCII.GetBytes(motor.serialValue)); string mes = "舵机SN:" + motor.serialValue + "\r\n"; getMessage(mes); motor.serialValue = null; } //string code = "222112321"; //code = code.Substring(2, 10) + "\r\n" + code.Substring(12, 10); // myClientSocket.Send(Encoding.ASCII.GetBytes(code)); } else if (strMessage == "707") { if (motor.motor == null) { Object obj = ups.getMotorMes("0201"); if (obj != null) { motor = (ResponseMotor2)obj; if (motor.motor != null) { myClientSocket.Send(Encoding.ASCII.GetBytes(motor.motor)); string mes = "舵机ID:" + motor.motor + "\r\n"; getMessage(mes); motor.motor = null; } else { getMessage("舵机计划已经为空或者获取舵机ID信息失败!\r\n"); } } else if (obj == null) { getMessage("舵机计划已经为空或者获取舵机ID信息失败!\r\n"); } } else { myClientSocket.Send(Encoding.ASCII.GetBytes(motor.motor)); string mes = "舵机ID:" + motor.motor + "\r\n"; getMessage(mes); motor.motor = null; } } else if (strMessage == "1111") { String currentTask = getTask(); myClientSocket.Send(Encoding.ASCII.GetBytes(currentTask)); } else { bool updateResult = updateTask(strMessage); if (updateResult) { } else //如果更新失败则再重复请求3次 { for (int i = 0; i < 3; i++) { Thread.Sleep(1000); if (updateTask(strMessage)) { break; } } } } } // message = "接收客户端{0}消息{1}" + myClientSocket.RemoteEndPoint.ToString() + ":" + strMessage; // textRecieve.AppendText(message+"\r\n"); } catch (Exception ex) { myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); getMessage(ex.Message); break; } } }