Browse Source

Initialisierung verbessert

develop
Flo Smilari 1 year ago
parent
commit
1d69c1ff92
4 changed files with 178 additions and 97 deletions
  1. BIN
      .vs/FSRemotePowerSwitch/v16/.suo
  2. 71
    13
      Form1.Designer.cs
  3. 106
    84
      Form1.cs
  4. 1
    0
      Message.cs

BIN
.vs/FSRemotePowerSwitch/v16/.suo View File


+ 71
- 13
Form1.Designer.cs View File

@@ -57,6 +57,10 @@ namespace FSRemotePowerSwitch {
this.button1 = new System.Windows.Forms.Button();
this.webCamRotatorPowerSwitch = new FSRemotePowerSwitch.SButton();
this.powerSwitchButton = new FSRemotePowerSwitch.SButton();
this.webCamRotVelocityLbl = new System.Windows.Forms.Label();
this.webCamRotatorVelocitySwitch = new FSRemotePowerSwitch.SButton();
this.webCamRotatorVelocitySlowLbl = new System.Windows.Forms.Label();
this.webCamRotatorVelocityFastLbl = new System.Windows.Forms.Label();
this.statusBar.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.powerStateLed)).BeginInit();
@@ -145,7 +149,7 @@ namespace FSRemotePowerSwitch {
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.label1.Location = new System.Drawing.Point(44, 219);
this.label1.Location = new System.Drawing.Point(44, 230);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(135, 19);
this.label1.TabIndex = 13;
@@ -247,7 +251,7 @@ namespace FSRemotePowerSwitch {
this.powerStateLed.BackgroundImage = global::FSRemotePowerSwitch.Properties.Resources.blue_led_off;
this.powerStateLed.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.powerStateLed.InitialImage = null;
this.powerStateLed.Location = new System.Drawing.Point(92, 258);
this.powerStateLed.Location = new System.Drawing.Point(92, 269);
this.powerStateLed.Name = "powerStateLed";
this.powerStateLed.Size = new System.Drawing.Size(40, 40);
this.powerStateLed.TabIndex = 12;
@@ -261,7 +265,7 @@ namespace FSRemotePowerSwitch {
this.webCamRotator.KnobBackColor = System.Drawing.Color.Silver;
this.webCamRotator.KnobPointerStyle = KnobControl.KnobControl.KnobPointerStyles.circle;
this.webCamRotator.LargeChange = 5;
this.webCamRotator.Location = new System.Drawing.Point(247, 122);
this.webCamRotator.Location = new System.Drawing.Point(234, 133);
this.webCamRotator.Maximum = 180;
this.webCamRotator.Minimum = -180;
this.webCamRotator.Name = "webCamRotator";
@@ -287,7 +291,7 @@ namespace FSRemotePowerSwitch {
this.rotatorActValue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.rotatorActValue.Font = new System.Drawing.Font("Bahnschrift", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.rotatorActValue.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(151)))), ((int)(((byte)(156)))));
this.rotatorActValue.Location = new System.Drawing.Point(309, 264);
this.rotatorActValue.Location = new System.Drawing.Point(296, 275);
this.rotatorActValue.Name = "rotatorActValue";
this.rotatorActValue.Size = new System.Drawing.Size(40, 20);
this.rotatorActValue.TabIndex = 18;
@@ -298,7 +302,7 @@ namespace FSRemotePowerSwitch {
this.webCamRotatorLbl.AutoSize = true;
this.webCamRotatorLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.webCamRotatorLbl.ForeColor = System.Drawing.SystemColors.HotTrack;
this.webCamRotatorLbl.Location = new System.Drawing.Point(261, 96);
this.webCamRotatorLbl.Location = new System.Drawing.Point(248, 96);
this.webCamRotatorLbl.Name = "webCamRotatorLbl";
this.webCamRotatorLbl.Size = new System.Drawing.Size(137, 19);
this.webCamRotatorLbl.TabIndex = 19;
@@ -309,7 +313,7 @@ namespace FSRemotePowerSwitch {
this.webCamRotPowerLbl.AutoSize = true;
this.webCamRotPowerLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.webCamRotPowerLbl.ForeColor = System.Drawing.SystemColors.HotTrack;
this.webCamRotPowerLbl.Location = new System.Drawing.Point(412, 219);
this.webCamRotPowerLbl.Location = new System.Drawing.Point(400, 230);
this.webCamRotPowerLbl.Name = "webCamRotPowerLbl";
this.webCamRotPowerLbl.Size = new System.Drawing.Size(157, 19);
this.webCamRotPowerLbl.TabIndex = 21;
@@ -320,7 +324,7 @@ namespace FSRemotePowerSwitch {
this.webCamRotatorPowerStatusLed.BackgroundImage = global::FSRemotePowerSwitch.Properties.Resources.blue_led_off;
this.webCamRotatorPowerStatusLed.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.webCamRotatorPowerStatusLed.InitialImage = null;
this.webCamRotatorPowerStatusLed.Location = new System.Drawing.Point(525, 258);
this.webCamRotatorPowerStatusLed.Location = new System.Drawing.Point(512, 269);
this.webCamRotatorPowerStatusLed.Name = "webCamRotatorPowerStatusLed";
this.webCamRotatorPowerStatusLed.Size = new System.Drawing.Size(40, 40);
this.webCamRotatorPowerStatusLed.TabIndex = 20;
@@ -331,7 +335,7 @@ namespace FSRemotePowerSwitch {
this.webCamRotPwrStatusLbl.AutoSize = true;
this.webCamRotPwrStatusLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 10F, System.Drawing.FontStyle.Bold);
this.webCamRotPwrStatusLbl.ForeColor = System.Drawing.SystemColors.HotTrack;
this.webCamRotPwrStatusLbl.Location = new System.Drawing.Point(524, 241);
this.webCamRotPwrStatusLbl.Location = new System.Drawing.Point(511, 252);
this.webCamRotPwrStatusLbl.Name = "webCamRotPwrStatusLbl";
this.webCamRotPwrStatusLbl.Size = new System.Drawing.Size(43, 17);
this.webCamRotPwrStatusLbl.TabIndex = 22;
@@ -342,7 +346,7 @@ namespace FSRemotePowerSwitch {
this.webCamRotatorPowerOffLbl.AutoSize = true;
this.webCamRotatorPowerOffLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold);
this.webCamRotatorPowerOffLbl.ForeColor = System.Drawing.Color.Red;
this.webCamRotatorPowerOffLbl.Location = new System.Drawing.Point(413, 267);
this.webCamRotatorPowerOffLbl.Location = new System.Drawing.Point(400, 278);
this.webCamRotatorPowerOffLbl.Name = "webCamRotatorPowerOffLbl";
this.webCamRotatorPowerOffLbl.Size = new System.Drawing.Size(34, 19);
this.webCamRotatorPowerOffLbl.TabIndex = 23;
@@ -353,7 +357,7 @@ namespace FSRemotePowerSwitch {
this.webCamRotatorPowerOnLbl.AutoSize = true;
this.webCamRotatorPowerOnLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold);
this.webCamRotatorPowerOnLbl.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(151)))), ((int)(((byte)(156)))));
this.webCamRotatorPowerOnLbl.Location = new System.Drawing.Point(491, 268);
this.webCamRotatorPowerOnLbl.Location = new System.Drawing.Point(478, 279);
this.webCamRotatorPowerOnLbl.Name = "webCamRotatorPowerOnLbl";
this.webCamRotatorPowerOnLbl.Size = new System.Drawing.Size(30, 19);
this.webCamRotatorPowerOnLbl.TabIndex = 24;
@@ -364,7 +368,7 @@ namespace FSRemotePowerSwitch {
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.ForeColor = System.Drawing.SystemColors.HotTrack;
this.label2.Location = new System.Drawing.Point(427, 135);
this.label2.Location = new System.Drawing.Point(400, 96);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(128, 19);
this.label2.TabIndex = 26;
@@ -372,7 +376,7 @@ namespace FSRemotePowerSwitch {
//
// button1
//
this.button1.Location = new System.Drawing.Point(456, 165);
this.button1.Location = new System.Drawing.Point(403, 123);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 26);
this.button1.TabIndex = 27;
@@ -382,7 +386,7 @@ namespace FSRemotePowerSwitch {
//
// webCamRotatorPowerSwitch
//
this.webCamRotatorPowerSwitch.Location = new System.Drawing.Point(447, 267);
this.webCamRotatorPowerSwitch.Location = new System.Drawing.Point(434, 278);
this.webCamRotatorPowerSwitch.MinimumSize = new System.Drawing.Size(45, 22);
this.webCamRotatorPowerSwitch.Name = "webCamRotatorPowerSwitch";
this.webCamRotatorPowerSwitch.OffBackColor = System.Drawing.Color.Gray;
@@ -408,11 +412,61 @@ namespace FSRemotePowerSwitch {
this.powerSwitchButton.UseVisualStyleBackColor = true;
this.powerSwitchButton.CheckStateChanged += new System.EventHandler(this.onSwitchCheckStateChanged);
//
// webCamRotVelocityLbl
//
this.webCamRotVelocityLbl.AutoSize = true;
this.webCamRotVelocityLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.webCamRotVelocityLbl.ForeColor = System.Drawing.SystemColors.HotTrack;
this.webCamRotVelocityLbl.Location = new System.Drawing.Point(400, 165);
this.webCamRotVelocityLbl.Name = "webCamRotVelocityLbl";
this.webCamRotVelocityLbl.Size = new System.Drawing.Size(170, 19);
this.webCamRotVelocityLbl.TabIndex = 28;
this.webCamRotVelocityLbl.Text = "WebCam Geschwindigkeit";
//
// webCamRotatorVelocitySwitch
//
this.webCamRotatorVelocitySwitch.Location = new System.Drawing.Point(447, 194);
this.webCamRotatorVelocitySwitch.MinimumSize = new System.Drawing.Size(45, 22);
this.webCamRotatorVelocitySwitch.Name = "webCamRotatorVelocitySwitch";
this.webCamRotatorVelocitySwitch.OffBackColor = System.Drawing.Color.Gray;
this.webCamRotatorVelocitySwitch.OffToggleColor = System.Drawing.Color.Gainsboro;
this.webCamRotatorVelocitySwitch.OnBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(151)))), ((int)(((byte)(156)))));
this.webCamRotatorVelocitySwitch.OnToggleColor = System.Drawing.Color.WhiteSmoke;
this.webCamRotatorVelocitySwitch.Size = new System.Drawing.Size(45, 22);
this.webCamRotatorVelocitySwitch.TabIndex = 31;
this.webCamRotatorVelocitySwitch.UseVisualStyleBackColor = true;
//
// webCamRotatorVelocitySlowLbl
//
this.webCamRotatorVelocitySlowLbl.AutoSize = true;
this.webCamRotatorVelocitySlowLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold);
this.webCamRotatorVelocitySlowLbl.ForeColor = System.Drawing.Color.Red;
this.webCamRotatorVelocitySlowLbl.Location = new System.Drawing.Point(400, 194);
this.webCamRotatorVelocitySlowLbl.Name = "webCamRotatorVelocitySlowLbl";
this.webCamRotatorVelocitySlowLbl.Size = new System.Drawing.Size(45, 19);
this.webCamRotatorVelocitySlowLbl.TabIndex = 29;
this.webCamRotatorVelocitySlowLbl.Text = "SLOW";
//
// webCamRotatorVelocityFastLbl
//
this.webCamRotatorVelocityFastLbl.AutoSize = true;
this.webCamRotatorVelocityFastLbl.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 12F, System.Drawing.FontStyle.Bold);
this.webCamRotatorVelocityFastLbl.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(151)))), ((int)(((byte)(156)))));
this.webCamRotatorVelocityFastLbl.Location = new System.Drawing.Point(494, 195);
this.webCamRotatorVelocityFastLbl.Name = "webCamRotatorVelocityFastLbl";
this.webCamRotatorVelocityFastLbl.Size = new System.Drawing.Size(38, 19);
this.webCamRotatorVelocityFastLbl.TabIndex = 30;
this.webCamRotatorVelocityFastLbl.Text = "FAST";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(584, 361);
this.Controls.Add(this.webCamRotatorVelocitySwitch);
this.Controls.Add(this.webCamRotatorVelocitySlowLbl);
this.Controls.Add(this.webCamRotatorVelocityFastLbl);
this.Controls.Add(this.webCamRotVelocityLbl);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.webCamRotatorPowerSwitch);
@@ -490,6 +544,10 @@ namespace FSRemotePowerSwitch {
private System.Windows.Forms.Label webCamRotatorPowerOnLbl;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label webCamRotVelocityLbl;
private SButton webCamRotatorVelocitySwitch;
private System.Windows.Forms.Label webCamRotatorVelocitySlowLbl;
private System.Windows.Forms.Label webCamRotatorVelocityFastLbl;
}
}

