Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Controlling a Real Measurement 3153 Hioki Hipot Device by Using Serial and Parallel Ports in C#

3.43/5 (3 votes)
2 Feb 2008GPL37 min read 1   564  
Controlling the 3153 Hioki Hipot device by using serial and parallel ports in C#.

Introduction

Serial port RS-232 is still used in industrial applications but has recently disappeared from most notebooks. There is a SerialPort class in the .NET Framework to handle all serial port communication, and you can use this class in your code simply by adding "using System.IO.Ports;". Also, for parallel port access, you must use Inpout32.dll from the following URL: http://logix4u.net/Legacy_Ports/Parallel_Port/Inpout32.dll_for_Windows_98/2000/NT/XP.html.

In this project, I use a real device to communicate by using the Hioki 3153 Hipot tester via serial port. Indication of a fail condition will be shown by the LED as told in this article: I/O Ports Uncensored - 1 - Controlling LEDs (Light Emitting Diodes) with Parallel Port by Levent Saltuklaroglu.

At the end of this article, you can get information about the Hipot device. All the information was taken from the manual of the device. Thanks to Behlul Savaskan for typing these parts. I must also mention that Hioki may not have a customer support department. I asked some questions via e-mail, but I did not get any reply from them. Finally, I also added a setup project for copying inpout32.dll, which is needed for the parallel port to the C:\Windows\System32 folder.

The Namespace

I use two different namespaces: hipotinterface which includes the main program and the serial port code, and ParallelPort that handles the parallel port communication.

The Main Code

Here is the code for the serial port and the main window. When the main form is loaded with the FrmMain_Load event, all the parallel port pins are set to low and the device is set with the settings indicated in the HIOKI 3153 Automatic Insulation Resistance Withstanding Tester Settings section of this document. According to the user manual, after every command, a delimiter (Carriage Return and Line Feed) must be used. I used the Writeline method of the SerialPort class. Settings like "conf:with:kind AC50" were taken from the user manual. I use the FrmMain_Load event to force the device to be started with my initial settings when parameters are changed by the operator.

In the CommWith3153At9600 function, the Hipot device communicates with our PC. You can read the 3153 To PC Interface section for the details of the sent messages and queries.

The RunTest() function handles Red/Green label transformation, closing serial and parallel ports according to the result of the CommWith3153At9600 function.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.IO.Ports;
using System.Threading;

namespace hipotinterface
{
    public partial class FrmMain : Form
    {
       //RS232 variables
       //Create a new SerialPort object with Hioki 3153 HiTester settings;
       public static SerialPort _serialPort = 
         new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
       public static string strTestResult = null;
       static bool _ESR0CommandSent = false;
       const string CRLF = "\r\n"; //Delimiter for 3153  

