<i id='zvrZb'><tr id='zvrZb'><dt id='zvrZb'><q id='zvrZb'><span id='zvrZb'><b id='zvrZb'><form id='zvrZb'><ins id='zvrZb'></ins><ul id='zvrZb'></ul><sub id='zvrZb'></sub></form><legend id='zvrZb'></legend><bdo id='zvrZb'><pre id='zvrZb'><center id='zvrZb'></center></pre></bdo></b><th id='zvrZb'></th></span></q></dt></tr></i><div id='zvrZb'><tfoot id='zvrZb'></tfoot><dl id='zvrZb'><fieldset id='zvrZb'></fieldset></dl></div>
  • <tfoot id='zvrZb'></tfoot>

      <small id='zvrZb'></small><noframes id='zvrZb'>

      <legend id='zvrZb'><style id='zvrZb'><dir id='zvrZb'><q id='zvrZb'></q></dir></style></legend>

        <bdo id='zvrZb'></bdo><ul id='zvrZb'></ul>

        将正确的 Gcode 字符串发送到串行端口?

        Sending the right Gcode string to a Serial Port?(将正确的 Gcode 字符串发送到串行端口?)

          <bdo id='MRjRc'></bdo><ul id='MRjRc'></ul>
            <tfoot id='MRjRc'></tfoot>

              <tbody id='MRjRc'></tbody>
          • <legend id='MRjRc'><style id='MRjRc'><dir id='MRjRc'><q id='MRjRc'></q></dir></style></legend>
          • <small id='MRjRc'></small><noframes id='MRjRc'>

                  <i id='MRjRc'><tr id='MRjRc'><dt id='MRjRc'><q id='MRjRc'><span id='MRjRc'><b id='MRjRc'><form id='MRjRc'><ins id='MRjRc'></ins><ul id='MRjRc'></ul><sub id='MRjRc'></sub></form><legend id='MRjRc'></legend><bdo id='MRjRc'><pre id='MRjRc'><center id='MRjRc'></center></pre></bdo></b><th id='MRjRc'></th></span></q></dt></tr></i><div id='MRjRc'><tfoot id='MRjRc'></tfoot><dl id='MRjRc'><fieldset id='MRjRc'></fieldset></dl></div>
                  本文介绍了将正确的 Gcode 字符串发送到串行端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过 port.Write("g28"); 行将 gcode g28 发送到我的 RepRap 3D 打印机.

                  I am trying to send the gcode g28 to my RepRap 3D printer through the line port.Write("g28");.

                  我的程序连接到正确的串行端口,但是当我尝试将信息作为字符串发送时,对 com 端口的访问被拒绝.这很奇怪,因为在向其发送 Gcode 之前,串口是打开的. 它甚至还发送了一些数据.那里有什么问题,我该如何解决?

                  My program connects to the right serial port however when I try sending the information as string the access to the com port gets denied. This is strange because the Serial port was open before sending the Gcode to it. It even sent some data back. What is the problem there and how could i fix it?

                  以下是我正在使用的代码行.此页面上提供了 gcode 命令列表.

                  Below are the lines of code that I am using. The list of gcode commands are available on this page.

                  我尝试在字符串末尾添加 " " ,但没有成功.

                  I have tried adding a " " at the end of the string but it did not work.

                      //Fields
                      List<string> myReceivedLines = new List<string>();
                  
                  
                      //subscriber method for the port.DataReceived Event
                      private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
                      {
                          SerialPort sp = (SerialPort)sender;
                          while (sp.BytesToRead > 0)
                          {
                              try
                              {
                                  myReceivedLines.Add(sp.ReadLine());
                              }
                              catch (TimeoutException)
                              {
                                  break;
                              }
                          }
                      }
                  
                  
                      protected override void SolveInstance(IGH_DataAccess DA)
                      {
                  
                          string selectedportname = default(string);
                          DA.GetData(1, ref selectedportname);
                          int selectedbaudrate = default(int);
                          DA.GetData(2, ref selectedbaudrate);
                          bool connecttodevice = default(bool);
                          DA.GetData(3, ref connecttodevice);
                          bool homeall = default(bool);
                          DA.GetData(5, ref homeall);
                  
                          SerialPort port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); 
                  
                          port.DtrEnable = true;   
                          port.Open();             
                  
                          if (connecttodevice == true)
                          {
                              port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                              DA.SetDataList(0, myReceivedLines);
                          }
                  
                          if (homeall == true)
                          {
                              port.Write("g28");
                          }
                  
                      }
                  

                  推荐答案

                  通常当我创建一个 Com Port 时,它是一个 Class 级别的对象,我在启动期间初始化一次.我会将您的初始化代码从您的方法中移出并放入您可以在启动期间调用一次的构造函数或方法中.这应该可以防止您尝试多次打开它,但仍然可以让它响应 Received 数据.

                  Usually when I create a Com Port it is a Class level object that I initialize once during startup. I would move your Initialization Code out of your method and into the Constructor or a Method that you can call once during your startup. That should prevent you from trying to open it more than once and still leave it available to respond to Received data.

                  我不确定您的架构,看起来您可能计划拥有多个端口,但在评论中回答您的问题,您为创建 SerialPort 所做的一切.SerialPort 的声明至少应该是一个类级别的变量.然后,您可以检查它是否为空,并在第一次创建它.

                  I am not sure of your architecture it looks like you may be planning to have multiple ports but to answer your question in the comments, everything that you are doing to create the SerialPort. At the bare minimum the declaration for the SerialPort should be a Class Level variable. You could then check to see if it was nothing and create it the first time.

                  string selectedportname = default(string); 
                  DA.GetData(1, ref selectedportname); 
                  int selectedbaudrate = default(int); 
                  DA.GetData(2, ref selectedbaudrate); 
                  bool connecttodevice = default(bool); 
                  DA.GetData(3, ref connecttodevice); 
                  
                  SerialPort port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);   
                  port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                  
                  DA.SetDataList(0, myReceivedLines);   
                  
                  port.Open();  // optional  I usually open it at the time that I initialize it. you can check `port.IsOpen()`
                                // and Open it if it is false.
                  

                  类级别 SerialPort 的第二个选项试试这样:

                  second option with a Class Level SerialPort Try something like this:

                  protected override void SolveInstance(IGH_DataAccess DA)
                  {
                      if(port == null)
                      {
                          string selectedportname = default(string);
                          DA.GetData(1, ref selectedportname);
                          int selectedbaudrate = default(int);
                          DA.GetData(2, ref selectedbaudrate);
                          bool connecttodevice = default(bool);
                          DA.GetData(3, ref connecttodevice);
                          port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);
                          if (connecttodevice == true)
                          {
                              port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                              DA.SetDataList(0, myReceivedLines);
                          }
                  
                          port.DtrEnable = true;
                          port.Open();
                      }
                  
                  
                      bool homeall = default(bool);
                      DA.GetData(5, ref homeall);
                  
                      if (homeall == true)
                      {
                          port.Write("g28");
                      }
                  
                  }   
                  

                  这篇关于将正确的 Gcode 字符串发送到串行端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                    <legend id='reoT7'><style id='reoT7'><dir id='reoT7'><q id='reoT7'></q></dir></style></legend>

                  • <small id='reoT7'></small><noframes id='reoT7'>

                      <tbody id='reoT7'></tbody>
                  • <i id='reoT7'><tr id='reoT7'><dt id='reoT7'><q id='reoT7'><span id='reoT7'><b id='reoT7'><form id='reoT7'><ins id='reoT7'></ins><ul id='reoT7'></ul><sub id='reoT7'></sub></form><legend id='reoT7'></legend><bdo id='reoT7'><pre id='reoT7'><center id='reoT7'></center></pre></bdo></b><th id='reoT7'></th></span></q></dt></tr></i><div id='reoT7'><tfoot id='reoT7'></tfoot><dl id='reoT7'><fieldset id='reoT7'></fieldset></dl></div>
                            <bdo id='reoT7'></bdo><ul id='reoT7'></ul>
                            <tfoot id='reoT7'></tfoot>