+ 106
- 84
Form1.cs View File

@@ -22,12 +22,12 @@ namespace FSRemotePowerSwitch {
set { connected = value; }
}
private bool requestPending;
private Boolean RequestPending {
private static bool requestPending;
private static Boolean RequestPending {
[MethodImpl(MethodImplOptions.Synchronized)]
get { return requestPending; }
[MethodImpl(MethodImplOptions.Synchronized)]
set { requestPending = value; System.Console.WriteLine(value); }
set { requestPending = value; }
}
private Boolean initializing;
@@ -38,7 +38,6 @@ namespace FSRemotePowerSwitch {
public MainForm() {
initializing = false;
Connected = false;
RequestPending = false;
firmwareVersion = new PropertiesHandler(SETTINGS_FILE).get(EXP_FIRMWARE_VER, "V0.0.1");
timerCount = 0;
InitializeComponent();
@@ -75,7 +74,15 @@ namespace FSRemotePowerSwitch {
while (stopwatch.ElapsedMilliseconds < 3000) {
Thread.Sleep(50);
if (Connected) {
reconnectBtn.Visible = false;
waitForPendingRequest();
sendRelayStatusCmd();
waitForPendingRequest();
sendWebCamStatusCmd();
waitForPendingRequest();
sendWebCamActualAzimutCmd();
BeginInvoke(new EventHandler(delegate {
reconnectBtn.Visible = false;
}));
break;
}
}
@@ -92,6 +99,12 @@ namespace FSRemotePowerSwitch {
Cursor.Position = Cursor.Position;
}
private void waitForPendingRequest() {
while(RequestPending) {
Thread.Sleep(2);
}
}
private void initializeSerialPortComboBox() {
initializing = true;
string[] ports = SerialPort.GetPortNames();
@@ -118,68 +131,71 @@ namespace FSRemotePowerSwitch {
}
private void onSerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) {
string s = readSerialPortData((SerialPort)sender);
try {
while (serialPort.IsOpen && serialPort.BytesToRead > 0) {
string s = readSerialPortData((SerialPort)sender);
Message msg = JsonConvert.DeserializeObject<Message>(s);
if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_VER) && msg.Val.Equals(firmwareVersion)) {
RequestPending = false;
BeginInvoke(new EventHandler(delegate {
sendRelayStatusCmd();
Connected = true;
initStatusLbl.Text = "Verbunden";
connectedSerialPortLbl.Text = "Port: " + ((SerialPort)sender).PortName;
fwVersionLbl.Text = "Ver: " + firmwareVersion;
initProgressBar.Style = ProgressBarStyle.Continuous;
initProgressBar.Value = 100;
openBtn.Enabled = false;
closeBtn.Enabled = true;
powerSwitchButton.Enabled = true;
statusTimer.Enabled = true;
}));
} else if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_STATUS_RELAY)) {
RequestPending = false;
BeginInvoke(new EventHandler(delegate {
if (msg.Val.Equals(Message.CMD_VAL_ON)) {
powerStateLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_on;
powerSwitchButton.CheckStateChanged -= onSwitchCheckStateChanged;
powerSwitchButton.Checked = true;
powerSwitchButton.CheckStateChanged += onSwitchCheckStateChanged;
} else if (msg.Val.Equals(Message.CMD_VAL_OFF)) {
powerStateLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_off;
powerSwitchButton.CheckStateChanged -= onSwitchCheckStateChanged;
powerSwitchButton.Checked = false;
powerSwitchButton.CheckStateChanged += onSwitchCheckStateChanged;
}
}));
} else if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_STATUS_CAM)) {
RequestPending = false;
BeginInvoke(new EventHandler(delegate {
if (msg.Val.Equals(Message.CMD_VAL_ON)) {
webCamRotatorPowerStatusLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_on;
webCamRotatorPowerSwitch.CheckStateChanged -= onWebCamPowerSwitchCheckStateChanged;
webCamRotatorPowerSwitch.Checked = true;
webCamRotatorPowerSwitch.CheckStateChanged += onWebCamPowerSwitchCheckStateChanged;
} else if (msg.Val.Equals(Message.CMD_VAL_OFF)) {
webCamRotatorPowerStatusLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_off;
webCamRotatorPowerSwitch.CheckStateChanged -= onWebCamPowerSwitchCheckStateChanged;
webCamRotatorPowerSwitch.Checked = false;
webCamRotatorPowerSwitch.CheckStateChanged += onWebCamPowerSwitchCheckStateChanged;
}
}));
} else if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_ZERO_CAM)) {
RequestPending = false;
BeginInvoke(new EventHandler(delegate {
webCamRotator.Value = int.Parse(msg.Val);
}));
try {
if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_VER) && msg.Val.Equals(firmwareVersion)) {
BeginInvoke(new EventHandler(delegate {
Connected = true;
initStatusLbl.Text = "Verbunden";
connectedSerialPortLbl.Text = "Port: " + ((SerialPort)sender).PortName;
fwVersionLbl.Text = "Ver: " + firmwareVersion;
initProgressBar.Style = ProgressBarStyle.Continuous;
initProgressBar.Value = 100;
openBtn.Enabled = false;
closeBtn.Enabled = true;
powerSwitchButton.Enabled = true;
statusTimer.Enabled = true;
}));
} else if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_STATUS_RELAY)) {
RequestPending = false;
BeginInvoke(new EventHandler(delegate {
if (msg.Val.Equals(Message.CMD_VAL_ON)) {
powerStateLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_on;
powerSwitchButton.CheckStateChanged -= onSwitchCheckStateChanged;
powerSwitchButton.Checked = true;
powerSwitchButton.CheckStateChanged += onSwitchCheckStateChanged;
} else if (msg.Val.Equals(Message.CMD_VAL_OFF)) {
powerStateLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_off;
powerSwitchButton.CheckStateChanged -= onSwitchCheckStateChanged;
powerSwitchButton.Checked = false;
powerSwitchButton.CheckStateChanged += onSwitchCheckStateChanged;
}
}));
} else if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_STATUS_CAM)) {
RequestPending = false;
BeginInvoke(new EventHandler(delegate {
if (msg.Val.Equals(Message.CMD_VAL_ON)) {
webCamRotatorPowerStatusLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_on;
webCamRotatorPowerSwitch.CheckStateChanged -= onWebCamPowerSwitchCheckStateChanged;
webCamRotatorPowerSwitch.Checked = true;
webCamRotatorPowerSwitch.CheckStateChanged += onWebCamPowerSwitchCheckStateChanged;
} else if (msg.Val.Equals(Message.CMD_VAL_OFF)) {
webCamRotatorPowerStatusLed.BackgroundImage = FSRemotePowerSwitch.Properties.Resources.blue_led_off;
webCamRotatorPowerSwitch.CheckStateChanged -= onWebCamPowerSwitchCheckStateChanged;
webCamRotatorPowerSwitch.Checked = false;
webCamRotatorPowerSwitch.CheckStateChanged += onWebCamPowerSwitchCheckStateChanged;
}
}));
} else if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_ZERO_CAM)) {
BeginInvoke(new EventHandler(delegate {
webCamRotator.Value = int.Parse(msg.Val);
}));
} else if (msg.Sender.Equals(Message.CMD_SENDER_AN) && msg.Cmd.Equals(Message.CMD_ACT_AZIMUT_CAM)) {
RequestPending = false;
BeginInvoke(new EventHandler(delegate {
webCamRotator.Value = int.Parse(msg.Val);
}));
}
} catch (Exception ex) when (ex is Newtonsoft.Json.JsonReaderException ||
ex is Newtonsoft.Json.JsonSerializationException) {
initStatusLbl.Text = "Json Deserialize Exception!";
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
serialPort.Dispose();
}
} catch (Exception ex) /*when (ex is Newtonsoft.Json.JsonReaderException ||
ex is Newtonsoft.Json.JsonSerializationException)*/ {
RequestPending = false;
initStatusLbl.Text = "Json Deserialize Exception!";
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
serialPort.Dispose();
}
}
@@ -224,10 +240,13 @@ namespace FSRemotePowerSwitch {
private String readSerialPortData(SerialPort serialPort) {
string data = "";
while (serialPort.BytesToRead > 0) {
data += serialPort.ReadExisting();
Thread.Sleep(50);
}
int character = 0; ;
do {
if (serialPort.IsOpen) {
character = serialPort.ReadByte();
data += (char)character;
}
} while ((char)character != '}');
return data;
}
@@ -237,9 +256,8 @@ namespace FSRemotePowerSwitch {
msg.Cmd = Message.CMD_VER;
msg.Val = null;
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen && !RequestPending) {
if (serialPort.IsOpen) {
serialPort.Write(msJson);
RequestPending = true;
}
}
@@ -249,9 +267,8 @@ namespace FSRemotePowerSwitch {
msg.Cmd = Message.CMD_POWER_RELAY;
msg.Val = value;
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen && !RequestPending) {
if (serialPort.IsOpen) {
serialPort.Write(msJson);
RequestPending = true;
}
}
@@ -261,7 +278,7 @@ namespace FSRemotePowerSwitch {
msg.Cmd = Message.CMD_STATUS_RELAY;
msg.Val = null;
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen && !RequestPending) {
if (serialPort.IsOpen) {
serialPort.Write(msJson);
RequestPending = true;
}
@@ -273,7 +290,7 @@ namespace FSRemotePowerSwitch {
msg.Cmd = Message.CMD_STATUS_CAM;
msg.Val = null;
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen && !RequestPending) {
if (serialPort.IsOpen) {
serialPort.Write(msJson);
RequestPending = true;
}
@@ -285,9 +302,8 @@ namespace FSRemotePowerSwitch {
msg.Cmd = Message.CMD_POWER_CAM;
msg.Val = value;
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen && !RequestPending) {
if (serialPort.IsOpen) {
serialPort.Write(msJson);
RequestPending = true;
}
}
@@ -297,9 +313,8 @@ namespace FSRemotePowerSwitch {
msg.Cmd = Message.CMD_ZERO_CAM;
msg.Val = "0";
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen && !RequestPending) {
if (serialPort.IsOpen) {
serialPort.Write(msJson);
RequestPending = true;
}
}
@@ -309,11 +324,23 @@ namespace FSRemotePowerSwitch {
msg.Cmd = Message.CMD_AZIMUT_CAM;
msg.Val = webCamRotator.Value.ToString();
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen && !RequestPending) {
if (serialPort.IsOpen) {
serialPort.Write(msJson);
}
}
private void sendWebCamActualAzimutCmd() {
Message msg = new Message();
msg.Sender = Message.CMD_SENDER_PC;
msg.Cmd = Message.CMD_ACT_AZIMUT_CAM;
msg.Val = "";
string msJson = JsonConvert.SerializeObject(msg);
if (serialPort.IsOpen) {
serialPort.Write(msJson);
RequestPending = true;
}
}
private void onStatusTimerTick(object sender, EventArgs e) {
if (timerCount % 2 == 0) {
sendWebCamStatusCmd();
@@ -324,11 +351,6 @@ namespace FSRemotePowerSwitch {
}
private void onMainFormClosing(object sender, FormClosingEventArgs e) {
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (RequestPending && stopwatch.ElapsedMilliseconds < 2000) {
Thread.Sleep(50);
}
if (serialPort.IsOpen) {
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();

+ 1
- 0
Message.cs View File

@@ -24,6 +24,7 @@ namespace FSRemotePowerSwitch {
public static string CMD_STATUS_CAM = "Status_Cam";
public static string CMD_POWER_CAM = "Power_Cam";
public static string CMD_AZIMUT_CAM = "Azimut_Cam";
public static string CMD_ACT_AZIMUT_CAM = "Actual_Azimut_Cam";
public static string CMD_ZERO_CAM = "Zero_Cam";
public static string CMD_VAL_ON = "On";
public static string CMD_VAL_OFF = "Off";

Loading…
Cancel
Save