【问题标题】:Sending the right Gcode string to a Serial Port?将正确的 Gcode 字符串发送到串行端口?
【发布时间】:2012-10-14 21:46:28
【问题描述】:

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

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

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

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

    //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 端口的访问被拒绝,但之前不是。” - 之前是什么?你试过大写(又名大写)“G”吗?
  • @sawdust 在将 Gcode 发送到串行端口之前,我刚刚更新了问题。非常感谢。
  • "port.Write("g28")" - "g28" 甚至没有被列为有效操作;你想做什么操作? “G”或“M”必须大写,并且您确实需要用“\n”终止该行。 “它甚至发回了一些数据。” - 那是什么信息?
  • 我假设 SolveInstance 只被调用一次,否则端口将被第一次调用时创建的 Com 端口锁定。
  • @ArthurMamou-Mani 尝试将 port.Close() 放在方法的末尾。

标签: c# serial-port grasshopper g-code


【解决方案1】:

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


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

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 的第二个选项试试这样:

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

}   

【讨论】:

  • 非常感谢,你会在上面的代码中的Initialization Code 中包含什么?
  • 对不起,如果我从 SolveInstance 中取出所有 if 语句,我将如何从我的程序中访问它们,因为 SolveInstance() 方法是我可以为其分配数据的地方?非常感谢。
  • @ArthurMamou-Mani 查看第二个选项,主要是避免创建多个 SerialPort。
  • 非常感谢@MarkHall,使用第二个选项打开HomeAll 时出现错误:Error: Solution exception: A device attached to the system is not functioning 然后当我打开其他命令并且应该连接端口时,我获取:Error:Solution exception: The port is closed. 当所有命令为假时,错误消失。我只想连接到一台设备。我不再收到 access denied 消息,即使多次关闭和打开 ConnecttoDevice 按钮也是一件好事。
  • 更新:感谢您的回复,我设法发送了 gcode,并且机器以正确的方式做出了反应(所以这是正确的答案),但我不再收到反馈。我会努力的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多