|
|
@@ -45,17 +45,13 @@ public class SerialPortExample : MonoBehaviour
|
|
|
private void Init()
|
|
|
{
|
|
|
_isInit[gameObject.name] = GetInstanceID();
|
|
|
- serialPortUtility = gameObject.GetComponent<SerialPortUtilityPro>();
|
|
|
- serialPortUtility.IsAutoOpen = false;
|
|
|
#if UNITY_ANDROID && !UNITY_EDITOR
|
|
|
+ serialPortUtility = gameObject.GetComponent<SerialPortUtilityPro>();
|
|
|
serialPortUtility.OpenMethod = openMode;
|
|
|
serialPortUtility.DeviceName = PortName;
|
|
|
serialPortUtility.StopBit = SerialPortUtilityPro.StopBitEnum.OneBit;
|
|
|
serialPortUtility.DataBit = SerialPortUtilityPro.DataBitEnum.EightBit;
|
|
|
serialPortUtility.Open();
|
|
|
- //serialPortUtility.ReadCompleteEventObject.AddListener(ReadStreamingBinary);
|
|
|
- //byte[] datas = new byte[8] { 0xAA, 0x08, 0x04, 0x01, 0x00, 0x00, 0x3C, 0xBB };
|
|
|
- //serialPortUtility.Write(datas);
|
|
|
|
|
|
LOG($"{PortName} 串口打开");
|
|
|
if (PortName.Contains("/dev/ttyS0"))
|
|
|
@@ -65,6 +61,9 @@ public class SerialPortExample : MonoBehaviour
|
|
|
//拉取后台数据
|
|
|
ReqSettingData();
|
|
|
}
|
|
|
+#endif
|
|
|
+#if UNITY_EDITOR
|
|
|
+ TestBackUrl();
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
@@ -157,7 +156,7 @@ public class SerialPortExample : MonoBehaviour
|
|
|
}
|
|
|
response.Add(temp);//校验位
|
|
|
Debug.LogError($"responseCheck={responseCheck} tempResponseCheck={tempResponseCheck} data ={data} dataIndex={dataIndex} temp={temp}");
|
|
|
- serialPortUtility.Write(response.ToArray());
|
|
|
+ serialPortUtility?.Write(response.ToArray());
|
|
|
|
|
|
if (data == 0X01)
|
|
|
StandaloneAPI.InsertCoint(bytes[6]);
|
|
|
@@ -181,7 +180,7 @@ public class SerialPortExample : MonoBehaviour
|
|
|
byte temp = (byte)(request[1] ^ request[2]);
|
|
|
request.Add(temp);//校验 (校验 = 命令 ^ 数据长度)
|
|
|
request.Add(0x55);//结束
|
|
|
- serialPortUtility.Write(request.ToArray());
|
|
|
+ serialPortUtility?.Write(request.ToArray());
|
|
|
LOG($"请求后台数据!");
|
|
|
}
|
|
|
|
|
|
@@ -281,7 +280,7 @@ public class SerialPortExample : MonoBehaviour
|
|
|
request.Add(temp);
|
|
|
//数据包结束------------------------->(1个字节)->55
|
|
|
request.Add(0x55);//结束
|
|
|
- serialPortUtility.Write(request.ToArray());
|
|
|
+ serialPortUtility?.Write(request.ToArray());
|
|
|
LOG($"请求保存后台数据!");
|
|
|
}
|
|
|
|
|
|
@@ -392,12 +391,8 @@ public class SerialPortExample : MonoBehaviour
|
|
|
LOG($"请求后台支付URL");
|
|
|
byte[] datas = new byte[7] { 0xAA, 0x55, 0x04, dataIndex, 0xAA, 0x01, 0xAE };
|
|
|
datas[6] = GetXor(datas);
|
|
|
- serialPortUtility.Write(datas);
|
|
|
+ serialPortUtility?.Write(datas);
|
|
|
Invoke("CheckURLBack", 5f);
|
|
|
-
|
|
|
-#if UNITY_EDITOR
|
|
|
- TestBackUrl();
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
private void CheckURLBack()
|
|
|
@@ -459,7 +454,7 @@ public class SerialPortExample : MonoBehaviour
|
|
|
List<byte> s = new List<byte>(bytes);
|
|
|
s.RemoveAt(index);
|
|
|
bytes = s.ToArray();
|
|
|
- serialPortUtility.Write(bytes);
|
|
|
+ serialPortUtility?.Write(bytes);
|
|
|
}
|
|
|
|
|
|
byte GetXor(byte[] btyes)
|
|
|
@@ -476,7 +471,7 @@ public class SerialPortExample : MonoBehaviour
|
|
|
if (_isInit.TryGetValue(gameObject.name, out var instanceID) && instanceID == GetInstanceID())
|
|
|
{
|
|
|
LOG($"{PortName} 串口程序被销毁");
|
|
|
- serialPortUtility.Close();
|
|
|
+ serialPortUtility?.Close();
|
|
|
_isInit.Remove(gameObject.name);
|
|
|
}
|
|
|
}
|