       private void FrmMain_Load(object sender, EventArgs e)
       {
            //change all parallel port data lines to low state.
            ParallelPort.HipotParPort.HipotParPort1(false);

            try
            {
                //Open Port
                _serialPort.Open();
                _serialPort.ReadTimeout = 5000;

                //Send any Character To Pass 3153 to Remote Mode.
                _serialPort.WriteLine(CRLF);

                //Set Withstand Test-voltage type to :AC 50 Hz
                _serialPort.WriteLine("conf:with:kind AC50");
                _serialPort.WriteLine(CRLF);

                //Set Withstand Test Voltage to 3.00kV
                _serialPort.WriteLine("conf:with:volt 3.00");
                _serialPort.WriteLine(CRLF);

                //Set Withstand Cutoff current upper limit to 5.5 mA
                _serialPort.WriteLine("with:clow ON");
                _serialPort.WriteLine(CRLF);
                _serialPort.WriteLine("conf:with:cupp 5.5");
                _serialPort.WriteLine(CRLF);

                //Set Withstand Cutoff current lower limit to 0.7 mA
                _serialPort.WriteLine("conf:with:clow 0.7");
                _serialPort.WriteLine(CRLF);

                //Set Withstand Ramp Timer(Test Time) to :3.0 s.
                _serialPort.WriteLine("with:tim ON");
                _serialPort.WriteLine(CRLF);
                _serialPort.WriteLine("conf:with:tim 3.0");
                _serialPort.WriteLine(CRLF);

                //Set Withstand Ramp-up Timer to OFF
                _serialPort.WriteLine("with:utim OFF");
                _serialPort.WriteLine(CRLF);

                //Set Withstand Ramp-down Timer to OFF
                _serialPort.WriteLine("with:dtim OFF");
                _serialPort.WriteLine(CRLF);

                ////

                //Set Insulation Test-voltage type to :DC
                _serialPort.WriteLine("conf:ins:kind AC50");
                _serialPort.WriteLine(CRLF);

                //Set Insulation Test Voltage to 500V
                _serialPort.WriteLine("conf:ins:volt 500");
                _serialPort.WriteLine(CRLF);

                //Set Insulation Resistance upper limit to :OFF.
                _serialPort.WriteLine("ins:rupp OFF");
                _serialPort.WriteLine(CRLF);

                //Set Insulation Resistance lower limit to : 4.0 Mohm
                _serialPort.WriteLine("conf:ins:rlow 4.0");
                _serialPort.WriteLine(CRLF);

                //Set Insulation Test Time to :1.0 s.
                _serialPort.WriteLine("ins:tim ON");
                _serialPort.WriteLine(CRLF);
                _serialPort.WriteLine("conf:ins:tim 1.0");
                _serialPort.WriteLine(CRLF);

                //Set Insulation delay time to OFF.
                _serialPort.WriteLine("ins:del OFF");
                _serialPort.WriteLine(CRLF);

                ////

                //Set Test Mode: Withstand voltage mode -> insulation resistance mode
                _serialPort.WriteLine(":mode AWI");
                _serialPort.WriteLine(CRLF);

                _serialPort.Close();

            }
            catch (Exception)
            {
                MessageBox.Show("Initialization Error.");
                Application.Restart();
            }
       }

       public FrmMain()
       {
            InitializeComponent();
            AssemblyInfo ainfo = new AssemblyInfo();
            this.Text = "Tuse HIPOT Operator Interface " + ainfo.Version;
       }

       public void CommWith3153At9600()
       {
            strTestResult = null;
            try
            {
                //Open Port
                _serialPort.Open();
                _serialPort.ReadTimeout = 5000;

                //Send Clear command in case of unexpected behaviours.
                _serialPort.WriteLine("*cls");
                _serialPort.WriteLine(CRLF);

                //Send Start Command&Read
                _serialPort.WriteLine("start");
                _serialPort.WriteLine(CRLF);

                //Read Status
                _ESR0CommandSent = true;

                while (_ESR0CommandSent == true)
                {
                    char ch = ' ';

                    while ((ch == '0') || (ch == ' '))
                    {
                        _serialPort.WriteLine("ESR0?");
                        _serialPort.WriteLine(CRLF);
                        //character, carriage return is ascii:13 and Line feed is ascii 10
                        string message = _serialPort.ReadLine(); 
                        if (message.StartsWith("9"))
                        //Test completed and within limits of comparator (PASS) 
                        {
                            ch = '9';
                            strTestResult = "Passed";
                            _ESR0CommandSent = false;
                        }
                        else if (message.StartsWith("10"))
                        //Test completed and above upper of comparator (FAIL) 
                        {
                            ch = 'A';
                            strTestResult = "Above Failed";
                            _ESR0CommandSent = false;
                        }
                        else if (message.StartsWith("12"))
                        //Test completed and below lower limit of comparator (FAIL)
                        {
                            ch = 'C';
                            strTestResult = "Lower Failed";
                            _ESR0CommandSent = false;
                        }
                    } // while ((ch == '0') || (ch == ' '))
                }//while                
                
                if (strTestResult != "Passed")
                {
                    _ESR0CommandSent = false;
                } //if  
               
                //Print Result to The Status Bar
                statusBar1.Text = "Result: " + strTestResult;

            }
            catch (Exception)
            {
                MessageBox.Show("Cannot communicate with the device");
                ParallelPort.HipotParPort.HipotParPort1(false);
                System.Environment.Exit(-1);
            }

       }  //CommWith3153At9600

       public void RunTest()
       {
            button_Run.Enabled = false;
            lblDut1.BackColor = SystemColors.Window;
            lblDut1.Update();
            statusBar1.Text = "";
            lblDut1.Text = "";
            strTestResult = null;
            
            CommWith3153At9600();

            if (strTestResult == "Passed") //
            {
                lblDut1.BackColor = Color.GreenYellow;
            } //if
            else
            {
                lblDut1.BackColor = Color.Red;
                lblDut1.Text = "Failed";

                //make the bulb on to warn the operator that test has failed.
                ParallelPort.HipotParPort.HipotParPort1(true);

                statusBar1.Text = "HIPOT TEST FAILED";
                MessageBox.Show("HIPOT TEST FAILED!!!", 
                  "Hipot Test Failed", 
                  MessageBoxButtons.OK, MessageBoxIcon.Error);

                //send stop command to the hipot and close serial connection
                _serialPort.WriteLine("stop");
                _serialPort.WriteLine(CRLF);
                _serialPort.WriteLine("*cls");
                _serialPort.WriteLine(CRLF);
                _serialPort.Close();

                //make the bulb off
                ParallelPort.HipotParPort.HipotParPort1(false);

                goto result1;
            } //else 
                       
            _serialPort.WriteLine("*cls");
            _serialPort.WriteLine(CRLF);
            _serialPort.Close();
            
            strTestResult = null;            
                
            result1:
                button_Run.Enabled = true;
                button_Run.Text = "RUN";
                button_Run.Update();
            }  //public void RunTest()

        private void button_Run_Click_1(object sender, EventArgs e)
        {
            RunTest();
        }    //button_Run_Click
    }// public partial class FrmMain : Form
}

The Parallel Port Code

The code in the Parallelport.cs file was modified from I/O Ports Uncensored - 1 - Controlling LEDs (Light Emitting Diodes) with Parallel Port by Levent Saltuklaroglu; if you need line by line information, you should take a look at the article. The code assumes that LPT1 is the default parallel port and lights only the first pin (D0) when the test fails.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace ParallelPort
{      
    public partial class HipotParPort : Form
    {
        //Default is LPT1. If you want to use LPT2 choose 632
        public static int nAddress = 888;
        //decimal 1 -> only D0 is high.
        public static Int32 onValue = 1;
        //decimal 0 -> all data pins of the ParallelPort is low.
        public static Int32 offValue = 0;
        // put some time between ON/OFF states in case of burning...
        public static int nDelay = 1000;

        public class PortAccess 
        {
            //http://logix4u.net/Legacy_Ports/Parallel_Port/
            //    Inpout32.dll_for_Windows_98/2000/NT/XP.html
            //Check the upper url to learn how the inpout32.dll works
            //Note that inpout32.dll must
            //     be put to the C:\Windows\System32 folder.
            [DllImport("inpout32.dll", EntryPoint="Out32")]
            public static extern void Output(int nAddress, int nDecimalValue);
        }  //public class PortAccess

        public static void HipotParPort1(Boolean bBulbOn)
        {            
            Application.DoEvents();

            if (bBulbOn)
                PortAccess.Output(nAddress, onValue);
            else
                PortAccess.Output(nAddress, offValue);
            
            Thread.Sleep(nDelay);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // HipotParPort
            // 
            this.ClientSize = new System.Drawing.Size(104, 47);
            this.Name = "HipotParPort";
            this.ResumeLayout(false);
        } //public static void HipotParPort1(Boolean bBulbOn)
    }
}

How to Use the HIPOT Program

The Hipot Interface program is used for controlling HIOKI HIPOT test devices.

Attention: You must remove all external I/O cards, start/stop buttons etc., if there are any. Because, they will not work in remote mode. Besides that, the top cover must be closed for operator safety. I asked Hioki about the usage of this top cover in remote mode; unfortunately, I did not get any response. If you run the program, you will see the following screen:

Image 1

You must click the RUN button to start testing. If the DUT (Device Under Test) passes the testing, there will be a green bar on the form. Otherwise, you will see a red bar to indicate that the test has failed and also a pop-up message.

