Guest User

C# deadlock

a guest
Apr 20th, 2012
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 35.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO.Ports;
  10. using System.Threading;
  11.  
  12.  
  13.  
  14. namespace Eurocoin_Voeding_Tester
  15. {
  16.    
  17.     public partial class Form1 : Form
  18.     {
  19.         /*initialisatie globale variable*/
  20.         public List<byte> rxBuffer = new List<byte>();
  21.         byte bMin12vEnabled = TRUE;
  22.         byte b8HourEnabled;
  23.  
  24.         const byte RequestToSendVoltage =       1;
  25.         const byte RequestToSendCurrent =       2;
  26.         const byte ConectionCheck =             3;
  27.         const byte RequestToSendMaxVoltage =    4;
  28.         const byte RequestToSendMinVoltage =    5;
  29.         const byte RequestToSendMaxError =      6;
  30.         const byte RequestToSendMinError =      7;
  31.         const byte RequestAll =                 8;
  32.  
  33.         const byte ExtraLoad45V = 1;
  34.         const byte ExtraLoad24V = 2;
  35.         const byte ExtraLoad12V = 4;
  36.         const byte ExtraLoad5V = 8;
  37.  
  38.         byte ExtraLoadControl;
  39.  
  40.         byte[] TimeRemaining = new byte[7];
  41.  
  42.         byte changeStartStop;
  43.  
  44.         byte soakteststarted;
  45.  
  46.  
  47.         const byte RequestToStartSoakTest =     10;
  48.         const byte RequestToStopSoakTest =      11;
  49.         const byte RequestToResetValues =       12;
  50.        
  51.  
  52.         byte DataRequest = 1;
  53.         byte numberOfRequests = 7;
  54.  
  55.         bool BussySending = false;
  56.         bool connected = false;
  57.  
  58.         byte[] Voltage45V = new byte[7];
  59.         byte[] Voltage24V = new byte[7];
  60.         byte[] Voltage12V = new byte[7];
  61.         byte[] Voltage5V = new byte[7];
  62.         byte[] Voltagem12V = new byte[7];
  63.  
  64.         byte[] VoltageMax45V = new byte[7];
  65.         byte[] VoltageMax24V = new byte[7];
  66.         byte[] VoltageMax12V = new byte[7];
  67.         byte[] VoltageMax5V = new byte[7];
  68.         byte[] VoltageMinm12V = new byte[7];
  69.  
  70.         byte[] VoltageMin45V = new byte[7];
  71.         byte[] VoltageMin24V = new byte[7];
  72.         byte[] VoltageMin12V = new byte[7];
  73.         byte[] VoltageMin5V = new byte[7];
  74.         byte[] VoltageMaxm12V = new byte[7];
  75.  
  76.         byte[] MaxError = new byte[7];
  77.         byte[] MinError = new byte[7];
  78.  
  79.         byte EnablePowerSupply;
  80.         byte PowersupplyIsOn;
  81.         byte ExtraLoadStatus;
  82.  
  83.         byte SoakTestReady;
  84.         byte SoakTestMessageShown;
  85.  
  86.         byte SoakTestBussy;
  87.         byte Progressbar1Value;
  88.  
  89.         byte portnamecount;
  90.        
  91.         const byte TRUE = 0x01;
  92.         const byte FALSE = 0x00;
  93.  
  94.         const byte SOH = 0x01;
  95.         const byte KEY = 0xaa;
  96.  
  97.         byte RxByteCount_vuc = 253;
  98.        
  99.        
  100.        
  101.         public Form1()
  102.         {
  103.             InitializeComponent();
  104.  
  105.             radioButton1.Checked = true;
  106.             radioButton3.Checked = true;
  107.  
  108.             if (checkBox1.Checked == true || checkBox2.Checked == true || checkBox3.Checked == true || checkBox4.Checked == true)
  109.                 button3.Text = "All off";
  110.             else
  111.                button3.Text = "All on";
  112.  
  113.             timer1.Tick += new EventHandler(timer1_Tick); // Everytime timer ticks, timer_Tick will be called
  114.             timer1.Interval = 300;             // Timer will tick evert 300ms
  115.             timer1.Enabled = true;                       // Enable the timer
  116.             timer1.Stop();                              // Start the timer
  117.  
  118.             timer2.Tick += new EventHandler(timer2_Tick); // Everytime timer ticks, timer_Tick will be called
  119.             timer2.Interval = 100;             // Timer will tick evert 100ms
  120.             timer2.Enabled = true;                       // Enable the timer
  121.             timer2.Start();                              // Start the timer
  122.  
  123.             timer3.Tick += new EventHandler(timer3_Tick); // Everytime timer ticks, timer_Tick will be called
  124.             timer3.Interval = 500;             // Timer will tick evert 500ms
  125.             timer3.Enabled = true;                       // Enable the timer
  126.             timer3.Start();                              // Start the timer
  127.  
  128.             timer4.Tick += new EventHandler(timer4_Tick); // Everytime timer ticks, timer_Tick will be called
  129.             timer4.Interval = ((1000 * 60) * 2);             // Timer will tick every 2 min
  130.             timer4.Enabled = true;                       // Enable the timer
  131.             timer4.Stop();                              // Start the timer
  132.  
  133.             this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
  134.             serialPort1.ReceivedBytesThreshold = 1;            
  135.         }
  136.  
  137.         void timer1_Tick(object sender, EventArgs e)
  138.         {
  139.             timer1.Stop();
  140.             if (connected == true)
  141.             {
  142.                 connected = false;
  143.             }
  144.             try
  145.             {
  146.                 serialPort1.Close();
  147.             }
  148.             catch { }
  149.             BussySending = false;
  150.         }
  151.  
  152.         void timer2_Tick(object sender, EventArgs e)
  153.         {
  154.             if (serialPort1.IsOpen == false)
  155.             {
  156.                 BussySending = false;
  157.                 connected = false;
  158.                 SoakTestBussy = FALSE;
  159.                 bMin12vEnabled = TRUE;
  160.  
  161.             }
  162.                 string[] Portnames = SerialPort.GetPortNames();
  163.                 if (portnamecount > Portnames.Length)
  164.                     portnamecount = 0;
  165.                 if ((connected == false) && (Portnames.Length != 0))
  166.                 {
  167.                     for (;serialPort1.IsOpen == false; portnamecount++)
  168.                     {
  169.                         if ((Portnames.Length != 0))
  170.                         {
  171.                             if (portnamecount > (Portnames.Length - 1))
  172.                                 portnamecount = 0;
  173.                             serialPort1.PortName = Portnames[portnamecount];
  174.                             Portnames = SerialPort.GetPortNames();
  175.                             try { serialPort1.Open(); }
  176.                             catch { }
  177.                         }
  178.                         else break;
  179.                     }
  180.             }
  181.  
  182.             if (BussySending == false)
  183.             {
  184.                 SendBlock(DataRequest);
  185.                 DataRequest++;
  186.                 if (DataRequest > numberOfRequests)
  187.                     DataRequest = 1;
  188.             }
  189.            
  190.             if (connected == false)
  191.             {
  192.                 toolStripStatusLabel1.Text = "Not connected";
  193.             }
  194.             else
  195.             {
  196.                 toolStripStatusLabel1.Text = "Connected";
  197.             }
  198.         }
  199.  
  200.         void timer3_Tick(object sender, EventArgs e)
  201.         {
  202.             if (SoakTestReady == TRUE)
  203.                 textBox1.Text = ("Soaktest completed");
  204.             else
  205.                 textBox1.Text = ("");
  206.  
  207.             if (checkBox1.Checked == true || checkBox2.Checked == true || checkBox3.Checked == true || checkBox4.Checked == true)
  208.                 timer4.Start();
  209.             else
  210.                 timer4.Stop();
  211.  
  212.             if (checkBox1.Checked == true)
  213.                 ExtraLoadControl |= ExtraLoad45V;
  214.             else
  215.                 ExtraLoadControl &= 255 - ExtraLoad45V;
  216.  
  217.             if (checkBox2.Checked == true)
  218.                 ExtraLoadControl |= ExtraLoad24V;
  219.             else
  220.                 ExtraLoadControl &= 255 - ExtraLoad24V;
  221.  
  222.             if (checkBox3.Checked == true)
  223.                 ExtraLoadControl |= ExtraLoad12V;
  224.             else
  225.                 ExtraLoadControl &= 255 - ExtraLoad12V;
  226.  
  227.             if (checkBox4.Checked == true)
  228.                 ExtraLoadControl |= ExtraLoad5V;
  229.             else
  230.                 ExtraLoadControl &= 255 - ExtraLoad5V;
  231.  
  232.             if(PowersupplyIsOn == TRUE)
  233.                 button2.Text = "Stop Powersupply";
  234.             else
  235.                 button2.Text = "Start Powersupply";
  236.  
  237.            
  238.             if (SoakTestBussy == TRUE)
  239.             {
  240.                 button6.Enabled = false;
  241.                 toolStripStatusLabel3.Text = ByteArrayToStr(TimeRemaining);
  242.                 toolStripStatusLabel2.Text = "Time left:";
  243.                 button2.Enabled = false;
  244.                 groupBox2.Enabled = false;
  245.                 groupBox3.Enabled = false;
  246.                 groupBox4.Enabled = false;
  247.                 button4.Text = "Back";
  248.                 groupBox1.Text = "live measurements (Soaktest in progress)";
  249.                 if (changeStartStop == 0x01)
  250.                 {
  251.                     if (SoakTestBussy == soakteststarted)
  252.                     {
  253.                         changeStartStop = FALSE;
  254.                         button5.Text = "Stop";
  255.                     }
  256.                 }
  257.                 else if(changeStartStop == FALSE)
  258.                     button5.Text = "Stop";
  259.             }
  260.             else
  261.             {
  262.                 button6.Enabled = true;
  263.                 toolStripStatusLabel3.Text = "";
  264.                 toolStripStatusLabel2.Text = "";
  265.                 button2.Enabled = true;
  266.                 if (connected == true)
  267.                     groupBox2.Enabled = true;
  268.                 groupBox3.Enabled = true;
  269.                 groupBox4.Enabled = true;
  270.                 button4.Text = "Cancel";
  271.                 if (changeStartStop == 0x02)
  272.                 {
  273.                     if (SoakTestBussy == soakteststarted)
  274.                     {
  275.                         changeStartStop = FALSE;
  276.                         button5.Text = "Start";
  277.                     }
  278.                 }
  279.                 else if (changeStartStop == FALSE)
  280.                     button5.Text = "Start";
  281.  
  282.                 if (connected == false)
  283.                 {
  284.                     groupBox2.Enabled = false;
  285.                 }
  286.                 else
  287.                 {
  288.                     groupBox2.Enabled = true;
  289.                 }
  290.                 if (button1.Visible == false)
  291.                 {
  292.                     button5.Visible = true;
  293.                 }
  294.                 groupBox1.Text = "live measurements";
  295.             }
  296.  
  297.             toolStripProgressBar1.Value = Progressbar1Value;
  298.            
  299.             if (connected == true)
  300.             {
  301.                     textBox3.Text = ByteArrayToStr(Voltage45V);
  302.                     textBox6.Text = ByteArrayToStr(Voltage24V);
  303.                     textBox10.Text = ByteArrayToStr(Voltage12V);
  304.                     textBox14.Text = ByteArrayToStr(Voltage5V);
  305.                     if (bMin12vEnabled == TRUE || SoakTestBussy == FALSE)
  306.                         textBox18.Text = ByteArrayToStr(Voltagem12V);
  307.                     else if (SoakTestBussy == TRUE)
  308.                         textBox18.Text = "N/A";
  309.  
  310.                     textBox2.Text = ByteArrayToStr(VoltageMax45V);
  311.                     textBox7.Text = ByteArrayToStr(VoltageMax24V);
  312.                     textBox11.Text = ByteArrayToStr(VoltageMax12V);
  313.                     textBox15.Text = ByteArrayToStr(VoltageMax5V);
  314.                     if (bMin12vEnabled == TRUE || SoakTestBussy == FALSE)
  315.                         textBox19.Text = ByteArrayToStr(VoltageMinm12V);
  316.                     else if (SoakTestBussy == TRUE)
  317.                         textBox19.Text = "N/A";
  318.  
  319.                     textBox4.Text = ByteArrayToStr(VoltageMin45V);
  320.                     textBox5.Text = ByteArrayToStr(VoltageMin24V);
  321.                     textBox9.Text = ByteArrayToStr(VoltageMin12V);
  322.                     textBox13.Text = ByteArrayToStr(VoltageMin5V);
  323.                     if (bMin12vEnabled == TRUE || SoakTestBussy == FALSE)
  324.                         textBox17.Text = ByteArrayToStr(VoltageMaxm12V);
  325.                     else if (SoakTestBussy == TRUE)
  326.                         textBox17.Text = "N/A";
  327.             }
  328.             else
  329.             {
  330.                 textBox3.Text = "--";
  331.                 textBox6.Text = "--";
  332.                 textBox10.Text = "--";
  333.                 textBox14.Text = "--";
  334.                 textBox18.Text = "--";
  335.  
  336.                 textBox2.Text = "--";
  337.                 textBox7.Text = "--";
  338.                 textBox11.Text = "--";
  339.                 textBox15.Text = "--";
  340.                 textBox19.Text = "--";
  341.  
  342.                 textBox4.Text = "--";
  343.                 textBox5.Text = "--";
  344.                 textBox9.Text = "--";
  345.                 textBox13.Text = "--";
  346.                 textBox17.Text = "--";
  347.             }
  348.             if (MaxError[0] == 1)
  349.             {
  350.                 textBox2.ForeColor = System.Drawing.Color.Red;
  351.                 textBox2.BackColor = textBox4.BackColor;
  352.             }
  353.             else
  354.             {
  355.                 textBox2.ForeColor = System.Drawing.Color.Black;
  356.                 textBox2.BackColor = textBox4.BackColor;
  357.             }
  358.             if (MaxError[1] == 1)
  359.             {
  360.                 textBox7.ForeColor = System.Drawing.Color.Red;
  361.                 textBox7.BackColor = textBox4.BackColor;
  362.             }
  363.             else
  364.             {
  365.                 textBox7.ForeColor = System.Drawing.Color.Black;
  366.                 textBox7.BackColor = textBox4.BackColor;
  367.             }
  368.             if (MaxError[2] == 1)
  369.             {
  370.                 textBox11.ForeColor = System.Drawing.Color.Red;
  371.                 textBox11.BackColor = textBox4.BackColor;
  372.             }
  373.             else
  374.             {
  375.                 textBox11.ForeColor = System.Drawing.Color.Black;
  376.                 textBox11.BackColor = textBox4.BackColor;
  377.             }
  378.             if (MaxError[3] == 1)
  379.             {
  380.                 textBox15.ForeColor = System.Drawing.Color.Red;
  381.                 textBox15.BackColor = textBox4.BackColor;
  382.             }
  383.             else
  384.             {
  385.                 textBox15.ForeColor = System.Drawing.Color.Black;
  386.                 textBox15.BackColor = textBox4.BackColor;
  387.             }
  388.             if (bMin12vEnabled == TRUE)
  389.             {
  390.                 if (MinError[4] == 1)
  391.                 {
  392.                     textBox19.ForeColor = System.Drawing.Color.Red;
  393.                     textBox19.BackColor = textBox4.BackColor;
  394.                 }
  395.                 else
  396.                 {
  397.                     textBox19.ForeColor = System.Drawing.Color.Black;
  398.                     textBox19.BackColor = textBox4.BackColor;
  399.                 }
  400.             }
  401.             else
  402.             {
  403.                 textBox19.ForeColor = System.Drawing.Color.Black;
  404.                 textBox19.BackColor = textBox19.BackColor;
  405.             }
  406.  
  407.  
  408.  
  409.             if (MinError[0] == 1)
  410.             {
  411.                 textBox4.ForeColor = System.Drawing.Color.Red;
  412.                 textBox4.BackColor = textBox4.BackColor;
  413.             }
  414.             else
  415.             {
  416.                 textBox4.ForeColor = System.Drawing.Color.Black;
  417.                 textBox4.BackColor = textBox4.BackColor;
  418.             }
  419.             if (MinError[1] == 1)
  420.             {
  421.                 textBox5.ForeColor = System.Drawing.Color.Red;
  422.                 textBox5.BackColor = textBox4.BackColor;
  423.             }
  424.             else
  425.             {
  426.                 textBox5.ForeColor = System.Drawing.Color.Black;
  427.                 textBox5.BackColor = textBox4.BackColor;
  428.             }
  429.             if (MinError[2] == 1)
  430.             {
  431.                 textBox9.ForeColor = System.Drawing.Color.Red;
  432.                 textBox9.BackColor = textBox4.BackColor;
  433.             }
  434.             else
  435.             {
  436.                 textBox9.ForeColor = System.Drawing.Color.Black;
  437.                 textBox9.BackColor = textBox4.BackColor;
  438.             }
  439.             if (MinError[3] == 1)
  440.             {
  441.                 textBox13.ForeColor = System.Drawing.Color.Red;
  442.                 textBox13.BackColor = textBox4.BackColor;
  443.             }
  444.             else
  445.             {
  446.                 textBox13.ForeColor = System.Drawing.Color.Black;
  447.                 textBox13.BackColor = textBox4.BackColor;
  448.             }
  449.             if (bMin12vEnabled == TRUE)
  450.             {
  451.                 if (MaxError[4] == 1)
  452.                 {
  453.                     textBox17.ForeColor = System.Drawing.Color.Red;
  454.                     textBox17.BackColor = textBox4.BackColor;
  455.                 }
  456.                 else
  457.                 {
  458.                     textBox17.ForeColor = System.Drawing.Color.Black;
  459.                     textBox17.BackColor = textBox4.BackColor;
  460.                 }
  461.             }
  462.             else
  463.             {
  464.                 textBox17.ForeColor = System.Drawing.Color.Black;
  465.                 textBox17.BackColor = textBox17.BackColor;
  466.             }
  467.         }
  468.  
  469.         void timer4_Tick(object sender, EventArgs e)
  470.         {
  471.             checkBox1.Checked = false;
  472.             checkBox2.Checked = false;
  473.             checkBox3.Checked = false;
  474.             checkBox4.Checked = false;
  475.  
  476.             timer4.Stop();
  477.             MessageBox.Show("The extra loads have been switched off,\nthis happens automaticly after 2 minutes.");
  478.         }
  479.  
  480.         public static string ByteArrayToStr(byte[] byteArray)
  481.         {
  482.             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  483.             return encoding.GetString(byteArray);
  484.         }
  485.  
  486.         public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)    //receive serial data
  487.         {
  488.             byte readbyte;
  489.             try
  490.             {
  491.                 while (serialPort1.BytesToRead > 0)
  492.                 {
  493.                     readbyte = (byte)serialPort1.ReadByte();
  494.  
  495.                     if (RxByteCount_vuc < 30)
  496.                     {
  497.                         rxBuffer.Add(readbyte); //store data bytes in data
  498.                         RxByteCount_vuc++;
  499.                     }
  500.                     if (RxByteCount_vuc == 30)                      //if 30 !DATA! bytes have been received set the bytecount to start position
  501.                     {
  502.                         connected = true;
  503.                         timer1.Stop();
  504.                         RxByteCount_vuc = 253;
  505.                         serialPort1.DiscardInBuffer();
  506.                         ExecuteReceivedData();
  507.  
  508.                     }
  509.                     else
  510.                     {
  511.                         if ((RxByteCount_vuc == 255) && (readbyte == KEY))  //if value of prev byte + 1 has been received reset bytecount
  512.                         {
  513.                             RxByteCount_vuc = 0;
  514.                             BussySending = true;
  515.                         }
  516.                         else
  517.                         {
  518.                             if (RxByteCount_vuc == 255)
  519.                             {
  520.                                 RxByteCount_vuc = 253;
  521.                             }
  522.                             else
  523.                             {
  524.                                 if ((RxByteCount_vuc == 254) && (readbyte == (255 - SOH)))  //if ~SOH is next add 1 to byte count
  525.                                 {
  526.                                     RxByteCount_vuc = 255;
  527.                                 }
  528.                                 else
  529.                                 {
  530.                                     if (RxByteCount_vuc == 254)
  531.                                     {
  532.                                         RxByteCount_vuc = 253;
  533.                                     }
  534.                                     else
  535.                                     {
  536.                                         if ((RxByteCount_vuc == 253) && (readbyte == SOH))  //if ready for datablock and start of heading received reset byte counter
  537.                                         {
  538.                                             RxByteCount_vuc = 254;
  539.                                         }
  540.                                     }
  541.                                 }
  542.                             }
  543.                         }
  544.                     }
  545.                 }
  546.             }
  547.             catch { }
  548.         }
  549.  
  550.         private void ExecuteReceivedData ()
  551.         {
  552.             switch (rxBuffer[0])
  553.             {
  554.             case RequestToSendVoltage:
  555.                 {
  556.                     Voltage45V[0] = rxBuffer[10];
  557.                     Voltage45V[1] = rxBuffer[11];
  558.                     Voltage45V[2] = 46;
  559.                     Voltage45V[3] = rxBuffer[12];
  560.                     Voltage45V[4] = rxBuffer[13];
  561.                     Voltage45V[5] = 86;
  562.  
  563.                     Voltage24V[0] = rxBuffer[14];
  564.                     Voltage24V[1] = rxBuffer[15];
  565.                     Voltage24V[2] = 46;
  566.                     Voltage24V[3] = rxBuffer[16];
  567.                     Voltage24V[4] = rxBuffer[17];
  568.                     Voltage24V[5] = 86;
  569.  
  570.                     Voltage12V[0] = rxBuffer[18];
  571.                     Voltage12V[1] = rxBuffer[19];
  572.                     Voltage12V[2] = 46;
  573.                     Voltage12V[3] = rxBuffer[20];
  574.                     Voltage12V[4] = rxBuffer[21];
  575.                     Voltage12V[5] = 86;
  576.  
  577.                     Voltage5V[0] = rxBuffer[22];
  578.                     Voltage5V[1] = rxBuffer[23];
  579.                     Voltage5V[2] = 46;
  580.                     Voltage5V[3] = rxBuffer[24];
  581.                     Voltage5V[4] = rxBuffer[25];
  582.                     Voltage5V[5] = 86;
  583.  
  584.                     Voltagem12V[0] = 45;
  585.                     Voltagem12V[1] = rxBuffer[26];
  586.                     Voltagem12V[2] = rxBuffer[27];
  587.                     Voltagem12V[3] = 46;
  588.                     Voltagem12V[4] = rxBuffer[28];
  589.                     Voltagem12V[5] = rxBuffer[29];
  590.                     Voltagem12V[6] = 86;
  591.                     break;
  592.                 }
  593.             case RequestToSendMaxVoltage:
  594.                 {
  595.                     VoltageMax45V[0] = rxBuffer[10];
  596.                     VoltageMax45V[1] = rxBuffer[11];
  597.                     VoltageMax45V[2] = 46;
  598.                     VoltageMax45V[3] = rxBuffer[12];
  599.                     VoltageMax45V[4] = rxBuffer[13];
  600.                     VoltageMax45V[5] = 86;
  601.  
  602.                     VoltageMax24V[0] = rxBuffer[14];
  603.                     VoltageMax24V[1] = rxBuffer[15];
  604.                     VoltageMax24V[2] = 46;
  605.                     VoltageMax24V[3] = rxBuffer[16];
  606.                     VoltageMax24V[4] = rxBuffer[17];
  607.                     VoltageMax24V[5] = 86;
  608.  
  609.                     VoltageMax12V[0] = rxBuffer[18];
  610.                     VoltageMax12V[1] = rxBuffer[19];
  611.                     VoltageMax12V[2] = 46;
  612.                     VoltageMax12V[3] = rxBuffer[20];
  613.                     VoltageMax12V[4] = rxBuffer[21];
  614.                     VoltageMax12V[5] = 86;
  615.  
  616.                     VoltageMax5V[0] = rxBuffer[22];
  617.                     VoltageMax5V[1] = rxBuffer[23];
  618.                     VoltageMax5V[2] = 46;
  619.                     VoltageMax5V[3] = rxBuffer[24];
  620.                     VoltageMax5V[4] = rxBuffer[25];
  621.                     VoltageMax5V[5] = 86;
  622.  
  623.                     VoltageMaxm12V[0] = 45;
  624.                     VoltageMaxm12V[1] = rxBuffer[26];
  625.                     VoltageMaxm12V[2] = rxBuffer[27];
  626.                     VoltageMaxm12V[3] = 46;
  627.                     VoltageMaxm12V[4] = rxBuffer[28];
  628.                     VoltageMaxm12V[5] = rxBuffer[29];
  629.                     VoltageMaxm12V[6] = 86;
  630.                     break;
  631.                 }
  632.  
  633.             case RequestToSendMinVoltage:
  634.                 {
  635.                     VoltageMin45V[0] = rxBuffer[10];
  636.                     VoltageMin45V[1] = rxBuffer[11];
  637.                     VoltageMin45V[2] = 46;
  638.                     VoltageMin45V[3] = rxBuffer[12];
  639.                     VoltageMin45V[4] = rxBuffer[13];
  640.                     VoltageMin45V[5] = 86;
  641.  
  642.                     VoltageMin24V[0] = rxBuffer[14];
  643.                     VoltageMin24V[1] = rxBuffer[15];
  644.                     VoltageMin24V[2] = 46;
  645.                     VoltageMin24V[3] = rxBuffer[16];
  646.                     VoltageMin24V[4] = rxBuffer[17];
  647.                     VoltageMin24V[5] = 86;
  648.  
  649.                     VoltageMin12V[0] = rxBuffer[18];
  650.                     VoltageMin12V[1] = rxBuffer[19];
  651.                     VoltageMin12V[2] = 46;
  652.                     VoltageMin12V[3] = rxBuffer[20];
  653.                     VoltageMin12V[4] = rxBuffer[21];
  654.                     VoltageMin12V[5] = 86;
  655.  
  656.                     VoltageMin5V[0] = rxBuffer[22];
  657.                     VoltageMin5V[1] = rxBuffer[23];
  658.                     VoltageMin5V[2] = 46;
  659.                     VoltageMin5V[3] = rxBuffer[24];
  660.                     VoltageMin5V[4] = rxBuffer[25];
  661.                     VoltageMin5V[5] = 86;
  662.  
  663.                     VoltageMinm12V[0] = 45;
  664.                     VoltageMinm12V[1] = rxBuffer[26];
  665.                     VoltageMinm12V[2] = rxBuffer[27];
  666.                     VoltageMinm12V[3] = 46;
  667.                     VoltageMinm12V[4] = rxBuffer[28];
  668.                     VoltageMinm12V[5] = rxBuffer[29];
  669.                     VoltageMinm12V[6] = 86;
  670.                     break;
  671.                 }
  672.             case RequestToSendMaxError:
  673.                 {
  674.                     MaxError[0] = rxBuffer[10];
  675.                     MaxError[1] = rxBuffer[11];
  676.                     MaxError[2] = rxBuffer[12];
  677.                     MaxError[3] = rxBuffer[13];
  678.                     MaxError[4] = rxBuffer[14];
  679.                     break;
  680.                 }
  681.  
  682.             case RequestToSendMinError:
  683.                 {
  684.                     MinError[0] = rxBuffer[10];
  685.                     MinError[1] = rxBuffer[11];
  686.                     MinError[2] = rxBuffer[12];
  687.                     MinError[3] = rxBuffer[13];
  688.                     MinError[4] = rxBuffer[14];
  689.                     break;
  690.                 }
  691.             case RequestToSendCurrent:
  692.                 {
  693.                     break;
  694.                 }
  695.             case RequestToStartSoakTest:
  696.                 {
  697.                     soakteststarted = rxBuffer[10];
  698.                     break;
  699.                 }
  700.             case RequestToStopSoakTest :
  701.                 {
  702.                     soakteststarted = rxBuffer[10];
  703.                     break;
  704.                 }
  705.                 case ConectionCheck :
  706.                 {
  707.                     SoakTestBussy = rxBuffer[10];
  708.                     Progressbar1Value = rxBuffer[11];
  709.                     PowersupplyIsOn = rxBuffer[14];
  710.                     ExtraLoadStatus = rxBuffer[15];
  711.                     TimeRemaining[0] = 48;
  712.                     TimeRemaining[1] = rxBuffer[16];
  713.                     TimeRemaining[2] = 58;
  714.                     TimeRemaining[3] = rxBuffer[17];
  715.                     TimeRemaining[4] = rxBuffer[18];
  716.                     SoakTestReady = rxBuffer[19];
  717.                     break;
  718.                 }
  719.                
  720.                
  721.             }
  722.             rxBuffer.Clear();
  723.             BussySending = false;
  724.         }
  725.  
  726.         private void SendBlock(byte data)
  727.         {
  728.             if (serialPort1.IsOpen == true)
  729.             {
  730.                 BussySending = true;
  731.  
  732.                 byte[] TransmitData;
  733.                 TransmitData = new byte[36];
  734.  
  735.                 byte[] Handshake;
  736.                 Handshake = new byte[3];
  737.  
  738.                 Handshake[0] = SOH;
  739.                 Handshake[1] = (255 - SOH);
  740.                 Handshake[2] = KEY;
  741.  
  742.                 switch (data)
  743.                 {
  744.                     case RequestToSendMaxVoltage:
  745.                         {
  746.                             TransmitData[0] = RequestToSendMaxVoltage;
  747.                             break;
  748.                         }
  749.                     case RequestToSendMaxError :
  750.                         {
  751.                             TransmitData[0] = RequestToSendMaxError;
  752.                             break;
  753.                         }
  754.                     case RequestToSendMinError :
  755.                         {
  756.                             TransmitData[0] = RequestToSendMinError;
  757.                             break;
  758.                         }
  759.                     case RequestToSendMinVoltage:
  760.                         {
  761.                             TransmitData[0] = RequestToSendMinVoltage;
  762.                             break;
  763.                         }
  764.                     case RequestToStartSoakTest:
  765.                         {
  766.                             TransmitData[0] = data;
  767.                             TransmitData[10] = bMin12vEnabled;
  768.                             TransmitData[11] = b8HourEnabled;
  769.                             break;
  770.                         }
  771.                     case RequestToStopSoakTest:
  772.                         {
  773.                             TransmitData[0] = data;
  774.                             break;
  775.                         }
  776.                     case RequestToSendVoltage:
  777.                         {
  778.                             TransmitData[0] = data;
  779.                             break;
  780.                         }
  781.                     case RequestToSendCurrent:
  782.                         {
  783.                             TransmitData[0] = data;
  784.                             break;
  785.                         }
  786.                     case ConectionCheck:
  787.                         {
  788.                             TransmitData[0] = data;
  789.                             TransmitData[12] = ExtraLoadControl;
  790.                             TransmitData[13] = EnablePowerSupply;
  791.                             EnablePowerSupply = 0;
  792.                             break;
  793.                         }
  794.                     case RequestToResetValues:
  795.                         {
  796.                             TransmitData[0] = RequestToResetValues;
  797.                             break;
  798.                         }
  799.                 }
  800.                 try
  801.                 {
  802.                     serialPort1.Write(Handshake, 0, 3);
  803.                     serialPort1.Write(TransmitData, 0, 30);
  804.                 }
  805.                 catch
  806.                 {}
  807.                 timer1.Start();
  808.             }
  809.         }
  810.  
  811.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  812.         {
  813.             if (checkBox1.Checked == true || checkBox2.Checked == true || checkBox3.Checked == true || checkBox4.Checked == true)
  814.                 button3.Text = "All off";
  815.             else
  816.                 button3.Text = "All on";
  817.         }
  818.  
  819.         private void button3_Click(object sender, EventArgs e)
  820.         {
  821.             if (checkBox1.Checked == true || checkBox2.Checked == true || checkBox3.Checked == true || checkBox4.Checked == true)
  822.             {
  823.                 checkBox1.Checked = false;
  824.                 checkBox2.Checked = false;
  825.                 checkBox3.Checked = false;
  826.                 checkBox4.Checked = false;
  827.             }
  828.             else
  829.             {
  830.                 checkBox1.Checked = true;
  831.                 checkBox2.Checked = true;
  832.                 checkBox3.Checked = true;
  833.                 checkBox4.Checked = true;
  834.             }
  835.         }
  836.  
  837.         private void checkBox4_CheckedChanged_1(object sender, EventArgs e)
  838.         {
  839.             if (checkBox1.Checked == true || checkBox2.Checked == true || checkBox3.Checked == true || checkBox4.Checked == true)
  840.                 button3.Text = "All off";
  841.             else
  842.                 button3.Text = "All on";
  843.                
  844.         }
  845.  
  846.         private void checkBox2_CheckedChanged(object sender, EventArgs e)
  847.         {
  848.             if (checkBox1.Checked == true || checkBox2.Checked == true || checkBox3.Checked == true || checkBox4.Checked == true)
  849.                 button3.Text = "All off";
  850.             else
  851.                 button3.Text = "All on";
  852.         }
  853.  
  854.         private void checkBox3_CheckedChanged(object sender, EventArgs e)
  855.         {
  856.             if (checkBox1.Checked == true || checkBox2.Checked == true || checkBox3.Checked == true || checkBox4.Checked == true)
  857.                 button3.Text = "All off";
  858.             else
  859.                 button3.Text = "All on";
  860.         }
  861.  
  862.         private void button2_Click(object sender, EventArgs e)
  863.         {
  864.             if (PowersupplyIsOn == TRUE)
  865.             {
  866.                 if (MessageBox.Show("Are you sure you want to shut down the powersupply?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  867.                     EnablePowerSupply = TRUE;
  868.                 button2.Text = "Start Powersupply";
  869.             }
  870.             else
  871.             {
  872.                 if (MessageBox.Show("Are you sure you want to start up the powersupply?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  873.                     EnablePowerSupply = TRUE;
  874.                 button2.Text = "Stop Powersupply";
  875.             }
  876.         }
  877.  
  878.         private void groupBox2_EnabledChanged(object sender, EventArgs e)
  879.         {
  880.             checkBox1.Checked = false;
  881.             checkBox2.Checked = false;
  882.             checkBox3.Checked = false;
  883.             checkBox4.Checked = false;
  884.         }
  885.  
  886.         private void button1_Click_1(object sender, EventArgs e)
  887.         {
  888.             groupBox5.Visible = false;
  889.             button1.Visible = false;
  890.             button4.Visible = true;
  891.             button5.Visible = true;
  892.             groupBox3.Visible = true;
  893.             groupBox4.Visible = true;
  894.         }
  895.  
  896.         private void button4_Click(object sender, EventArgs e)
  897.         {
  898.             groupBox5.Visible = true;
  899.             button1.Visible = true;
  900.             button4.Visible = false;
  901.             button5.Visible = false;
  902.             groupBox3.Visible = false;
  903.             groupBox4.Visible = false;
  904.         }
  905.  
  906.         private void radioButton1_CheckedChanged(object sender, EventArgs e)
  907.         {
  908.             if (radioButton1.Checked == true)
  909.                 bMin12vEnabled = TRUE;
  910.             else
  911.                 bMin12vEnabled = FALSE;
  912.         }
  913.  
  914.         private void radioButton2_CheckedChanged(object sender, EventArgs e)
  915.         {
  916.             if (radioButton2.Checked == true)
  917.                 bMin12vEnabled = FALSE;
  918.             else
  919.                 bMin12vEnabled = TRUE;
  920.         }
  921.  
  922.         private void radioButton3_CheckedChanged(object sender, EventArgs e)
  923.         {
  924.             if (radioButton3.Checked == true)
  925.                 b8HourEnabled = FALSE;
  926.             else
  927.                 b8HourEnabled = TRUE;
  928.         }
  929.  
  930.         private void radioButton4_CheckedChanged(object sender, EventArgs e)
  931.         {
  932.             if (radioButton4.Checked == true)
  933.                 b8HourEnabled = TRUE;
  934.             else
  935.                 b8HourEnabled = FALSE;
  936.         }
  937.  
  938.         private void button5_Click(object sender, EventArgs e)
  939.         {
  940.             if (SoakTestBussy == TRUE)
  941.             {
  942.                 if (MessageBox.Show("Are you sure you want to stop the current soaktest?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
  943.                 {
  944.                     SendBlock(RequestToStopSoakTest);
  945.                 }
  946.             }
  947.             else
  948.             {
  949.                 SendBlock(RequestToStartSoakTest);
  950.                
  951.             }
  952.         }
  953.  
  954.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  955.         {
  956.             string target = "http://www.eurocoingaming.nl";
  957.             //Use no more than one assignment when you test this code.
  958.             //string target = "ftp://ftp.microsoft.com";
  959.             //string target = "C:\\Program Files\\Microsoft Visual Studio\\INSTALL.HTM";
  960.  
  961.             try
  962.             {
  963.                 System.Diagnostics.Process.Start(target);
  964.             }
  965.             catch
  966.                 (
  967.                  System.ComponentModel.Win32Exception noBrowser)
  968.             {
  969.                 if (noBrowser.ErrorCode == -2147467259)
  970.                     MessageBox.Show(noBrowser.Message);
  971.             }
  972.             catch (System.Exception other)
  973.             {
  974.                 MessageBox.Show(other.Message);
  975.             }
  976.         }
  977.  
  978.         private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  979.         {
  980.             System.Diagnostics.Process.Start("mailto:[email protected]");
  981.         }
  982.  
  983.         private void button6_Click(object sender, EventArgs e)
  984.         {
  985.             if (MessageBox.Show("Are you sure you want to reset all values?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  986.                 SendBlock(RequestToResetValues);
  987.         }
  988.     }
  989. }
Advertisement
Add Comment
Please, Sign In to add comment