Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: The_black_cat_dies on August 22, 2007, 12:28:04 PM

Title: Serial Port control under cypress enable
Post by: The_black_cat_dies on August 22, 2007, 12:28:04 PM
Hi
I have been trying for some time now to get the serial control to work reliably sending a file "900 bytes text" to my Boxford Duet. When I use the VB script editor and run it, it works perfectly however when I run a G code file and include it as a macro "M20" it stalls after 20-30 bytes, I have HyperTerminal observing the output. Also Mach seems to want to run the macro immediately the G code is loaded, what am I doing wrong I include the small macro I have written. 

Sub intialise_boxford()

message "Initialising Boxford"

For count = 1 To 255 ' Clear buffer seems to help
x = getfifoentry()
Next count

Open "c:\mach3\duet.dat" For Input As #1 ' Open file.

Do While Not EOF(1) ' Loop until end of file.

char = Input (1,#1) ' Read value into variable.

Call sendfifo(char)

Loop

message "Boxford Initialised"

Close #1 ' Close file.

End Sub

Any help would be gratefully appreciated, as this is frustrating as I’m very close to getting everything running.
Title: Re: Serial Port control under cypress enable
Post by: poppabear on August 22, 2007, 05:45:14 PM
Since your running that as a macro, mach will run it all out, and then keep on going on the Gcode.

What you might have to do is put a "control bit" in your Macro.

At the end of your Macro put:

SystemWaitFor(INPUT#) 'pick a input number
In your serial file, also toward the end before the line above, have it send some command that the machine has to activate some output
that come in on: INPUT#, Mach will pause and wait in your maco, until you machine recieves the full file, since at the end of that file, it will
trigger your physical machine to send to Mach a "I got it", bit. Once the system wait gets that bit (INPUT#) going active, it will drop out of the macro and continue executing your Gcode.

Scott
Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on August 23, 2007, 07:50:24 AM
Hi Thanks for that poppabear I see your point but that isn't the problem, the macro as it was just would not work when running in a script.

I managed to get the file to write to serial by using the following



Sub intialise_boxford()

For i = 1 To 100
getfifoentry()
Next i                                                         ' clear input buffer if there is one?


message "Initialising Boxford"                          ' Start message

Open "c:\mach3\duet.dat" For Input As #1      ' Open file.

Do While Not EOF(1)                                     ' Loop until end of file.

outint = Asc(Input (1,#1))                             ' Read byte into variable.

PutPortByte(1016,outint)                               ' write byte to COM1 Low level but works

GetFIFOentry()                                              ' either adds delay or clears buffer ?
 
Loop                                           ' Get another byte

message "Boxford Initialised"           ' completiion message

playwave "c:\tada.wav"                  ' file has been sent works :-) 

Close #1 ' Close file.                       ' Close file "Duet.dat"

For i = 1 To 1000000                     ' delay give message time to be seen
Next i

message ""                                    ' clear messages off screen

End Sub

Although this works, I think I need to check the transmit status bit on the serial port instead of the (GetFIFOentry()) which I think is just adds a delay, this is a major success and I will be moving on the receiving data soon. however Mach still seems to want to show all messages within the macro when the G code file is loaded, this is stange it doesn't appear to actually run any code it just displays the messages ?
any ideas greatfully received also are the macros running in their own thread ie multitasking?


Title: Re: Serial Port control under cypress enable
Post by: poppabear on August 23, 2007, 08:15:51 AM
Since you running this script in a Macro, and you want some pauses use this instead (where ever you need pauses)

Code "G4 P1"   'P=seconds (You can use partial seconds)
While IsMoving()
Wend

For your Messages tie them into some kind of:
 If (event) Then
Message("")
End If

***NOTE: as far as I am currently aware of Mach can only SEND serial stuff NOT receive it....You might want to ask Brian about getting the serial to recieve...........Thats why, I had your transmissions from the machine comming in on other inputs.

Scott
Title: Re: Serial Port control under cypress enable
Post by: Whacko on August 23, 2007, 01:24:53 PM
Have you got the modbus option enabled? Also, out of the Mach3 environment, serial I think is only supported with modbus protocol, and includes CRC. That could be why you only get X amount of bytes out. Just a thought, I may be way out.

Whacko
Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on August 23, 2007, 02:32:12 PM
Hi
Thanks for the input, I now have mach sending the file using the last bit of script. What may be of of interest to you all is that Mach will receive serial through the VB script, if you plug into another PC using a NULL modem cable and run hyperterminal then run this on your Mach machines VB script.

For i = 1 to 1000
x = getportbyte(1016)     ' Assumes you are using COM1 at &h3F8
putpportbyte(1016)
next i

You will see that every key you press on the terminal PC will be returned to the terminal PC via the VB script of Mach, not very usefull at the momment but just shows bi directional communication is possible, if I can now find the data flow control bits I think it will be possible that Mach can talk to any serially controlled hardware I hope :-)   
Title: Re: Serial Port control under cypress enable
Post by: Whacko on August 23, 2007, 06:54:10 PM
The file you want to send, is it constantly appended in the Mach3 environment? Remember, running a script is part of the OS and you could even do the same by running it on it's own without starting Mach3 in a .vbs file. What you are doing now is been taken care of by the OS via the VB Script engine and is meaningless to Mach3.

Whacko
Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on August 24, 2007, 12:48:35 PM
Hi
the file is a simple text file containing the microcode for the Boxford Duet the program simple reads it out byte by byte.

The Boxford duet requires that a file is sent to the machine to initialise it, it is more than usefull to do this within the Mach environment as I wish to fuilly integrate the Duet and it works!, on the subject of serial reads the macro may or may not be able to be understood  by Mach but I believe the macro can understand Mach variables and that is all I need as the duet also sends details of gaurd positon, I need to receive one or two bytes just to confirm that the gaurd is in place I am sure I can poke a few bits to acheive this. I have only just within thae last few days started programing with the Macros so I need ideas of what can be acheived. Is the macro multitasking in its own thread?


Cheers
Title: Re: Serial Port control under cypress enable
Post by: Whacko on August 24, 2007, 01:16:21 PM
I just want to clear something, is this file only sent at initialisation and not again, or should it be sent whenever the gaurd has been opened/closed/moved?.
The macro will be executed by Mach3 depending on how you implement it. MultiTasking is part of the Windows OS, maybe I don't understand what you mean. I think you want to know if once the macro is started, it will run independantly not under the control of Mach3. You can send the file via your serial port with the VBScript, but you will have to choose a second port if you are using the modbus with Mach3.
I unfortunately do not have experience with the Boxford, but I have lots of experience with serial ports etc.
You can even invoke another process from the VBScript that will run as a thread on its own.
I will try to help you if you give me some more info. The VBScript in Mach3 is actually the proper Windows OS Script engine, and the Cypress information is limited, you can use the MSDN help file for VBScript. There is a lot of possibilities.

Whacko
Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on August 25, 2007, 06:23:23 AM
Hi
Thanks for that I will not be using Modbus so I have a dedicated serial port to the Boxford, I will be looking for anything that helps

The general idea is :-

1) start Mach
2) load G code file

The G code File then includes

3) Initialise Boxford Macro ( this is the one we are talking about)
4) Send speed control to Boxford (next TODO list )
5) Get gaurd information/ speed ( continually)
5) run G code
 
