Click here to Skip to main content
Click here to Skip to main content
Go to top

Machine Controller

, 29 May 2014
Rate this:
Please Sign up or sign in to vote.
Using serial or paraller port to control a machine.

Downloads

Introduction

About This Solution

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

Using This 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 out put 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 in action in a specific time.

Event Based Controlling

If you what the machine to do some thing on specific condition, or when some thing else is happened.

Communication Controlling

It will help in sending commands or data to the machine to change its start up or run status or receive data from the machine to e proceeded by another applications.

What is serial port

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, data was transferred 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, its normal cable limitation of 50 feet can be extended to several hundred feet with high-quality cable. Many 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

Parallel port, printer port, or LPT port is another easy way for communicating with external devices. I provide an application that can be used to control parallel port connecting devices. The 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 be use to control relays box. Many companies provide a relays box for parallel port that could be used as PLC to controls machines. The parallel port contains three types of register: data, status and control registers. status register is a read only registers 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
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 manger that will control the menu items:

Public Class MostRecentlyFiles
    Inherits MostRecentlyFilesItem
    Implements IList(Of String)
  • The previous class is inherited to act as one of the file menu items and makes the control of it form the denser
  • It is Implements IList(Of String) to act as a list of files, just add file to it and it will be viewed correctly in the menu items
  • To set all items you could call the IList.AddRange method
  • Files property is added to get or set a comma separated list of files in the menu
  • If any file does not exist it will be ignored.
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 RS232Controler:

  • The built-in SerialPort control is extended to make writing and reading data more easy
  • DataReceived is monitored and when any new data is found a DataReaded event is fired
Public Class SerialPortEx
    Inherits SerialPort

    Public Sub OnDataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles Me.DataReceived
        Try
            'Me.BytesToRead Not Me.ReadBufferSize
            Dim BytesReaded(Me.BytesToRead - 1) As Byte
            Dim BytesReadedCount As Integer = Me.Read(BytesReaded, 0, Me.BytesToRead)
            If BytesReadedCount = 0 Then
                Exit Sub
            End If

            Dim sData As String = Me.Encoding.GetString(BytesReaded, 0, BytesReadedCount)
            OnDataReaded(sData, False)

        Catch ex As Exception
            If ErrMsg(ex) Then Diagnostics.Debugger.Break()
        End Try
    End Sub

    Public Event DataReaded As EventHandler(Of LinkClickedEventArgs)

About LPTControler

  • Several drivers are supported to read and write data.
  • The application will check for the available driver to use.
  • If no driver is found or no parallel port is found a virtual driver is used.
  • PortIO class is the main class to read and write to ports and it could be used as follows:
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)

PortIO Class Members:

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

Properties:

  • Port: Set or get the port to read and write to
  • PortAddress: get the address of the selected port
  • PortData: get or set the value of all data registers in one byte value
  • PortControl: get or set the value of all control registers in one byte value
  • PortStatus: get the value of all status registers in one byte value
  • IsTestSigning: check if the test signing for the drives is switching on or off.

Functions and methods:

  • PortsCount: get the count of the available parallel ports in the PC
  • Inp: get the value of all registers in one byte value from a specific port address
  • Out: set the value of all registers in one byte value for a specific port address
  • InpPhys: get a value from physical memory (not supported by winring0)
  • DataRegister: gets the Boolean value of a specific data register.
  • SetDataRegister: sets the 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 the Boolean value of a specific control register.
  • IsAdmin: checks if the application is run as administrator
  • RunAsAdmin: runs specific application as administrator
  • RunAsAdminRestart: restarts the current application as administrator

Events

  • GotError: Occurs when an error is 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 gel all parallel port addresses form the BIOS by checking the data stored in &H408 and the following function will do this: tThis function is a built-in PortIO class:

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 you should get a Parallel Port Relay Box such as KIT 74 V2, CK1601, quasar.

To build a relay box yourselves 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:

Follow

License

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

Share

About the Author

NewPast

Lebanon Lebanon
Mini Software Developer, VBA Macro Writer & Web developer
VB, ASP.Net, php & MySQL
------------------------------------------
Look at your eyes:
There is a developer behind every piece of code!
 
Eyes are too complex what about them!

Comments and Discussions

 
QuestionMachine Controller ... Love Machine control Pinmembergrralph130-May-14 2:38 
AnswerRe: Machine Controller ... Love Machine control PingroupNewPast19-Jun-14 5:52 
QuestionNo serial Pinmembernullpointer3-Feb-14 16:29 
AnswerRe: No serial PingroupNewPast6-Feb-14 6:09 
QuestionCodeproject not be used for political agenda PinmemberMember 271004225-Nov-13 23:31 
AnswerRe: Codeproject not be used for political agenda PinmemberYahya Mohammed Ammouri26-Nov-13 20:00 
GeneralRe: Codeproject not be used for political agenda Pinmembergrralph130-May-14 2:29 
GeneralRe: Codeproject not be used for political agenda PingroupNewPast19-Jun-14 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 29 May 2014
Article Copyright 2013 by NewPast
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid