8
8
9
9
using OpenNETCF . IO . Ports ;
10
10
using UnityEngine ;
11
+ using KSP . IO ;
11
12
12
13
namespace KSPSerialIO
13
14
{
@@ -44,6 +45,37 @@ public struct HandShakePacket
44
45
public byte M3 ;
45
46
}
46
47
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
+
47
79
[ KSPAddon ( KSPAddon . Startup . MainMenu , false ) ]
48
80
public class KSPSerialPort : MonoBehaviour
49
81
{
@@ -65,6 +97,8 @@ public static void sendPacket(object anything)
65
97
byte [ ] Packet = new byte [ size + 4 ] ;
66
98
67
99
//Packet = [header][size][payload][checksum];
100
+ //Header = [Header1=0xBE][Header2=0xEF]
101
+ //size = [payload.length (0-255)]
68
102
69
103
for ( int i = 0 ; i < size ; i ++ )
70
104
{
@@ -106,6 +140,7 @@ void initializeDataPackets()
106
140
107
141
void Awake ( )
108
142
{
143
+ print ( "KSPSerialIO: Version 0.12" ) ;
109
144
print ( "KSPSerialIO: Getting serial ports..." ) ;
110
145
initializeDataPackets ( ) ;
111
146
@@ -120,17 +155,31 @@ void Awake()
120
155
}
121
156
else
122
157
{
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 ) ;
124
161
125
162
print ( "KSPSerialIO: Found " + names . Length . ToString ( ) + " serial ports" ) ;
126
163
127
164
//look through all found ports for our display
165
+ int j = 0 ;
166
+
128
167
foreach ( string PortName in names )
129
168
{
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
+ }
132
179
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 ) ;
134
183
Port . ReceivedBytesThreshold = 3 ;
135
184
136
185
//print("KSPSerialIO: receive threshold " + Port.ReceivedBytesThreshold.ToString());
@@ -151,15 +200,15 @@ void Awake()
151
200
//secret handshake
152
201
if ( Port . IsOpen )
153
202
{
154
- Thread . Sleep ( 2500 ) ;
203
+ Thread . Sleep ( SettingsNStuff . HandshakeDelay ) ;
155
204
sendPacket ( HPacket ) ;
156
205
157
206
//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 )
160
209
{
161
210
Thread . Sleep ( 200 ) ;
162
- j ++ ;
211
+ k ++ ;
163
212
}
164
213
165
214
Port . Close ( ) ;
@@ -268,14 +317,16 @@ void OnDestroy()
268
317
[ KSPAddon ( KSPAddon . Startup . Flight , false ) ]
269
318
public class KSPSerialIO : MonoBehaviour
270
319
{
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
+
273
323
private ScreenMessageStyle KSPIOScreenStyle = ScreenMessageStyle . LOWER_CENTER ;
274
324
Vessel ActiveVessel ;
275
325
276
326
void Awake ( )
277
327
{
278
328
ScreenMessages . PostScreenMessage ( "IO awake" , 10f , KSPIOScreenStyle ) ;
329
+ refreshrate = SettingsNStuff . refreshrate ;
279
330
}
280
331
281
332
void Start ( )
0 commit comments