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

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

本文介绍了将正确的 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 字符串发送到串行端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将正确的 Gcode 字符串发送到串行端口?

基础教程推荐