Gcode and commands

本文深入解析了CNCSimulator中的G代码指令,包括G0、G1、G2、G3、G4等常用代码的含义、格式及应用实例,为用户提供全面的G代码知识指南。

G codes

Parent Previous Next

The following tables will give a brief explanation to the various G, M and other codes recognized by the CNCSimulator interpreter. In the Format column, you will see the format expected by the program. If a code is written within brackets like this [X#] it means that the code is non obligatory and can be omitted if not needed. The # sign means that the CNCSimulator expects a number and the $ sign means that it is expecting a text string within quotation marks.


Let's show an example:

G12[X#][Y#]Z#R#Q#S#SA#AS#RA#H#


This means that the code G12 accepts X and Y coordinates but they are not obligatory and can be omitted. On the other hand, you must specify the Z,R,Q,S,SA,AS,RA and H codes to avoid an alarm at runtime.

G-code

(codes valid from V1.0.6.5)

Explanation

Format

Example

Machine type

G0 or G00

Go rapidly (with maximum traverse rate) to the X/Y/Z position. This code is used for position and not for actual machining.

G0 [X#][Y#][Z#]

G0 Z100

All machines

G1 or G01

Travel in a straight line using the programmed feed rate (F). This code is used for machining.

G1 [X#][Y#][Z#][F#]

G1 X2.5 Y4.1 F200

All machines

G2 or G02

XY-machines

Lathe

Circular/Helical Interpolation clockwise. It causes a clockwise circular movement at programmed feed rate (F). The motion can be 2-dimensional (flat) or 3-dimensional (helical). The default plane of the circular movement is the XY-plane (G17) but other planes can be used as well (see G17-G19). The center of the arc or circle is programmed using the I, J and K letters (R can also be used).

G2[X#][Y#][Z#]

[I#][J#][K#][R#][F#]

G2 X10 Y10 I10 J0 F200

All machines

G3 or G03

XY-machines

Lathe

Exactly like G2 but the circular motion is going counterclockwise.

G3[X#][Y#][Z#]
[I#][J#][K#][R#][F#]

G3 X10 Y10 I10 J0 F200

All machines

G4

Dwell in milliseconds. This will keep the axes unmoving for the period of timed specified by the P number.

G4 P#

G4 P2000 (Two seconds delay)

All machines

G12

Circular drilling canned cycle. Use to drill holes around the contour of a circle. R is starting plane and Z is total drill depth for each hole. Q is incremental depth (peck). SA is circle start angle (angle of first hole too) and AS is angle between holes. RA is circle radius and H specifies the number of holes to drill.

G12[X#][Y#]Z#
R#Q#S#SA#
AS#RA#H#

G12 X0 Y0 Z-20 R2 Q5 SA0 AS36 RA30 H10

Milling machines only

G17

Selects the XY plane for circular movements (see G2 and G3).

G17

G17

Milling machines only

G18

Selects the XZ plane for circular movements (see G2 and G3)

G18

G18

Milling machines only

G19

Selects the YZ plane for circular movements (see G2 and G3)

G19

G19

Milling machines only

G28

Return home command. This command will first go to the programmed position X/Y/Z and then to the Xmin Ymax Zmax of the machine axes. It can be a convenient way to end a program putting the machine table in a position to change workpiece.

G28[X#][Y#][Z#]

G28 Z10

Milling and turning machines only

G40

Cancel cutter compensation previously activated by G41 or G42.

G40

G40

All machines except the 3D Printer

G41

Activates left side cutter compensation (or nose radius compensation in a lathe).

G41

G41

All machines except the 3D Printer

G42

Activates right side cutter compensation (or nose radius compensation in a lathe).

G42

G42

All machines except the 3D Printer

G54-G59

Fixture (work) offsets. A typical use of these G-codes is to establish a local coordinate system for each workpiece when using multiple ones. You need to setup the offsets in the Zero Points Data table in the Inventory Browser (F2).

G54 corresponds to offset registry index 0, G55 to index number 1 etc…

G54

G0 G54 X0 Y0 Z3

All machines

G54.1

Fixture (work) offsets. A typical use of these G-codes is to establish a local coordinate system for each workpiece when using multiple ones. You need to setup the offsets in the Zero Points Data table in the Inventory Browser (F2).

G54.1 uses letter P to specify the offset registry index.

G54.1 P# (0-99)

G54.1 P10 (Use work offset 10)

All machines

G73

Peck drilling canned cycle. The cycle is intended for deep drilling or chip breaking milling operations. The cycle retracts the tool to break chips. Code letter Q is used for peck size. R is starting plane and Z is total depth. Parameter P is used for dwell at each peck. Please note that at the end of the cycle, the return position in Z is controlled by G98 and G99.

G73 [X#][Y#][Z#]
[R#][Q#][P#]

G73 Z-20 R1 Q1 P100

Milling and turning machines only

G74-G76

Generic drilling/boring/tapping canned cycle. These are used in a generic way to create compatibility with many common CNC controllers on the market. They will bring the tool to the programmed Z depth. If R is programmed it will be used as the start plane, if not the current Z position will be used as the start plane. All other parameters will be ignored.

G74 [X#][Y#][Z#][R#]

G74 Z-20 R1

Milling and turning machines only

G80

Cancels any canned cycle. Please note that G0 – G3 also cancels canned cycles.

G80

G80

Milling and turning machines only

G81

Basic drilling canned cycle. R is starting plane and Z is total depth. Please note that at the end of the cycle, the return position in Z is controlled by G98 and G99.

G81 [X#][Y#][Z#][R#]

G81 Z-6 R2

Milling and turning machines only

G82-G89

Generic drilling/boring/tapping canned cycle. Same as G74-G76 above.

G82 [X#][Y#][Z#][R#]

G82 Z-20 R1

Milling and turning machines only

G90

Absolute programming mode. Distances given will move the tool relative to an absolute zero.

G90

G90 G0 X10 Y10

All machines

G91

Incremental programming mode. Distances given will move the tool relative to the current position of the tool.

G91

G91 G0 Z5

All machines

G92

Use to reposition the origin point (zero point).

G92[X#][Y#][Z#]

G92 X20 Y20 Z10

All machines

G98

Initial level return at the end of a canned cycle.

G98

G81 G98  Z-7 R2

Milling and turning machines only

G99

R level return at the end of a canned cycle.

G99

G81 G99  Z-7 R2

Milling and turning machines only



Created with the Personal Edition of HelpNDoc: Easy EBook and documentation generator

<think> 我们正在讨论的是如何在Visual Studio Code中开发PowerShell模块、命令和脚本。用户的问题非常具体,我们需要提供详细的步骤和必要的配置说明。 首先,我们需要确保用户已经安装了必要的组件: 1. Visual Studio Code (VS Code) 2. PowerShell扩展 3. PowerShell自身(Windows自带或跨平台版本) 步骤可以分解为: 1. 安装和配置环境 2. 创建PowerShell模块 3. 编写命令和脚本 4. 调试和测试 5. 发布模块(可选) 接下来,我们将详细说明每个步骤。 ### 1. 安装和配置环境 - 安装VS Code:从[官网](https://code.visualstudio.com/)下载并安装。 - 安装PowerShell扩展: 打开VS Code,转到Extensions视图(Ctrl+Shift+X),搜索“PowerShell”,由Microsoft发布,点击安装。 - 确保已安装PowerShell: - Windows:通常已安装,可通过Win+R,输入`powershell`确认。 - 非Windows系统:安装[PowerShell](https://github.com/PowerShell/PowerShell)。 ### 2. 创建PowerShell模块 PowerShell模块是一个包含PowerShell命令(函数、Cmdlet等)的包。创建步骤: - 创建一个文件夹作为模块的根目录,例如`MyPSModule`。 - 在该文件夹中创建模块文件,通常以`.psm1`为扩展名,例如`MyPSModule.psm1`。 - 在模块文件中定义函数。例如: ```powershell function Get-HelloWorld { Write-Output "Hello, World!" } ``` - 创建模块清单文件(`.psd1`),使用`New-ModuleManifest`命令: 在VS Code的终端(Ctrl+`)中运行: ```powershell New-ModuleManifest -Path ./MyPSModule.psd1 -RootModule MyPSModule.psm1 -Author "YourName" -Description "A sample module" ``` ### 3. 编写命令和脚本 在VS Code中编写PowerShell脚本(`.ps1`)或模块代码时,利用PowerShell扩展的特性: - 语法高亮和自动完成:扩展提供智能感知。 - 代码片段:输入部分命令,按Tab键自动补全。 - 代码格式化:Shift+Alt+F(Windows)或Shift+Option+F(Mac)格式化代码。 ### 4. 调试和测试 VS Code的PowerShell扩展提供强大的调试功能: - 设置断点:点击行号左侧的空白处。 - 启动调试:按F5,选择PowerShell环境。 - 运行脚本:在编辑器中右键选择“Run”或使用终端执行。 - 使用PowerShell内置的`Pester`框架进行单元测试(需安装Pester): 创建测试文件(如`tests.tests.ps1`),编写测试用例,然后运行测试。 ### 5. 发布模块(可选) 如果希望共享模块,可以发布到[PowerShell Gallery](https://www.powershellgallery.com/): - 注册账号。 - 使用`Publish-Module`命令发布。 ### 总结 在VS Code中开发PowerShell模块、命令和脚本,主要步骤包括环境配置、模块创建、编写代码、调试测试和发布。利用VS Code的PowerShell扩展可以极大地提高开发效率。 §§相关问题§§ 1. 如何配置VS Code以获得更好的PowerShell开发体验? 2. 如何为PowerShell模块编写单元测试? 3. 如何将PowerShell模块发布到私有仓库? </think> ### 在Visual Studio Code中开发PowerShell解决方案 在VS Code中开发PowerShell模块、命令和脚本需要以下核心组件和步骤: #### 1. **环境准备** - 安装[VS Code](https://code.visualstudio.com/) - 安装官方[PowerShell扩展](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell) - 确保已安装[PowerShell 7+](https://github.com/PowerShell/PowerShell) ```powershell # 验证PowerShell版本 $PSVersionTable.PSVersion ``` #### 2. **项目结构创建** 标准PowerShell模块目录结构: ``` MyModule/ ├── MyModule.psd1 # 模块清单 ├── MyModule.psm1 # 主模块文件 ├── Public/ │ └── Get-MyCommand.ps1 # 公开函数 ├── Private/ │ └── InternalHelper.ps1 # 私有函数 └── Tests/ └── MyModule.Tests.ps1 # Pester测试 ``` #### 3. **关键开发功能** 1. **代码智能感知** PowerShell扩展提供: - 自动补全Cmdlet/参数 - 实时语法检查 - 函数定义跳转 2. **调试配置**(`.vscode/launch.json`): ```json { "version": "0.2.0", "configurations": [ { "type": "PowerShell", "request": "launch", "name": "调试当前文件", "script": "${file}", "cwd": "${fileDirname}" } ] } ``` 3. **模块开发流程**: ```powershell # 创建新模块 New-ModuleManifest -Path ./MyModule.psd1 # 导出函数(在.psm1文件中) Export-ModuleMember -Function 'Get-*' ``` #### 4. **测试与调试** - 使用[Pester](https://pester.dev/)框架: ```powershell Describe "模块测试" { It "验证函数输出" { Get-MyCommand | Should -Be "ExpectedResult" } } ``` - 按F5启动调试,支持: - 断点设置 - 变量监视 - 调用堆栈跟踪 #### 5. **实用扩展推荐** | 扩展名称 | 功能 | |---------|------| | [PowerShell ISE Theme](https://marketplace.visualstudio.com/items?itemName=RobSiklos.PowerShell-ISE) | ISE风格主题 | | [Pester Test Explorer](https://marketplace.visualstudio.com/items?itemName=pspester.pester-test) | 可视化测试管理 | | [Code Spell Checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker) | 拼写检查 | #### 6. **最佳实践** 1. 使用`[CmdletBinding()]`启用高级函数特性 2. 通过`$PSScriptRoot`引用模块内资源 3. 遵循[PowerShell脚本分析规则](https://github.com/PowerShell/PSScriptAnalyzer) 4. 使用`Write-Verbose`替代调试输出 > 提示:按`Ctrl+Shift+P` > `PowerShell: Show Session Menu`可快速切换PowerShell版本[^1]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值