'****************************************************************************
'Intellectual property of John Pearson (aka cjmerlin) created on 5th Nov 2006
'****************************************************************************

'This macro was designed for an 8051 microcontroller connected to a Denford Orac Toolchanger but can be 
'used with any suitably programmed microcontroller.
'It uses a simple form of handshaking with the micro in the form of a carriage return value sent to, and received
'from the micro.

'it also has Error coding to get out of routine loops, which displays in the Mach message box

'First thing to do to enable Mach to talk and receive input from a micro is to click on 'config/ports & pins'
' and select event driven serial control.
'Next go to 'config/serial monitor' and configure the port,baud rate and click parity = none checkbox
'Click 'reconnect/save changes' button and then restart Mach.

'You should now be able to use this macro if your micro has been programmed to send and recieve the values below.
'Note to make them easier to see, all microcontroller values are in hexadecimal and start with '&H' 

'All the usual diclaimers, This macro is intended to help you understand how to write your own macro for 
'your machine.
'Or you can alter this one if it suits your purpose.  

tool = GetSelectedTool()

ToolCommand = &H1E     'Tells microcontroller the job is tool change
JobDone = &HB6         'Micro sends this value when the job is done
CReturn = &H0D         'Value of carriage return in Hexadecimal


Sendval = 3           'Number of tries to send command before producing an error
RecVal = 10           'Number of time to wait for receive handshake value from micro before producing an error.


Select Case tool

Case 1
ToolN = &H1           'Tool value sent to microcontroller
Case 2
ToolN = &H2
Case 3
ToolN = &H3

Case 4

Case 5

End Select 

message "Changing To Tool " & tool
 

'============================== Send Command and recieve return chr ===============================
Do
q =q +1
If GetInf = Chr(CReturn) Then Exit Do                  'if micro returned chr "1" ,job done exit loop to next routine

 
 For count = 1 To 5
 x = getfifoentry()                               'Flush buffer, Rule of thumb is to flush once per chr recieved during job.
 Next                                             'If Mach misses a carriage return it wont pickup what was sent
                                                  'and will add it to the next string of chr's sent causing errors

Call sendfifo (Chr(ToolCommand) & Chr(ToolN) & Chr(CReturn))   'send 3 chr's 'Tool Command,Tool Number,carriage return or something

 For t = 1 To 5                                  'Read buffer 5 times before sending command again
   GetInf = getfifoentry()                        'get buffer return chr
   If GetInf <> "None" Then Exit For              'if chr returned then exit
   SetTimer( 1 ) 
   While GetTimer(1) < 0.05                        'wait approx 0.5 seconds, this value works for me
   Wend
 Next t

Loop Until q= SendVal

If q = SendVal Then 
Message "Sending Tool Command Error"
Exit Sub
End If

'====================================================================================================


'****************************** Waiting for Tool selected signal from micro **************************

 Mrec = "none"
 Do
    q= q+1
  If q = RecVal Then                    'Gets us out of the loop after 10 tries and produces an error.
    Message "Receive Error"
    Exit Sub
  End If            
 
    SetTimer( 1 )
  While GetTimer(1) < 0.05              'Wait
  Wend
    Mrec = getfifoentry()               'get return chr
    'message Mrec
Loop Until Mrec <> "None"

If Mrec = Chr(JobDone)  Then            'If returned chr correct then job done
message "Tool " & tool & " Selected"
SetCurrentTool(tool)

Else
Message "Tool Change Error"
End If
      
