using System; using System.IO; using System.Linq; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; using System.Collections.Concurrent; using Timer = System.Windows.Forms.Timer; using weconcnc; namespace Demo { public partial class Form1 : Form { private readonly CCommApi m_pComm = null; private CProxyEntry m_pEntry = null; private CProxyStatus m_pStatus = null; private CProxySys m_pSys = null; private CProxyMotion m_pMotion = null; private CIoControl_C m_pIoCtrl = null; private readonly Timer timer = null; private readonly List> m_listIniConfig = null; private readonly List> m_listAbsConfig = null; public Form1() { InitializeComponent(); m_pComm = new CCommApi(); timer = new Timer(); timer.Interval = 100; timer.Tick += updateStatus; m_listIniConfig = new List>(); m_listAbsConfig = new List>(); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows[0].HeaderCell.Value = "1/脉冲当量"; gvAbsConfig.Rows[1].HeaderCell.Value = "导程(螺距)"; gvAbsConfig.Rows[2].HeaderCell.Value = "减速比分母"; gvAbsConfig.Rows[3].HeaderCell.Value = "减速比分子"; gvAbsConfig.Rows[4].HeaderCell.Value = "方向"; gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows[0].HeaderCell.Value = "最大速度(mm/s)"; gvIniConfig.Rows[1].HeaderCell.Value = "最大加速度(mm/s^2)"; gvIniConfig.Rows[2].HeaderCell.Value = "正限位"; gvIniConfig.Rows[3].HeaderCell.Value = "负限位"; gvIniConfig.Rows[4].HeaderCell.Value = "跟随误差"; gvIniConfig.Rows[5].HeaderCell.Value = "最小跟随误差"; gvIniConfig.Rows[6].HeaderCell.Value = "间隙"; richTextBoxGCode.MouseWheel += richTextBoxGCode_MouseWheel; } private void richTextBoxGCode_MouseWheel(object sender, MouseEventArgs e) { // 检查是否按下了Ctrl键 if (ModifierKeys == Keys.Control) { // 标记事件为已处理,阻止缩放效果 ((HandledMouseEventArgs)e).Handled = true; } } void updateStatus(object sender, EventArgs e) { if (null == m_pStatus) { Console.WriteLine("pStatus is null"); return; } MACHINE_STATE_E eState = new MACHINE_STATE_E(); var ret = m_pStatus.getMotionState(ref eState); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { return; } switch (eState) { case MACHINE_STATE_E.STATE_ESTOP: { lEstopState.Text = "STATE_ESTOP"; } break; case MACHINE_STATE_E.STATE_ESTOP_RESET: { lEstopState.Text = "STATE_ESTOP_RESET"; } break; case MACHINE_STATE_E.STATE_ON: { lEstopState.Text = "STATE_ON"; } break; default: { lEstopState.Text = "STATE_ESTOP"; } break; } COORDINATE_T stCoord = new COORDINATE_T(); ret = m_pStatus.getCoord(ref stCoord); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { return; } leXPos.Text = stCoord.getX().ToString("0.######"); leYPos.Text = stCoord.getY().ToString("0.######"); leZPos.Text = stCoord.getZ().ToString("0.######"); leAPos.Text = stCoord.getA().ToString("0.######"); leCPos.Text = stCoord.getC().ToString("0.######"); var queErr = new ConcurrentQueue(); ret = m_pStatus.getError(ref queErr); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { return; } while (!queErr.IsEmpty) { leErrInfo.Text = queErr.First(); queErr.TryDequeue(out _); } int nLine = 0; ret = m_pStatus.getLine(ref nLine); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { return; } leLine.Text = nLine.ToString(); double dVel = 0.0; ret = m_pStatus.getVelocity(ref dVel); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { return; } dVel *= 60; leCurrentVel.Text = dVel.ToString("0.######"); } private void btnRun_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } WECONCNC_ERROR_E ret; ret = m_pMotion.setFeedOverride(100); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("set feed override failed!"); return; } else { Console.WriteLine(ret); } ret = m_pMotion.runProgram(0); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("run program failed!"); } else { Console.WriteLine(ret); } } private void btnEstop_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.setEstop(true); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("set estop failed!"); } else { Console.WriteLine(ret); } } private void btnEstopReset_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.setEstop(false); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("set estop failed!"); } else { Console.WriteLine(ret); } } private void btnPause_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.pauseProgram(); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("pause program failed!"); } else { Console.WriteLine(ret); } } private void btnStop_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.stopProgram(); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("stop program failed!"); } else { Console.WriteLine(ret); } } private void btnResume_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.resumeProgram(); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("resume program failed!"); } else { Console.WriteLine(ret); } } private void btnEnable_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.setEnable(true); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("set enable failed!"); } else { Console.WriteLine(ret); } } private void btnStep_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.stepProgram(); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("step program failed!"); } else { Console.WriteLine(ret); } } private void btnDisable_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.setEnable(false); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("set enable failed!"); } else { Console.WriteLine(ret); } } private void btnRunMDI_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var ret = m_pMotion.execMDI(textMDI.Text); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("run mdi failed!"); } else { Console.WriteLine(ret); } } private void btnLink_Click(object sender, EventArgs e) { if (0 == leIp.Text.Length || 0 == lePort.Text.Length) { return; } String IP = leIp.Text; ushort Port = Convert.ToUInt16(lePort.Text); var eRtn = m_pComm.connect(ref IP, Port); if (eRtn != WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS) { Console.WriteLine("cnc link failed"); return; } m_pEntry = new CProxyEntry(m_pComm); m_pSys = m_pEntry.getProxySys(); m_pMotion = m_pEntry.getProxyMotion(); m_pStatus = m_pEntry.getProxyStatus(); timer.Start(); m_pIoCtrl = new CIoControl_C(); var ret = m_pIoCtrl.init(leIp.Text, Convert.ToInt32(leIoCtrlPort.Text)); if (ret != 0) { Console.WriteLine("io ctrl link failed"); } else { Console.WriteLine(eRtn); } } private void btnDisconnect_Click(object sender, EventArgs e) { if (null == m_pComm) { Console.WriteLine("pComm is null"); return; } timer.Stop(); var ret = m_pComm.disconnect(); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("disconnect failed!"); } else { Console.WriteLine(ret); } } private void btnIniConfGet_Click(object sender, EventArgs e) { if (null == m_pSys) { Console.WriteLine("pSys is null"); return; } m_listIniConfig.Clear(); gvIniConfig.Rows.Clear(); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows.Add(new DataGridViewRow()); gvIniConfig.Rows[0].HeaderCell.Value = "最大速度(mm/s)"; gvIniConfig.Rows[1].HeaderCell.Value = "最大加速度(mm/s^2)"; gvIniConfig.Rows[2].HeaderCell.Value = "正限位"; gvIniConfig.Rows[3].HeaderCell.Value = "负限位"; gvIniConfig.Rows[4].HeaderCell.Value = "跟随误差"; gvIniConfig.Rows[5].HeaderCell.Value = "最小跟随误差"; gvIniConfig.Rows[6].HeaderCell.Value = "间隙"; CNC_INI_CONFIG_T stIniConfig = new CNC_INI_CONFIG_T(); for (int i = 0; i < 9; i++) { var ret = m_pSys.getIniConfig(i, ref stIniConfig); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("get ini config failed!"); return; } else { Console.WriteLine(ret); } gvIniConfig.Rows[0].Cells[i].Value = stIniConfig.dMaxVel.ToString("0.######"); gvIniConfig.Rows[1].Cells[i].Value = stIniConfig.dMaxAcc.ToString("0.######"); gvIniConfig.Rows[2].Cells[i].Value = stIniConfig.dMaxLimit.ToString("0.######"); gvIniConfig.Rows[3].Cells[i].Value = stIniConfig.dMinLimit.ToString("0.######"); gvIniConfig.Rows[4].Cells[i].Value = stIniConfig.dFError.ToString("0.######"); gvIniConfig.Rows[5].Cells[i].Value = stIniConfig.dMinFError.ToString("0.######"); gvIniConfig.Rows[6].Cells[i].Value = stIniConfig.dBacklash.ToString("0.######"); // update buffer Dictionary IniConfigDict = new Dictionary { { (INI_CONFIG_FIELD)0, stIniConfig.dMaxVel.ToString("0.######") }, { (INI_CONFIG_FIELD)1, stIniConfig.dMaxAcc.ToString("0.######") }, { (INI_CONFIG_FIELD)2, stIniConfig.dMaxLimit.ToString("0.######") }, { (INI_CONFIG_FIELD)3, stIniConfig.dMinLimit.ToString("0.######") }, { (INI_CONFIG_FIELD)4, stIniConfig.dFError.ToString("0.######") }, { (INI_CONFIG_FIELD)5, stIniConfig.dMinFError.ToString("0.######") }, { (INI_CONFIG_FIELD)6, stIniConfig.dBacklash.ToString("0.######") } }; m_listIniConfig.Add(IniConfigDict); } } private void btnReadAbsConfig_Click(object sender, EventArgs e) { if (null == m_pSys) { Console.WriteLine("pSys is null"); return; } m_listAbsConfig.Clear(); gvAbsConfig.Rows.Clear(); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows.Add(new DataGridViewRow()); gvAbsConfig.Rows[0].HeaderCell.Value = "1/脉冲当量"; gvAbsConfig.Rows[1].HeaderCell.Value = "导程(螺距)"; gvAbsConfig.Rows[2].HeaderCell.Value = "减速比分母"; gvAbsConfig.Rows[3].HeaderCell.Value = "减速比分子"; gvAbsConfig.Rows[4].HeaderCell.Value = "方向"; CNC_ABS_CONFIG_T stAbsConfig = new CNC_ABS_CONFIG_T(); for (int i = 0; i < 6; i++) { var ret = m_pSys.getAbsConfig(i, ref stAbsConfig); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("get abs config failed!"); return; } else { Console.WriteLine(ret); } // update display gvAbsConfig.Rows[0].Cells[i].Value = stAbsConfig.dScale.ToString("0.######"); gvAbsConfig.Rows[1].Cells[i].Value = stAbsConfig.dLead.ToString("0.######"); gvAbsConfig.Rows[2].Cells[i].Value = stAbsConfig.dRRDen.ToString("0.######"); gvAbsConfig.Rows[3].Cells[i].Value = stAbsConfig.dRRNum.ToString("0.######"); gvAbsConfig.Rows[4].Cells[i].Value = stAbsConfig.bCw.ToString(); // update buffer Dictionary AbsConfigDict = new Dictionary { { (ABS_CONFIG_FIELD)0, stAbsConfig.dScale.ToString("0.######") }, { (ABS_CONFIG_FIELD)1, stAbsConfig.dLead.ToString("0.######") }, { (ABS_CONFIG_FIELD)2, stAbsConfig.dRRDen.ToString("0.######") }, { (ABS_CONFIG_FIELD)3, stAbsConfig.dRRNum.ToString("0.######") }, { (ABS_CONFIG_FIELD)4, stAbsConfig.bCw.ToString() } }; m_listAbsConfig.Add(AbsConfigDict); } } private void btnIniConfSet_Click(object sender, EventArgs e) { if (null == m_pSys) { Console.WriteLine("pSys is null"); return; } for (int i = 0; i < 9; i++) { List> listIniConfig = new List>(); for (int j = 0; j < 7; j++) { INI_CONFIG_FIELD iniFiled = (INI_CONFIG_FIELD)j; if (null == gvIniConfig.Rows[j].Cells[i] || null == gvIniConfig.Rows[j].Cells[i].Value) { m_listIniConfig[i][iniFiled] = ""; } else { if (gvIniConfig.Rows[j].Cells[i].Value.ToString() != m_listIniConfig[i][iniFiled]) { listIniConfig.Add(new KeyValuePair(iniFiled, gvIniConfig.Rows[j].Cells[i].Value.ToString())); } m_listIniConfig[i][iniFiled] = gvIniConfig.Rows[j].Cells[i].Value.ToString(); } } foreach (var config in listIniConfig) { Console.WriteLine($"{config.Key}: {config.Value}"); } if (listIniConfig.Count > 0) { var ret = m_pSys.setIniConfig(i, ref listIniConfig); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("set ini config failed!"); return; } else { Console.WriteLine(ret); } } } } private void btnWriteAbsConfig_Click(object sender, EventArgs e) { if (null == m_pSys) { Console.WriteLine("pSys is null"); return; } for (int i = 0; i < 6; i++) { List> listAbsConfig = new List>(); for (int j = 0; j < 5; j++) { ABS_CONFIG_FIELD absFiled = (ABS_CONFIG_FIELD)j; if (null == gvAbsConfig.Rows[j].Cells[i] || null == gvAbsConfig.Rows[j].Cells[i].Value) { m_listAbsConfig[i][absFiled] = ""; } else { if (gvAbsConfig.Rows[j].Cells[i].Value.ToString() != m_listAbsConfig[i][absFiled]) { listAbsConfig.Add(new KeyValuePair(absFiled, gvAbsConfig.Rows[j].Cells[i].Value.ToString())); } m_listAbsConfig[i][absFiled] = gvAbsConfig.Rows[j].Cells[i].Value.ToString(); } } foreach (var config in listAbsConfig) { Console.WriteLine($"{config.Key}: {config.Value}"); } var ret = m_pSys.setAbsConfig(i, ref listAbsConfig); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("set abs config failed!"); } else { Console.WriteLine(ret); } } } private void on_btnAdd_pressed(object sender, MouseEventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } int nAxis = 0; if (((Button)sender).Text.Contains("X")) { nAxis = 0; } else if (((Button)sender).Text.Contains("Y")) { nAxis = 1; } else if (((Button)sender).Text.Contains("Z")) { nAxis = 2; } else if (((Button)sender).Text.Contains("A")) { nAxis = 3; } else if (((Button)sender).Text.Contains("C")) { nAxis = 5; } double dVel = Convert.ToDouble(leVel.Text); double dDistance = Convert.ToDouble(leDistance.Text); Console.WriteLine($"{((Button)sender).Text} : {dVel} {dDistance}"); if (radioCont.Checked) { var ret = m_pMotion.continuousJog(nAxis, dVel); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("continuous jog failed!"); } else { Console.WriteLine(ret); } } else { var ret = m_pMotion.incrementJog(nAxis, dVel, dDistance); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("increment jog failed!"); } else { Console.WriteLine(ret); } } } private void on_btnAdd_released(object sender, MouseEventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } int nAxis = 0; if (((Button)sender).Text.Contains("X")) { nAxis = 0; } else if (((Button)sender).Text.Contains("Y")) { nAxis = 1; } else if (((Button)sender).Text.Contains("Z")) { nAxis = 2; } else if (((Button)sender).Text.Contains("A")) { nAxis = 3; } else if (((Button)sender).Text.Contains("C")) { nAxis = 5; } if (radioCont.Checked) { var ret = m_pMotion.stopJog(nAxis); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("stop jog failed!"); } else { Console.WriteLine(ret); } } } private void on_btnSub_pressed(object sender, MouseEventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } int nAxis = 0; if (((Button)sender).Text.Contains("X")) { nAxis = 0; } else if (((Button)sender).Text.Contains("Y")) { nAxis = 1; } else if (((Button)sender).Text.Contains("Z")) { nAxis = 2; } else if (((Button)sender).Text.Contains("A")) { nAxis = 3; } else if (((Button)sender).Text.Contains("C")) { nAxis = 5; } double dVel = Convert.ToDouble(leVel.Text) * (-1); double dDistance = Convert.ToDouble(leDistance.Text); if (radioCont.Checked) { var ret = m_pMotion.continuousJog(nAxis, dVel); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("continuous jog failed!"); } else { Console.WriteLine(ret); } } else { var ret = m_pMotion.incrementJog(nAxis, dVel, dDistance); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("increment jog failed!"); } else { Console.WriteLine(ret); } } } private void on_btnSub_released(object sender, MouseEventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } int nAxis = 0; if (((Button)sender).Text.Contains("X")) { nAxis = 0; } else if (((Button)sender).Text.Contains("Y")) { nAxis = 1; } else if (((Button)sender).Text.Contains("Z")) { nAxis = 2; } else if (((Button)sender).Text.Contains("A")) { nAxis = 3; } else if (((Button)sender).Text.Contains("C")) { nAxis = 5; } if (radioCont.Checked) { var ret = m_pMotion.stopJog(nAxis); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("stop jog failed!"); } else { Console.WriteLine(ret); } } } private void btnHome_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } int nAxis = 0; if (((Button)sender).Text.Contains("X")) { nAxis = 'X'; } else if (((Button)sender).Text.Contains("Y")) { nAxis = 'Y'; } else if (((Button)sender).Text.Contains("Z")) { nAxis = 'Z'; } else if (((Button)sender).Text.Contains("A")) { nAxis = 'A'; } else if (((Button)sender).Text.Contains("C")) { nAxis = 'C'; } var ret = m_pMotion.home(nAxis); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("home failed!"); } else { Console.WriteLine(ret); } } private void btnGetAllIoName_Click(object sender, EventArgs e) { if (null == m_pIoCtrl) { Console.WriteLine("pIoCtrl is null"); return; } List listPinName = new List(); List listPinValue = new List(); var ret = m_pIoCtrl.getSupportio(ref listPinName); Console.WriteLine(ret < 0 ? "get support io failed!" : "get support io succeed!"); if (ret < 0) { return; } ret = m_pIoCtrl.getIoValue(listPinName, ref listPinValue); Console.WriteLine(ret < 0 ? "get io value failed!" : "get io value succeed!"); if (ret < 0) { return; } int nSize = listPinName.Count; Console.WriteLine($"nSize: {listPinName.Count}"); gvIoList.Invoke((Action)(() => { gvIoList.Rows.Clear(); // 暂停 DataGridView 的绘制,防止闪烁 gvIoList.SuspendLayout(); // 创建行集合 List rows = new List(); for (int i = 0; i < nSize; i++) { DataGridViewRow row = new DataGridViewRow(); row.HeaderCell.Value = (i + 1).ToString(); row.Cells.Add(new DataGridViewTextBoxCell { Value = listPinName[i] }); row.Cells.Add(new DataGridViewTextBoxCell { Value = listPinValue[i] }); rows.Add(row); } // 批量添加行 gvIoList.Rows.AddRange(rows.ToArray()); // 恢复 DataGridView 的绘制 gvIoList.ResumeLayout(); })); } private void btnGetIo_Click(object sender, EventArgs e) { if (null == m_pIoCtrl) { Console.WriteLine("pIoCtrl is null"); return; } String sValue = ""; int ret = m_pIoCtrl.getIoValue(leIOname.Text, ref sValue); leIOvalue.Text = sValue.ToString(); Console.WriteLine(ret < 0 ? "get io value failed!" : "get io value succeed!"); if (ret < 0) { return; } Console.WriteLine($"sValue:{sValue}"); } private void btnSetIo_Click(object sender, EventArgs e) { if (null == m_pIoCtrl) { Console.WriteLine("pIoCtrl is null"); return; } Console.WriteLine($"Name : {leIOname.Text} Value : {leIOvalue.Text}"); var ret = m_pIoCtrl.setIoValue(leIOname.Text, leIOvalue.Text); Console.WriteLine(ret < 0 ? "set io value failed!" : "set io value succeed!"); } private void btnOpenFile_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "选择文件"; // 设置对话框的初始目录 openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // 设置对话框中允许选择的文件类型 openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"; // 设置是否允许选择多个文件 openFileDialog.Multiselect = false; // 如果用户点击了"打开"按钮 if (openFileDialog.ShowDialog() == DialogResult.OK) { // 获取用户选择的文件名 String sfilePath = openFileDialog.FileName; Console.WriteLine(sfilePath); try { // 使用StreamReader读取文件内容 using (StreamReader sr = new StreamReader(sfilePath)) { // 读取文件内容并将其写入RichTextBox String sfileContent = sr.ReadToEnd(); richTextBoxGCode.Text = sfileContent; } var ret = m_pMotion.downloadGcode(ref sfilePath); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("download gcode failed!"); } else { Console.WriteLine(ret); } ret = m_pMotion.openProgram(Path.GetFileName(sfilePath)); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("open program failed!"); } else { Console.WriteLine(ret); } } catch (Exception ex) { MessageBox.Show("文件读取失败: " + ex.Message); } } } private void showLineNo() { // 获得当前坐标信息 Point p = this.richTextBoxGCode.Location; int crntFirstIndex = this.richTextBoxGCode.GetCharIndexFromPosition(p); // 获取RichTextBox中当前位置的字符索引 int crntFirstLine = this.richTextBoxGCode.GetLineFromCharIndex(crntFirstIndex); // 获取当前位置所在的行号 Point crntFirstPos = this.richTextBoxGCode.GetPositionFromCharIndex(crntFirstIndex); // 获取当前位置的坐标信息 p.Y += this.richTextBoxGCode.Height; int crntLastIndex = this.richTextBoxGCode.GetCharIndexFromPosition(p); // 获取RichTextBox中底部位置的字符索引 int crntLastLine = this.richTextBoxGCode.GetLineFromCharIndex(crntLastIndex); // 获取底部位置所在的行号 Point crntLastPos = this.richTextBoxGCode.GetPositionFromCharIndex(crntLastIndex); // 获取底部位置的坐标信息 // 准备画图 Graphics g = this.pnl_ShowLine.CreateGraphics(); float zoomFactor = this.richTextBoxGCode.ZoomFactor; // 获取RichTextBox的缩放比例 Font font = new Font(this.richTextBoxGCode.Font.FontFamily, (this.richTextBoxGCode.Font.Size - 3) * zoomFactor, this.richTextBoxGCode.Font.Style); // 根据缩放比例调整字体大小 SolidBrush brush = new SolidBrush(Color.Green); // 刷新画布 Rectangle rect = this.pnl_ShowLine.ClientRectangle; brush.Color = this.pnl_ShowLine.BackColor; g.FillRectangle(brush, 0, 0, rect.Width, rect.Height); // 用背景色填充画布 brush.Color = Color.Black; // 重置画笔颜色,更改行号颜色 // 绘制行号 int lineSpace; if (crntFirstLine != crntLastLine) { lineSpace = (crntLastPos.Y - crntFirstPos.Y) / (crntLastLine - crntFirstLine); } else { lineSpace = Convert.ToInt32(font.Size); } int brushX = this.pnl_ShowLine.ClientRectangle.Width - Convert.ToInt32(font.Size * 1.5); // 更改为行号靠右显示 //int brushX = this.pnl_ShowLine.ClientRectangle.Width - Convert.ToInt32(font.Size * 3); // 更改为行号靠左显示 int brushY = crntLastPos.Y + Convert.ToInt32(font.Size * 0.35f); // 计算行号绘制的垂直位置(稍微向上偏移一点) for (int i = crntLastLine; i >= crntFirstLine; i--) { string lineNumber = (i + 1).ToString(); SizeF size = g.MeasureString(lineNumber, font); // 测量行号文本的大小 g.DrawString(lineNumber, font, brush, brushX - size.Width, brushY); // 靠右显示行号 //g.DrawString((i + 1).ToString(), font, brush, brushX, brushY);// 靠左显示行号 brushY -= lineSpace; // 更新垂直位置,准备绘制下一行的行号 } g.Dispose(); font.Dispose(); brush.Dispose(); } private void richTextBoxGCode_TextChanged(object sender, EventArgs e) { showLineNo(); } //控件滚动事件,当算出的行数大于本控件长度 private void richTextBoxGCode_VScroll(object sender, EventArgs e) { showLineNo(); } private void richTextBoxGCode_ContentsResized(object sender, ContentsResizedEventArgs e) { showLineNo(); } private void btnHomeAll_Click(object sender, EventArgs e) { if (null == m_pMotion) { Console.WriteLine("pMotion is null"); return; } var nAxis = new int[] { 'X', 'Y', 'Z', 'A', 'C' }; for (int i = 0; i < nAxis.Length; ++i) { var ret = m_pMotion.home(i); if (WECONCNC_ERROR_E.WECONCNC_ERROR_SUCCESS != ret) { Console.WriteLine("home failed!"); return; } else { Console.WriteLine(ret); } } } } }