我試圖通過port.Write("g28");
行將gcode g28
發送到我的RepRap 3D打印機。將正確的Gcode字符串發送到串行端口?
我的程序連接到正確的串行端口,但是當我嘗試發送信息作爲字符串訪問com端口被拒絕。 這很奇怪,因爲在將Gcode發送給它之前,串口是打開的。它甚至發回了一些數據。那裏有什麼問題,我如何解決它?
以下是我正在使用的代碼行。 gcode命令列表可在此page上獲得。
我曾嘗試在字符串的末尾添加"\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
@sawdust在將Gcode發送到串行端口之前,我剛更新了這個問題。非常感謝你。 –
*「port.Write(」g28「)」* - 「g28」甚至未被列爲有效操作;你想要做什麼手術? 「G」或「M」必須大寫,並且您需要用「\ n」來終止該行。 *「它甚至發回了一些數據。」* - 那個消息是什麼? – sawdust