The initialise_boxford part of the macro is run only once at the beginning of work( does not need to be sent again unless the machine is switched off/on), if the boxford is already initialised then it will be programmed not to do it again, I do intend however to have this macro home the axis and basically set the machine up, the Guard  postion is also found ( closed / open) and dependant on this I can run the rest of the G code or not, it would be nice however for a gaurd open to control a stop, this means having a serial reception running all the time, I have run out of inputs in mach so I can't use hardware.

The Boxford speed control is also set by serial commands these I will attach to the relevant macro, the spindle speed is reported back by serial @ once a second but as I have already interfaced the spindle read to Mach so it is not that important.

I have the initialise script written, however this does not yet check that the initialisation has already been done, I dropped the messages as they pop up even before the macro is run?

*****************************************************************************************

Sub intialise_boxford() ' Boxford Duet1 9600 8 data 1 stop even parity

'******************************** file send routine *****************************
message "Initialising Boxford"
Open "c:\mach3\duet.dat" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
outint = Asc(Input (1,#1)) ' Read value into variable.
loop_1: If (getportbyte(1021) And 32)<>0  Then   'Line Status Register bit 5 TX holding reg empty
putportbyte(1016,outint) 'This is the TX register
Else
GoTo loop_1
End If
Loop
message "Boxford Initialised"
playwave "c:\tada.wav"
Close #1 ' Close file.
'***********************************************************************************
End Sub


**** The read macro looks at the momment like this it's saving to a file so I can see the results


***************************************************************
' This works and reads just the available serial byte received

Open "c:\mach3\Dout.dat" For Output As #1 ' Open file.

For i = 1 To 10000
If (getportbyte(1021) And 1) <> 0 Then ' receive status register
inbyte = getportbyte(1016)
Print #1,inbyte
End If
Next i

*************************************************************

Thanks Again

Title: Re: Serial Port control under cypress enable
Post by: Whacko on August 26, 2007, 03:53:58 PM
I want to establish compatibility with what I want to do for you.

Copy and paste the following text in notepad. Save the file on your desktop with a .vbs file extension, example ComTest.vbs
Then close the file, exit notepad and doubleclick the .vbs file on your desktop. Wait for 10 seconds, and check if you get the second textbox.
If it works we can proceed.
----------------------------------------------------------------

'Testing compatibility
'
PORT = "COM1"
MsgBox "Ready to do 10 second test!"
Set fso = CreateObject("Scripting.FileSystemObject")
Set com = fso.OpenTextFile("COM1")
Wscript.Sleep 10000
com.Close()
MsgBox "10 Seconds passed, no error!"



--------------------------------------------------------------------------------------------------------

Let me know if it works on your pc.

Whacko
Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on September 05, 2007, 08:11:30 AM
Hi
Thanks for that I've been on holiday for a week (Spain in the SUN compared to the rainy UK it's been bliss)

I tried the code but I only have the inbuilt cypress enable VB scripter and this code does not work with it.

I have had some sucess and have now moved on to the tool changer "if anyone wants the code for the serial give us a shout" the peculiarity of the boxford tool changer is that tools are changed using X and Y , I need to first detect which tool is pesently in use ie GetCurrentTool(), GetSelectedTool() and SetCurrentTool() then this determines which direction to goto the safe zone then the tool holder has to goto the front or back depending on which tool is to be selected, one of the problems is that the tool offset has a Y component, so I need to add a Y position indicator to Mach turn. as usual any ideas greatfully received.
Title: Re: Serial Port control under cypress enable
Post by: Huebe on December 16, 2010, 03:42:02 PM
Hi “The_black_cat_dies” or anyone.
I just got an boxford duet and I want to control it with mach3. And I have the same problem, i can’t let my Spindel run! Unfortunately i don’t have any experience with VB or Mach3. I just startet up experimenting with the duet. I was reading the posts here and have seen you were able to solve the problem. Maybe you can help me to let the spindel of my duet run. I would be glad to get the code for the serial! And maybe also some tips how to configurate Mach.
Any help would be great...
Title: Re: Serial Port control under cypress enable
Post by: cjmerlin on December 18, 2010, 01:16:43 PM
Hi, Very interesting post. I have a Boxford TCL160 lathe that is original and uses Win98 software.

I would be interested to understand the serial communication Boxford uses and try to get Mach3 working with it.

I have put up with the win98 software for now but the reason I need a change is that the tool is selected by type and not tool position. I have a few tools of the same type but it will only select the first one it sees. It also can't do internal righthand threads without me having to tinker with the postprocessing which can be a manual nightmare.

Great work..
Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on December 29, 2010, 03:12:23 PM
Hi this is the cypress enable script to run the spindes this loads the rom on the controller board
with the file duet.dat which exists within the boxford software which you can download sorry
its not simple but that is the way it is you must talk directly to the serial ports and send six bytes
use a null modem cable you will know you have it working because there are two led's on the board
which flash and then stop, see the manua, try it ouot if you have problems place a post.


********************* M2000 **********************************
' This is a working initialisation procedure modified 051208


Sub intialise_boxford() ' Boxford Duet1 9600 8 data 1 stop even parity

'******************************** file send routine *****************************
Dim counter As Integer
Dim bytes As Integer
Dim flag As Integer

counter = 0
bytes = 0
flag = 0

Do 

   If (getportbyte(1021) And 1) <> 0 Then

   inbyte = getportbyte(1016)

   bytes = bytes + 1
   
   End If

   counter = counter + 1

   If counter > 10000 Then

   flag = 0
   
   Exit Do
   
   End If
   
   If bytes > 6 Then
   
'Print   "Boxford Already Initialised"
   
   flag = 1

   Exit Do
   
   End If
   
Loop


If flag =1 Then

GoTo Boxford_already_initialised

Else

GoTo Initialise_boxford

End If

 
Initialise_boxford:


putportbyte(888,(GetPortByte(888) Or 128)) 'This is the TX register


For i = 1 To 1000000
Next i

putportbyte(888,(PortByte And 127)) 'This is the TX register

For i = 1 To 1000000
Next i


Open "c:\mach3\data\duet\duet.dat" For Input As #1 ' Open file.
Open "c:\mach3\data\duet\Dout.dat" For Output As #2 ' Open file.

Do While Not EOF(1) ' Loop until end of file.

   outint = Asc(Input (1,#1)) ' Read value into variable.

loop_1: If (getportbyte(1021) And 32)<>0  Then   'Line Status Register bit 5 TX holding reg empty

      putportbyte(1016,outint) 'This is the TX register

   Else

   GoTo loop_1

   End If
 
Loop

   For i = 1 To 1000000
   Next i


bytes = 0
counter = 0

Do 

   If (getportbyte(1021) And 1) <> 0 Then

   inbyte = getportbyte(1016)

   Print #2,inbyte

   bytes = bytes + 1
   
   End If

   counter = counter + 1

   If counter > 10000 Then

   Print "Boxford not initialised"

   
   Exit Do
   
   End If
   
   If bytes > 6 Then

   Exit Do
   
   End If
   
   
Loop

Close #1 ' Close file.
Close #2 ' Close file.

Boxford_already_initialised:

DeActivateSignal(11)   'A
DeActivateSignal(10)   'B   This will set the index input
DeActivateSignal(9)   'C

code "M05"


End Sub


' M03 start the spindle in CW as viewed from the chuck towards the tail stock (correct view)

Dim outint(7) As Integer

outint(1) = Asc("S")
outint(2) = 0
outint(3) = 255
outint(4) = 0
outint(5) = 0
outint(6) = 0
outint(7) = 16


If (getuserLED(1010) =1 And  getuserLED(1011) = 0) Then

Message "Spindle Already CW"

Else

   For i = 1 To 7

   putint = outint(i)

   loop_1: If (getportbyte(1021) And 32)<>0  Then  'Line Status Register bit 5 TX holding reg empty
      putportbyte(1016,putint)       'This is the TX register

   Else

   GoTo loop_1
   
   End If

   Next i

   Call setuserLED(1010,1)            'Sets the LED variable to be read by getuserLED(1010)
   Call setuserLED(1011,0)            'for spindle cal CW and CWW as these are different
                     'both are set so an error check can be performed

   message "Spindle CW"
   
   DoSpinCW()

End If


Sub Mach_Code(Code_String)

Code Code_String

While ismoving()
Wend


End Sub


' M04 start the spindle in CCW as viewed from the chuck towards the tail stock (correct view)

Dim outint(7) As Integer

outint(1) = Asc("S")
outint(2) = 255
outint(3) = 0
outint(4) = 0
outint(5) = 0
outint(6) = 0
outint(7) = 16

If (getuserLED(1010) =0 And  getuserLED(1011) = 1) Then

Message "Spindle Already CWW"

Else

   For i = 1 To 7

   putint = outint(i)

   loop_1: If (getportbyte(1021) And 32)<>0  Then  'Line Status Register bit 5 TX holding reg empty
      putportbyte(1016,putint)       'This is the TX register

   Else

   GoTo loop_1
   
   End If

   Next i

   Call Mach_Code("G04 P200")

   Call setuserLED(1010,0)            'Sets the LED variable to be read by getuserLED(1010)
   Call setuserLED(1011,1)            'for spindle cal CW and CWW as these are different
                     'both are set so an error check can be performed

   Message "Spindle CCW"
   
   DoSpinCCW()
                     

End If                        

Sub Mach_Code(Code_String)

Code Code_String

While ismoving()
Wend


End Sub

Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on December 29, 2010, 03:17:35 PM
Sorry forgot the speed  I hope this is enough to get you started.

   ' *************** RPM setting code *******************

   speed = RPM(requested_rpm)
   
outint(1) = Asc("S")
outint(2) = 255
outint(3) = 255
outint(4) = 0
outint(5) = 128
outint(6) = Speed
outint(7) = 16

   For i = 1 To 7

   putint = outint(i)

   loop_1: If (getportbyte(1021) And 32)<>0  Then  'Line Status Register bit 5 TX holding reg empty
      putportbyte(1016,putint)       'This is the TX register

   Else

   GoTo loop_1
   
   End If

   Next i

   
   End If
Title: Re: Serial Port control under cypress enable
Post by: Huebe on January 03, 2011, 04:07:04 PM
Hello again, Thank you for the script! I tried it out and I was able to send the duet.dat to the boxford. The led's flash and then stop. That is already a great success to me, tnx.  But I still have a problem with the speed!
I saved your script under spindlespeed as a macro. If I want to run the spindle i get an error in the spindlespeed. If I run it in the VB Script editor I get (compile error: Syntax error) in the last line by End[/b] If.
Do you have any idea what i do wrong?
Title: Re: Serial Port control under cypress enable
Post by: The_black_cat_dies on January 04, 2011, 02:46:21 PM
Well done :-) so far so good. I think it's too many "end if" 's remove the last one (too many bourbons and coke for xmas). Careful with this the spindle can work at 3000 RPM also this is for the Duet MKI  I believe the MkII is different, also the guard needs to be down otherwise it won't start, you should hear the relay click on, also sorry my mistake it is 7 bytes. It could do with someone else confirming the codes I used the old software and put a COM serial sniffer to record the sent and received codes but my old dos pc is now dead.
I hope you noted that M03 and M04 are included in the post they need to split these out also I didn't include M05 I'm not near my lathe at the moment I think it's the same as M03,M04 just 0 in place of the 255 in  bytes  2&3
Title: Re: Serial Port control under cypress enable
Post by: Huebe on January 04, 2011, 05:23:29 PM
I noted that M3 and M4 was included and now I also made the M5. The last "End If" in the speed I removed.
now after starting the duet and run the M2000 in the MDI mode the led's flash and the green one is on. if i run M3 S500 in the MDI mode I also hear the relay click on. The message in the Status  line is (Scripter Compile Error. In: SpindleSpeed.mis) and the spindle does not turn. When I run after that M4 S500 or M5 the relay does not click anymore. (Message in the Status line: Spindle Already CW)
When I run the speed Script in the VB Script Editor I get (Error on line:3-Sub or function not defined:RPM)
Is It possible that in the Speed script is still something missing? I also believe the M5 Scrip does not realy stop the Spindle because I should hear the relay again, right?
I think one problem is the M5 script because after M5 I should be able again to hear the relay click again with M3 or M4. And the other is the speed script.