Skip to main content

Usually in a Galil motion control application, the Host pc serves as the master/client by establishing a connection to the motion controller. It can be handy, however, to do the opposite. By running a simple program that listens to a specific network card on a specific port, your PC can be turned into a slave/server. This way a Galil motion controller can connect to the PC. From there the PC and motion controller can exchange data. This allows for asynchronous connections from a controller to one or multiple PCs. Possible applications:

1.) asynchronous dumps of controller data, arrays, variables, etc. Backup of controller EEPROM to central server.

2.) Controller "pushing" events to pc, perhaps allowing the PC to invoke email, SMS, or other higher-level functions.

3.) Fully DMC-code-centric application design for Galil language fans.

The below code as a minimal VB example of how to create a PC server.

'minimal code to sart listening to a port on the host.  Allows the Galil Controller to be the master (client) over the PC (slave/server)
'IHB=>-2
'WT100
'IHB=192,168,1,10<13000>2
'WT100
'CFB
'CW2
'c=0
'#LOOP
'MGTIME
'WT1000
'c=c+1
'JP#LOOP,c<10
'IHB=>-2
'EN

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic

Module Module1
    Sub Main()

        Dim server As TcpListener
        server = Nothing
        Try
            ' Set the TcpListener on port 13000.
            Dim port As Int32 = 13000
            Dim localAddr As IPAddress = IPAddress.Parse("192.168.1.10") 'address of host's nic card (where to listen locally)
            server = New TcpListener(localAddr, port)

            ' Start listening for client requests.
            server.Start()

            ' Buffer for reading data
            Dim bytes(1024) As Byte
            Dim data As String = Nothing

            ' Enter the listening loop.
            While True
                Console.Write("Waiting for a connection... ")

                ' Perform a blocking call to accept requests.
                ' You could also user server.AcceptSocket() here.
                Dim client As TcpClient = server.AcceptTcpClient()
                Console.Write("Connected to " + client.Client.RemoteEndPoint.ToString + " ")

                data = Nothing

                ' Get a stream object for reading and writing
                Dim stream As NetworkStream = client.GetStream()

                Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(Chr(18) + Chr(22) + vbCrLf) 'send control R control V to display controller type
                stream.Write(msg, 0, msg.Length)

                Dim i As Int32

                ' Loop to receive all the data sent by the client.
                i = stream.Read(bytes, 0, bytes.Length)
                While (i <> 0)
                    ' Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                    Console.Write(data)
                    i = stream.Read(bytes, 0, bytes.Length)
                End While

                ' Shutdown and end connection
                client.Close()
            End While
        Catch e As SocketException
            Console.WriteLine("SocketException: {0}", e)
        Finally
            server.Stop()
        End Try

        Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
        Console.Read()
    End Sub 'Main

End Module