Home » Software, Coding & Hosting » NewPast Software » Machine Controller free download with sorce code

Machine Controller free download with sorce code

    Machine Controller allows using the serial or parallel port to control a machine, freeware download with source code

    System Requirements for Machine Controller

    • Windows XP, 7, Vista, 8, 8.1, 10
    • .NET Framework v4

    About This Solution

    • This solution contains two windows applications, one for serial port and the other for parallel port
    • I use VB to develop a very simple RS232 Controller application that makes use of serial port easy.
    • It supports storing your command in a text file for later use
    • It supports file sending to the serial port
    • I build a separate application for a parallel port controller
    • This solution also contains a Most Recently Files menu strip library that used in the RS232 controller to make track to last files used and we could use this library in any Windows application.

    Using Machine Controller Application

    • Download and install the application
    • Connect your device to the computer with the suitable cable
    • Switch on your machine
    • Run the application and use it to send and receive data for your machine
    • Store your command or the machine output for later use

    Reasons for Using Machine Controller:

    If you need to extend your machine usage, you may need to control it via Machine Controller. This can be in these categories: time-based controlling, event, and communication.

    Time-Based Controlling

    Time-based actions include actions based on a set amount of time passing or inaction in a specific time.

    Event-Based Controlling

    If you want the machine to do something in a specific condition, or when something else has happened.

    Communication Controlling

    The Machine Controller application will help to send commands or data to the machine to change its start-up or run status or receive data from the machine to process the data by other applications.

    What is Serial Port

    The serial port is a serial communication physical interface through which information transfers in or out one bit at a time (in contrast to a parallel port). Throughout most of the history of personal computers, I transferred data through serial ports to devices such as modems, terminals, and various peripherals.

    What is RS-232

    Recommended Standard-232 is a standard for serial transmission between computers and peripheral devices. Using a 25-pin DB-25 or 9-pin DB-9 connector, we can extend its normal cable limitation of 50 feet to several hundred feet with high-quality cable. A good deal of equipment and machines have their built-in RS232 port. Making use of this port is very simple. All programming languages sport this port.

    About Parallel Port

    The parallel port, printer port, or LPT port is another simple way of communicating with external devices. I provide an application that can control parallel port connecting devices. Direct port access in Windows NT versions is not allowed, so we need to have a driver in order to communicate with ports directly. Therefore, we are using a freeware driver and library. This library supports 32 bit and 64-bit versions of Windows for both x86 and x64 CPU.

    Communicating with the Parallel Port

    The parallel port usually comes as a 25-pin female port and it is commonly used to connect printers to a computer. It could control the relay box. Many companies provide a relay box for the parallel ports that could PLC to control machines. The parallel port contains three types of registers: data, status, and control registers. The status register is a read-only register to get port status.

    Points of Interest

    About Most Recently Files Library

    • We first build the MostRecentlyFilesItem class that inherits ToolStripMenuItem
    • We add a property FileName
    • Once you set the FileName property, the text of this menu item will be changed to the number of the file and the file name
      //C# Code
      public sealed override string Text{
                  get {
                      try{
                          if (this.Enabled){
                              var number = this.Index + 1;
                              var sEntryName = ShortenPathname(this.FileName);
                              if (number < 10){
                                  return "&" + number + "  " + sEntryName;
                              }else if (number == 10){
                                  return "1&0" + "  " + sEntryName;
                              }
                              else
                              {
                                  return number + "  " + sEntryName;
                              }
                          }
                          else
                          {
                              return DefaultText;
                          }
                      }
                      catch (Exception ex)
                      {
                          if (_ErrMsg.ErrMsg(ex))
                          {
                              System.Diagnostics.Debugger.Break();
                          }
                          return null;
                      }
                  }
                  set
                  {
                      //
                  }
              }

      'VB Code
      Public NotOverridable Overrides Property Text As String
          Get
              Try
                  If Me.Enabled Then
                      Dim number = Me.Index + 1
                      Dim sEntryName = ShortenPathname(Me.FileName)
                      If number < 10 Then
                          Return "&" & number & "  " & sEntryName
                      ElseIf number = 10 Then
                          Return "1&0" & "  " & sEntryName
                      Else
                          Return number & "  " & sEntryName
                      End If
                  Else
                      Return DefaultText
                  End If
              Catch ex As Exception
                  If ErrMsg(ex) Then Diagnostics.Debugger.Break()
                  Return Nothing
              End Try
          End Get
          Set(value As String)
              '
          End Set
      End Property
      

      Then we build the main menu item manager that will control the menu items:

      //C# Code
      public class MostRecentlyFiles : MostRecentlyFilesItem, IList<string>

      'Vb Code
      Public Class MostRecentlyFiles
          Inherits MostRecentlyFilesItem
          Implements IList(Of String)
      
      • The previous class inherited to act as one of the file menu items and makes the control of it form the denser
      • ItImplements IList(Of String) to act as a list of files, just add the file to it and we will view it correctly in the menu items
      • To set all items, you could call the IList.AddRange method
      • Files property added to get or set a comma-separated list of files in the menu
      • If any file does not exist, the application will ignore it.
        //C# code
        public void Add(string sFileName)
                {
                    try
                    {
                        if (string.IsNullOrEmpty(sFileName) || !System.IO.File.Exists(sFileName))
                        {
                            return;
                        }
                        this.Enabled = true;
        
                        if (Count > 0)
                        {
                            int Index = _EntriesFiles.IndexOf(sFileName);
                            if (Index >= 0)
                            {
                                if (Index == 0)
                                {
                                    return;
                                }
                                this._EntriesFiles.Remove(sFileName);
                                this._EntriesFiles.Insert(0, sFileName);
                                for (int i = 0; i < this._EntriesFiles.Count; i++)
                                {
                                    this._EntriesItems[i].FileName = this._EntriesFiles[i];
                                }
                                return;
                            }
                        }
                        while (!(this.Count <= _EntriesMaxCount))
                        {
                            this.RemoveAt(this.Count - 1);
                        }
                        MostRecentlyFilesItem menuItem = null;
                        if (this.Count == 0 && !HasDropDownItems)
                        {
                            this.Initialize(this, sFileName, OnClick);
                            menuItem = this;
                        }
                        else
                        {
                            menuItem = new MostRecentlyFilesItem(this, sFileName, OnClick);
                            MenuItems.Insert(StartIndex, menuItem);
                        }
                        this._EntriesFiles.Insert(0, sFileName);
                        this._EntriesItems.Insert(0, menuItem);
                    }
                    catch (Exception ex)
                    {
                        if (_ErrMsg.ErrMsg(ex))
                        {
                            System.Diagnostics.Debugger.Break();
                        }
                    }
                }

        'VB code
        Public Sub Add(ByVal sFileName As String) Implements IList(Of String).Add
            Try
                If String.IsNullOrEmpty(sFileName) OrElse Not IO.File.Exists(sFileName) Then
                    Exit Sub
                End If
                Me.Enabled = True
        
                If Count > 0 Then
                    Dim Index As Integer = _EntriesFiles.IndexOf(sFileName)
                    If Index >= 0 Then
                        If Index = 0 Then Exit Sub
                        Me._EntriesFiles.Remove(sFileName)
                        Me._EntriesFiles.Insert(0, sFileName)
                        For i As Integer = 0 To Me._EntriesFiles.Count - 1
                            Me._EntriesItems(i).FileName = Me._EntriesFiles(i)
                        Next
                        Exit Sub
                    End If
                End If
                Do Until Me.Count <= _EntriesMaxCount
                    Me.RemoveAt(Me.Count - 1)
                Loop
                Dim menuItem As MostRecentlyFilesItem
                If Me.Count = 0 AndAlso Not HasDropDownItems Then
                    Me.Initialize(Me, sFileName, AddressOf OnClick)
                    menuItem = Me
                Else
                    menuItem = New MostRecentlyFilesItem(Me, sFileName, AddressOf OnClick)
                    MenuItems.Insert(StartIndex, menuItem)
                End If
                Me._EntriesFiles.Insert(0, sFileName)
                Me._EntriesItems.Insert(0, menuItem)
            Catch ex As Exception
                If ErrMsg(ex) Then Diagnostics.Debugger.Break()
            End Try
        End Sub
        
        Public Property Files As String
            Get
                'NEH
                If Count = 0 Then
                    Return ""
                Else
                    Return Join(_EntriesFiles.ToArray, ",")
                End If
            End Get
            Set(value As String)
            Try
                Me.Clear()
                If String.IsNullOrEmpty(value) Then Exit Property
                Dim vItems = Split(value, ",")
                If vItems Is Nothing OrElse vItems.Length = 0 Then Exit Property
                AddRange(vItems)
            Catch ex As Exception
                If ErrMsg(ex) Then Diagnostics.Debugger.Break()
            End Try
            End Set
        End Property
        

        About RS232Controller

        • The built-in serial port control extended to make writing and reading data easier
        • DataReceived monitor data and fire the event if find any new data,
        • DataReaded Read the data
          //C# code
          public class SerialPortEx : SerialPort
              {
                  public void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
                  {
                      try
                      {
                          //Me.BytesToRead Not Me.ReadBufferSize
                          byte[] BytesReaded = new byte[this.BytesToRead];
                          int BytesReadedCount = this.Read(BytesReaded, 0, this.BytesToRead);
                          if (BytesReadedCount == 0)
                          {
                              return;
                          }
          
                          string sData = this.Encoding.GetString(BytesReaded, 0, BytesReadedCount);
                          OnDataReaded(sData, false);
          
                      }
                      catch (Exception ex)
                      {
                          if (_ErrMsg.ErrMsg(ex))
                          {
                              System.Diagnostics.Debugger.Break();
                          }
                      }
                  }
          
                  public event EventHandler<LinkClickedEventArgs> DataReaded;

          'VB code
          Dim vProtIO As New PortIO
          'Read
          Dim Value = vProtIO.PortData
          'or
          Dim DataRegister0 = vProtIO.DataRegister(PortIO.BitRegister.Bit0)
          'Write
          vProtIO.PortData = Value
          'or
          vProtIO.SetDataRegister(PortIO.BitRegister.Bit0, DataRegister0)
          

          About LPTController

          • I support several drivers to read and write data.
          • The application will check for the driver to use.
          • If the application finds no driver or no parallel, it will use a virtual driver.
          • PortIO class is the main class to read and write to ports and we could use it as follows:
            //C# code
            PortIO vProtIO = new PortIO();
            //Read
            var Value = vProtIO.PortData;
            //or
            var DataRegister0 = vProtIO.DataRegister(PortIO.BitRegister.Bit0);
            //Write
            vProtIO.PortData = Value;
            //or
            vProtIO.SetDataRegister(PortIO.BitRegister.Bit0, DataRegister0);

            'VB code
            Private Sub GetParallelPortAddress()
                Try
                    _PortsAddresses = New List(Of UInt16)
                    For nPortNo As Integer = 0 To 9
                        Dim pPortAddressLocation = New IntPtr(&H408 + 2 * nPortNo)
                        Dim nPortAddress = _PortIO.InpPhys(pPortAddressLocation).ToInt32
                        If nPortAddress > 0 AndAlso nPortAddress < &HFFF Then
                            _PortsAddresses.Add(CType(nPortAddress, UInt16))
                        Else
                            Exit For
                        End If
                    Next
                Catch ex As Exception
                    ErrMsg(ex)
                End Try
            End Sub
            

            PortIO Class Members

            I use one class to access different libraries and this class has the following members:

            Properties
            • Port: Sets or get the port to read and write to
            • PortAddress: Gets the address of the selected port
            • PortData: Gets or sets the value of all data registers in one-byte value
            • PortControl: Gets or sets the value of all control registers in one-byte value
            • PortStatus: Gets the value of all status registers in one-byte value
            • IsTestSigning: Checks if the test signing for the drives is switching on or off
            Functions and Methods
            • PortsCount: Gets the count of the parallel ports in the PC
            • Inp: Gets the value of all registers in one-byte value from a specific port address
            • Out: Sets the value of all registers in one-byte value for a specific port address
            • InpPhys: Gets a value from physical memory (not supported by winring0)
            • DataRegister: Gets the Boolean value of a specific data register
            • SetDataRegister: Sets the Boolean value of a specific data register
            • StatusRegister: Gets the Boolean value of a specific status register
            • ControlRegister: Gets the Boolean value of a specific control register
            • SetControlRegister: Sets the Boolean value of a specific control register
            • IsAdmin: Checks if the application runs as with the administrator’s rights
            • RunAsAdmin: Runs specific application as administrator
            • RunAsAdminRestart: Restarts the current application as administrator
            Events
            • GotError: Occurs when an error raised in dealing with the library

            Addressing the Parallel Port and Registers

            Port addressing controls a great deal in port programming, as it is the door that enables programs to connect to the external circuit or device. Therefore, I would like to explain all the available port addresses.

            In normal PCs, a parallel port address can vary depending on the BIOS settings and some other issues. The most common address is &h378, however, you can get all parallel port addresses from the BIOS by checking the data stored in &H408 and the following function will do this: This function is a built-in PortIO class:

            //C# code
            private void GetParallelPortAddress()
                {
                    try
                    {
                        _PortsAddresses = new List<UInt16>();
                        for (int nPortNo = 0; nPortNo <= 9; nPortNo++)
                        {
                            var pPortAddressLocation = new IntPtr(0x408 + 2 * nPortNo);
                            var nPortAddress = _PortIO.InpPhys(pPortAddressLocation).ToInt32();
                            if (nPortAddress > 0 && nPortAddress < 0xFFF)
                            {
                                _PortsAddresses.Add(Convert.ToUInt16(nPortAddress));
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _ErrMsg.ErrMsg(ex);
                    }
                }

            'VB code
            Private Sub GetParallelPortAddress()
                Try
                    _PortsAddresses = New List(Of UInt16)
                    For nPortNo As Integer = 0 To 9
                        Dim pPortAddressLocation = New IntPtr(&H408 + 2 * nPortNo)
                        Dim nPortAddress = _PortIO.InpPhys(pPortAddressLocation).ToInt32
                        If nPortAddress > 0 AndAlso nPortAddress < &HFFF Then
                            _PortsAddresses.Add(CType(nPortAddress, UInt16))
                        Else
                            Exit For
                        End If
                    Next
                Catch ex As Exception
                    ErrMsg(ex)
                End Try
            End Sub

            Device Needed to Use LPT Controller

            To use this application, get a Parallel Port Relay Box such as KIT 74 V2CK1601quasar.

            To build a relay box yourself, remember that you could not drain much current for the bin as this would burn your parallel port or your entire motherboard. So use them at your own risk…!

            Useful Links

            License

            This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

            End