1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
using System.Linq; using UnityEngine; using System.Collections; using System.Collections.Generic; using SimpleJSON; using System; using System.IO.Ports; namespace SerialTrial { public class SerialTrial : MVRScript { // Name of plugin public static string pluginName = "SerialTrial"; // Variables protected JSONStorableFloat Pulseinterval; private float pulsetime = 1; private string pulsestring = ""; protected UIDynamicButton startbutton; protected UIDynamicButton stopbutton; protected JSONStorableString jstring; protected UIDynamicTextField dtext; protected JSONStorableFloat ThrustX; // Serial Stuff SerialPort serial; // Function to initiate plugin public override void Init() { try { pluginLabelJSON.val = "Serial Trial"; // Serial pulse rate control Pulseinterval = new JSONStorableFloat("Serial Pulse Interval", 1f, 0.01f, 2f, true, true); RegisterFloat(Pulseinterval); CreateSlider(Pulseinterval, false); // Serial buttons startbutton = CreateButton("Start Serial"); if (startbutton != null) { startbutton.button.onClick.AddListener(StartButtonCallback); } stopbutton = CreateButton("Stop Serial"); if (stopbutton != null) { stopbutton.button.onClick.AddListener(StopButtonCallback); } jstring = new JSONStorableString("FooString", "Text"); dtext = CreateTextField(jstring); // Thrust position ThrustX = new JSONStorableFloat("X Position", 50f, 0f, 100f, true, true); RegisterFloat(ThrustX); CreateSlider(ThrustX, false); } catch (Exception e) { SuperController.LogError("Exception caught: " + e); } } // Update is called with each rendered frame by Unity void Update() { try { // Watch for next serial pulse pulsetime -= Time.deltaTime; //jstring.val = "time" + string.Format("{0:N2}",pulsetime); jstring.val = pulsestring; if (pulsetime <= 0f) { pulsetime = Pulseinterval.val; if(serial != null && serial.IsOpen) { SendPulse(); } } } catch (Exception e) { SuperController.LogError("Exception caught: " + e); } } // Serial button functions void StartButtonCallback() { StartSerial(); SuperController.LogMessage("Serial connection started"); } void StopButtonCallback() { StopSerial(); SuperController.LogMessage("Serial connection stopped"); } // Function to start serial void StartSerial() { serial = new SerialPort("COM8", 9600); serial.Open(); serial.ReadTimeout = 10; } // Function to stop serial void StopSerial() { if(serial != null && serial.IsOpen) serial.Close(); } // Function to send serial void SendPulse() { if (ThrustX.val >= 99f) { pulsestring = "L99"; } else if (ThrustX.val >= 10f) { pulsestring = "L" + string.Format("{0:0}",ThrustX.val); } else if (ThrustX.val >= 1f) { pulsestring = "L0" + string.Format("{0:0}",ThrustX.val); } else { pulsestring = "L00"; } serial.WriteLine(pulsestring); } // Function to close plugin void OnDestroy() { try { StopSerial(); } catch (Exception e) { SuperController.LogError("Exception caught: " + e); } } } } |
この記事のコピーは以下のサイトで販売している場合があります。
また、未販売の記事のリクエストをお受けしています。
https://neo-sahara.booth.pm/
販売開始リクエスト受付