发送正确的G代码字符串到串行端口? [英] Sending the right Gcode string to a Serial Port?

查看:711
本文介绍了发送正确的G代码字符串到串行端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送G代码 G28 来我通过线 port.Write(G28)的RepRap三维打印机;



我的程序连接到但是当我尝试发送的信息作为字符串访问COM端口的串口被拒绝。 这是奇怪的,因为串口被发送到G代码之前打开。它甚至还发送了一些数据回来。有什么问题,我怎么能解决?



下面是我使用的代码行。 G代码的命令列表可在此



我曾尝试添加到字符串末尾的\\\
,但没有奏效。

  //字段
名单,LT;字符串> myReceivedLines =新的List<串GT;();


//用户为port.DataReceived事件
私人无效DataReceivedHandler(对象发件人,System.IO.Ports.SerialDataReceivedEventArgs E)
方法{
的SerialPort SP =(的SerialPort)发送;
,而(sp.BytesToRead大于0)
{

{
myReceivedLines.Add(sp.ReadLine());
}
赶上(TimeoutException异常)
{
中断;
}
}
}


保护覆盖无效SolveInstance(IGH_DataAccess DA)
{

串selectedportname =默认值(字符串);
DA.GetData(1,标记selectedportname);
INT selectedbaudrate =默认值(INT);
DA.GetData(2,参考selectedbaudrate);
布尔connecttodevice =默认值(布尔);
DA.GetData(3参connecttodevice);
布尔=首页所有默认值(布尔);
DA.GetData(5,楼盘首页所有);

的SerialPort端口=新的SerialPort(selectedportname,selectedbaudrate,Parity.None,8,StopBits.One);

port.DtrEnable = TRUE;
port.Open();

如果(connecttodevice ==真)
{
port.DataReceived + =新SerialDataReceivedEventHandler(DataReceivedHandler);
DA.SetDataList(0,myReceivedLines);
}

如果(==首页所有真)
{
port.Write(G28);
}

}


解决方案

通常当我创建一个COM端口它是一个类级的对象,我在启动过程中初始化一次。我将你的初始化代码你的方法,进入构造或者您可以在启动时调用一次的方法。这应该阻止你试图打开它一次以上,仍然把它提供给接收数据作出反应。






我我不知道你的架构的它看起来就像你可能计划有多个端口,但
回答在评论你的问题,你正在做创建的SerialPort一切。在最低限度对于SerialPort声明应该是一个类级别的变量。 。然后,您可以检查,看看它是什么,打造它的第一次。



 字符串selectedportname =默认值(字符串); 
DA.GetData(1,标记selectedportname);
INT selectedbaudrate =默认值(INT);
DA.GetData(2,参考selectedbaudrate);
布尔connecttodevice =默认值(布尔);
DA.GetData(3参connecttodevice);

的SerialPort端口=新的SerialPort(selectedportname,selectedbaudrate,Parity.None,8,StopBits.One);
port.DataReceived + =新SerialDataReceivedEventHandler(DataReceivedHandler);

DA.SetDataList(0,myReceivedLines);

port.Open(); //可选我通常在我初始化的时候打开它。您可以检查`port.IsOpen()`
//并打开它,如果它是假的。



与职业等级的SerialPort尝试是这样的第二个选项:

 保护覆盖无效SolveInstance(IGH_DataAccess DA)
{
如果(端口== NULL)
{
串selectedportname =默认值(字符串);
DA.GetData(1,标记selectedportname);
INT selectedbaudrate =默认值(INT);
DA.GetData(2,参考selectedbaudrate);
布尔connecttodevice =默认值(布尔);
DA.GetData(3参connecttodevice);
端口=新的SerialPort(selectedportname,selectedbaudrate,Parity.None,8,StopBits.One);
如果(connecttodevice ==真)
{
port.DataReceived + =新SerialDataReceivedEventHandler(DataReceivedHandler);
DA.SetDataList(0,myReceivedLines);
}

port.DtrEnable = TRUE;
port.Open();
}


布尔=首页所有默认值(布尔);
DA.GetData(5,楼盘首页所有);

如果(==首页所有真)
{
port.Write(G28);
}

}


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

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?

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 "\n" 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");
        }

    }

解决方案

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.


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.

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");
    }

}   

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