ISAP/Assets/Scripts/Net/TcpNet.cs

134 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TcpNet : MonoBehaviour
{
TcpConnect tcpConnect;
// Start is called before the first frame update
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
void Start()
{
tcpConnect = TcpConnect.Instance;
//tcpConnect.InitConnect("192.168.1.2",8899);
//tcpConnect.InitConnect("124.248.69.29", 13344);
//tcpConnect.InitConnect("192.168.1.2", 8899);
//EventCenter.dispatcher.AddListener(MsgType.OnGetBodyNum, OnGetBodyNum);
}
public void OnGetBodyNum(Message evt)
{
if (DataManager.Instance.bodyNum.Count > 0)
{
//string head = Convert.ToString(255, 16) + Convert.ToString(0, 16) + Convert.ToString(255, 16)
// + Convert.ToString(0, 16) + Convert.ToString(255, 16);
//string end = Convert.ToString(252, 16) + Convert.ToString(252, 16) + Convert.ToString(252, 16);
string head = "ff 00 ff 00 ff ";//尾部+空格
string end = "fc fc fc";
string type = "5e ";
string length = "02 ";
string data = "ca 01 ";
string str = head + type + length + data + end;
var bodyNum = DataManager.Instance.bodyNum[0];
//bodyNum.text = "体温:" + data.bodyTemperature + "\n血压" + data.bloodPressure;
if (!string.IsNullOrEmpty(bodyNum.heartRate))//心率
{
int heart = int.Parse(bodyNum.heartRate);
type = "5a ";
length = "03 ";
data = bodyNum.heartRate + " ";
str = head + type + length + data + end;
StartCoroutine(SendBody(str));
}
if (!string.IsNullOrEmpty(bodyNum.bodyTemperature))//体温
{
string[] temp = bodyNum.bodyTemperature.Split('.');
type = "5a ";
length = "03 ";
string str1, str2;
if (temp.Length > 2)
{
str1 = temp[0];
str2 = temp[1];
}
else
{
str1 = temp[0];
str2 = "00";
}
data = "03 " + str1 + " " + str2 + " ";
str = head + type + length + data + end;
StartCoroutine(SendBody(str));
}
//if (!string.IsNullOrEmpty(bodyNum.breathingNum))//呼吸次数
//{
// type = "5a ";
// length = "02 ";
// data = "02 " + bodyNum.breathingNum + " ";
// str = head + type + length + data + end;
// StartCoroutine(SendBody(str));
//}
//if (!string.IsNullOrEmpty(bodyNum.bloodPressure))//血压 低 高
//{
// str = head + type + length + data + end;
// StartCoroutine(SendBody(str));
//}
}
}
// Update is called once per frame
void Update()
{
//if (Input.GetKeyDown(KeyCode.A))
//{
// SendConnectSuccess();
//}
}
void SendConnectSuccess()
{
string head = Convert.ToString(255, 16) + Convert.ToString(0, 16) + Convert.ToString(255, 16)
+ Convert.ToString(0, 16) + Convert.ToString(255, 16);
string end = Convert.ToString(252, 16) + Convert.ToString(252, 16) + Convert.ToString(252, 16);
head = "ff 00 ff 00 ff ";//尾部+空格
string type = "5e ";
string length = "02 ";
string data = "ca 01 ";
end = "fc fc fc";
string str = head + type + length + data + end;
//string str = head + end;
tcpConnect.SendMessage(textWork16(str));
}
int counts = 1;
IEnumerator SendBody(string url)
{
yield return new WaitForSeconds(counts++);
tcpConnect.SendMessage(textWork16(url));
}
private byte[] textWork16(string strText)
{
strText = strText.Replace(" ", "");
byte[] bText = new byte[strText.Length / 2];
for (int i = 0; i < strText.Length / 2; i++)
{
bText[i] = Convert.ToByte(Convert.ToInt32(strText.Substring(i * 2, 2), 16));
}
return bText;
}
}