HIOKI 3153 Automatic Insulation Resistance Withstanding Tester Settings

  1. Default connection settings of the 3153 Hipot tester:
    1. Serial Port: "COM1"
    2. Bps: 9600
    3. Parity:1
    4. Databits:8
    5. StopBits:1
  2. Program arrange mode of the Hipot tester as in the following initial settings:
    • Send any character to pass 3153 to Remote mode
    • Set Withstand Test-voltage type to AC 50 Hz
    • Set Withstand Test Voltage to 3.00kV
    • Set Withstand Cutoff Current Upper Limit to 5.5 mA
    • Set Withstand Cutoff Current Lower Limit to 0.7 mA
    • Set Withstand Ramp Timer (Test Time) to 3.0 s
    • Set Withstand Ramp-up Timer to OFF
    • Set Withstand Ramp-down Timer to OFF
    • Set Insulation Test-voltage Type to DC
    • Set Insulation Test Voltage to 500V
    • Set Insulation Resistance Upper Limit to OFF
    • Set Insulation Resistance Lower Limit to 4.0 Mohm
    • Set Insulation Test Time to 1.0 s
    • Set Insulation Delay Time to OFF
    • Set Test Mode: Withstand Voltage mode -> Insulation Resistance mode

    Note that these settings were obtained by trying the device manually.

  3. If the result of the test is fail, then the D0 pin (#2) of the parallel port gets high. But you can also set other pins to be lit.
  4. If the result of the test is fail and to make the tester hear the beep sound, then stop command will not be sent to the Hipot tester. To stop it, the operator has to click the OK button.

3153 To PC Interface

The 3153 includes two 8 bit event registers. It is possible to determine the status of the unit by reading these registers.

The event registers are cleared in these situations:

  1. When a "*cls" command is executed
  2. When the event register is executed
  3. When the unit is powered on
    1. Standard Event Status Register (SESR) bit assignments
    2. Bit 7

      PON

      Power On flag

      When the power is turned on or off recovery from a power cut, this bit is set to 1

      Bit 6Unused
      Bit 5 CME

      Command error

      When a command which has been received contains a syntactic or semantic error, this bit is set to 1

      • The command is not supported by the 3153
      • There is a mistake in a program header
      • The number of data parameters is wrong
      • The format of parameters is wrong
      Bit 4 EXE

      Execution error

      When for some reason a command which has been received cannot be executed, this bit is set to 1

      • The designated data value is outside the set range
      • The designated data value is not acceptable
      Bit 3 DDE

      Device dependent error

      When a command cannot be executed due to some reason other than a command error, a query error, or an execution error, this bit is set to 1

      Bit 2 QYE

      Query error

      This bit is set to 1 when a query error is detected by the output queue control

      • When the data overflows the output queue
      • When data in the output queue has been lost
      Bit 1Unused
      Bit 0Unused
    3. Event Status Register 0 (ESR0) bit assignments
    4. Bit 7Unused
      Bit 6 Unused
      Bit 5 Unused
      Bit 4 Unused

      Bit 3

      EOM
      Test completed

      Bit 2

      LFAIL

      Below lower limit of comparator

      Bit 1

      UFAIL

      Above upper limit of comparator

      Bit 0

      PASS

      Within limits of comparator
      1. ESR0 = xxxx1100 = 0x0C = .12 ----> Test completed and below lower limit of comparator (FAIL)
      2. ESR0 = xxxx1010 = 0x0A = .10 ----> Test completed and above upper limit of comparator (FAIL)
      3. ESR0 = xxxx1001 = 0x09 = .9 -----> Test completed and within limits of comparator (PASS)
    5. RS 232 Command Reference
      • CLS -----> Clears the status byte registers and the event registers
      • ESR? -----> Returns the contents of the Standard Event Status Register (SESR) as a numerical value in NR1 format between 0 to 255 and then clears the SESR register
      • ESR0? -----> Returns the value of the event status register 0 (ESR0) as a numerical value in NR1 format between 0 to 255 and then clears the ESR0 register

Final Note

I hope you have found my project very useful and can implement other devices. I try to avoid the classical way of putting all the available serial port options into a combo box. By examining the 3153 To PC interface section and my code, you can understand how things can be implemented. Also, please excuse me for my English.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)