Compare commits
2 Commits
da5e399163
...
a0aefe0337
| Author | SHA1 | Date |
|---|---|---|
|
|
a0aefe0337 | |
|
|
5619199284 |
|
|
@ -89,8 +89,8 @@ Material:
|
|||
- _TexWidth: 16
|
||||
- _UVSec: 0
|
||||
- _WiperAmount: 0
|
||||
- _WiperAngle: 0
|
||||
- _WiperAxis: 1
|
||||
- _WiperAngle: 76.7
|
||||
- _WiperAxis: -5.65
|
||||
- _WiperDirection: 0
|
||||
- _WiperInterval: 0.5
|
||||
- _WiperRadius: 0.8
|
||||
|
|
|
|||
|
|
@ -77,6 +77,9 @@ namespace Script
|
|||
{
|
||||
client = new ModbusTcpClient();
|
||||
await client.ConnectToServer();
|
||||
|
||||
// client.StartModbus();
|
||||
|
||||
while (true)
|
||||
{
|
||||
await client.SendModbusRequest();
|
||||
|
|
@ -435,6 +438,13 @@ namespace Script
|
|||
// vehicleStandardInput.SetThrottleValue(xx);
|
||||
|
||||
// SetSpeedBasedOnValue(1);
|
||||
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.F12))
|
||||
{
|
||||
client.SendModbusRequest();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void CheckAndUpdateGear()
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
|
|
@ -34,6 +35,10 @@ namespace ModbusManager
|
|||
public Queue<CarStatusData> modbusQueue;
|
||||
public static ModbusTcpClient modbusTcpClient;
|
||||
|
||||
|
||||
private CancellationTokenSource cancellationTokenSource;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
modbusTcpClient = this;
|
||||
|
|
@ -44,20 +49,24 @@ namespace ModbusManager
|
|||
|
||||
private void Start()
|
||||
{
|
||||
// StartModbus();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
async Task StartModbus()
|
||||
public void StartModbus()
|
||||
{
|
||||
await ConnectToServer();
|
||||
while (true)
|
||||
{
|
||||
await SendModbusRequest();
|
||||
// await SendModbusRequest12();
|
||||
await Task.Delay(TimeSpan.FromSeconds(.1));
|
||||
}
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
// MonitorConnection(cancellationTokenSource.Token);
|
||||
MonitorQueue(cancellationTokenSource.Token);
|
||||
|
||||
// await ConnectToServer();
|
||||
// while (true)
|
||||
// {
|
||||
// await SendModbusRequest();
|
||||
// // await SendModbusRequest12();
|
||||
// await Task.Delay(TimeSpan.FromSeconds(.1));
|
||||
// }
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
|
|
@ -124,6 +133,47 @@ namespace ModbusManager
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task MonitorConnection(CancellationToken token)
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
if (!tcpClient.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ConnectToServer();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.Log("连接失败: " + ex.Message);
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), token); // 重试前等待
|
||||
}
|
||||
}
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), token); // 检查间隔
|
||||
}
|
||||
}
|
||||
|
||||
private async Task MonitorQueue(CancellationToken token)
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
if (modbusQueue.Count == 0)
|
||||
{
|
||||
await SendModbusRequest();
|
||||
await Task.Delay(TimeSpan.FromSeconds(3), token); // 队列检查频率
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), token); // 等待一段时间后再次检查
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设备回复: 00 00 00 00 00 24 01 03 20 00 01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00 09 00 0A 00 0B 00 0C 00 0D 00 0E 00 0F 00 10
|
||||
/// "00 00 00 00 00 24" 为TCP报文头,24 表示后面有三十六个字节。
|
||||
|
|
@ -241,6 +291,7 @@ namespace ModbusManager
|
|||
{
|
||||
tcpClient.GetStream().Close();
|
||||
tcpClient.Close();
|
||||
cancellationTokenSource.Cancel();
|
||||
Debug.Log("已关闭与Modbus服务器的连接。");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,9 @@ public class WeatherManager : MonoBehaviour
|
|||
case "雪天":
|
||||
weather_snow.SetActive(true);
|
||||
xuetianglass.SetActive(true);
|
||||
|
||||
weather_snow.transform.Find("下雪01").GetComponent<ParticleSystem>().Play();
|
||||
|
||||
// 地面.transform.GetComponent<MeshRenderer>().material = snowMaterial;
|
||||
break;
|
||||
case "雾天":
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -18,10 +18,10 @@ EditorUserSettings:
|
|||
value: 22424703114646680e0b0227036c52151802563f22213229
|
||||
flags: 0
|
||||
RecentlyUsedScenePath-4:
|
||||
value: 22424703114646680e0b0227036c52111f19276439262f2434
|
||||
value: 22424703114646680e0b0227036c4c0417050c6439262f2434
|
||||
flags: 0
|
||||
RecentlyUsedScenePath-5:
|
||||
value: 22424703114646680e0b0227036c4c0417050c6439262f2434
|
||||
value: 22424703114646680e0b0227036c52111f19276439262f2434
|
||||
flags: 0
|
||||
vcSharedLogLevel:
|
||||
value: 0d5e400f0650
|
||||
|
|
|
|||
Loading…
Reference in New Issue