Skip to content

Commit 70059ec

Browse files
committedFeb 9, 2014
Version 0.12 added config files
1 parent c8bc3bc commit 70059ec

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed
 

‎KSPSerialIO/KSPIO.cs

+61-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
using OpenNETCF.IO.Ports;
1010
using UnityEngine;
11+
using KSP.IO;
1112

1213
namespace KSPSerialIO
1314
{
@@ -44,6 +45,37 @@ public struct HandShakePacket
4445
public byte M3;
4546
}
4647

48+
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
49+
public class SettingsNStuff : MonoBehaviour
50+
{
51+
public static PluginConfiguration cfg = PluginConfiguration.CreateForType<SettingsNStuff>();
52+
public static string DefaultPort;
53+
public static double refreshrate;
54+
public static int HandshakeDelay;
55+
public static int BaudRate;
56+
57+
void Awake()
58+
{
59+
//cfg["refresh"] = 0.08;
60+
//cfg["DefaultPort"] = "COM1";
61+
//cfg["HandshakeDelay"] = 2500;
62+
print("KSPSerialIO: Loading settings...");
63+
64+
cfg.load();
65+
DefaultPort = cfg.GetValue<string>("DefaultPort");
66+
print("KSPSerialIO: Default Port = " + DefaultPort);
67+
68+
refreshrate = cfg.GetValue<double>("refresh");
69+
print("KSPSerialIO: Refreshrate = " + refreshrate.ToString());
70+
71+
BaudRate = cfg.GetValue<int>("BaudRate");
72+
print("KSPSerialIO: BaudRate = " + BaudRate.ToString());
73+
74+
HandshakeDelay = cfg.GetValue<int>("HandshakeDelay");
75+
print("KSPSerialIO: Handshake Delay = " + HandshakeDelay.ToString());
76+
}
77+
}
78+
4779
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
4880
public class KSPSerialPort : MonoBehaviour
4981
{
@@ -65,6 +97,8 @@ public static void sendPacket(object anything)
6597
byte[] Packet = new byte[size + 4];
6698

6799
//Packet = [header][size][payload][checksum];
100+
//Header = [Header1=0xBE][Header2=0xEF]
101+
//size = [payload.length (0-255)]
68102

69103
for (int i = 0; i < size; i++)
70104
{
@@ -106,6 +140,7 @@ void initializeDataPackets()
106140

107141
void Awake()
108142
{
143+
print("KSPSerialIO: Version 0.12");
109144
print("KSPSerialIO: Getting serial ports...");
110145
initializeDataPackets();
111146

@@ -120,17 +155,31 @@ void Awake()
120155
}
121156
else
122157
{
123-
String[] names = SerialCOMSKey.GetValueNames();
158+
String[] realports = SerialCOMSKey.GetValueNames(); // get list of all serial devices
159+
String[] names = new string[realports.Length + 1]; // make a new list with 1 extra, we put the default port first
160+
realports.CopyTo(names, 1);
124161

125162
print("KSPSerialIO: Found " + names.Length.ToString() + " serial ports");
126163

127164
//look through all found ports for our display
165+
int j = 0;
166+
128167
foreach (string PortName in names)
129168
{
130-
PortNumber = (string)SerialCOMSKey.GetValue(PortName);
131-
print("KSPSerialIO: trying port " + PortName + " - " + PortNumber);
169+
if (j == 0) // try default port first
170+
{
171+
PortNumber = SettingsNStuff.DefaultPort;
172+
print("KSPSerialIO: trying default port " + PortNumber);
173+
}
174+
else
175+
{
176+
PortNumber = (string)SerialCOMSKey.GetValue(PortName);
177+
print("KSPSerialIO: trying port " + PortName + " - " + PortNumber);
178+
}
132179

133-
Port = new SerialPort(PortNumber, 38400, Parity.None, 8, StopBits.One);
180+
j++;
181+
182+
Port = new SerialPort(PortNumber, SettingsNStuff.BaudRate, Parity.None, 8, StopBits.One);
134183
Port.ReceivedBytesThreshold = 3;
135184

136185
//print("KSPSerialIO: receive threshold " + Port.ReceivedBytesThreshold.ToString());
@@ -151,15 +200,15 @@ void Awake()
151200
//secret handshake
152201
if (Port.IsOpen)
153202
{
154-
Thread.Sleep(2500);
203+
Thread.Sleep(SettingsNStuff.HandshakeDelay);
155204
sendPacket(HPacket);
156205

157206
//wait for reply
158-
int j = 0;
159-
while (Port.BytesToRead == 0 && j < 5 && !DisplayFound)
207+
int k = 0;
208+
while (Port.BytesToRead == 0 && k < 5 && !DisplayFound)
160209
{
161210
Thread.Sleep(200);
162-
j++;
211+
k++;
163212
}
164213

165214
Port.Close();
@@ -268,14 +317,16 @@ void OnDestroy()
268317
[KSPAddon(KSPAddon.Startup.Flight, false)]
269318
public class KSPSerialIO : MonoBehaviour
270319
{
271-
private float lastUpdate = 0.0f;
272-
private float refreshrate = 0.08f;
320+
private double lastUpdate = 0.0f;
321+
public double refreshrate = 1.0f;
322+
273323
private ScreenMessageStyle KSPIOScreenStyle = ScreenMessageStyle.LOWER_CENTER;
274324
Vessel ActiveVessel;
275325

276326
void Awake()
277327
{
278328
ScreenMessages.PostScreenMessage("IO awake", 10f, KSPIOScreenStyle);
329+
refreshrate = SettingsNStuff.refreshrate;
279330
}
280331

281332
void Start()

‎settings

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var config = KSP.IO.PluginConfiguration.CreateForType<YourClass>();
2+
[23:23:52] zitron FA: do you need to specify a file name?
3+
[23:23:57] nick.noether: then config.load() / config.save()
4+
[23:23:57] nick.noether: no
5+
[23:24:02] nick.noether: you do that via the classname
6+
[23:24:13] zitron FA: where is it saved then?
7+
[23:24:28] nick.noether: it looks up the assembly your class belongs to (i.e. your dll) then adds PluginData/ as a folder and creates a config file there
8+
[23:24:41] zitron FA: ah
9+
[23:24:58] zitron FA: it is editable or some binary file?
10+
[23:25:02] nick.noether: editable
11+
[23:25:13] zitron FA: perfect thanks alot!
12+
[23:25:33] nick.noether: simple xml file
13+
[23:25:53] nick.noether: you can just do config.SetValue(name,value) and done
14+
[23:26:02] zitron FA: very nice

0 commit comments

Comments
 (0)
Please sign in to comment.