2012-10-06 32 views
0

我得到以下兩個錯誤與下面的代碼放在同一行port.WriteLine(gcode);,做我需要創建一個for循環列表中的每一個項目?SerialPort.WriteLine(串)具有一些無效參數,不能轉換參數字符串

關於「System.IO.Ports.SerialPort.WriteLine(字符串)」最好的重載的方法匹配具有一些無效參數

參數1:不能從「System.Collections.Generic.List」轉換到'串'

代碼:

//Fields 
SerialPort port; 
string myReceivedLines; 

protected override void SolveInstance(IGH_DataAccess DA) 
{ 
    List<string> gcode = new List<string>(); 
    DA.GetDataList(0, gcode); 

    if (!DA.GetDataList(0, gcode)) 
     return; 

    port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); 
    port.DtrEnable = true; 
    port.Open();    
    port.DataReceived += this.portdatareceived; 

    if (gcode == null) 
    { 
     AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Specify a valid GCode"); 
     return; 
    } 

    if (connecttodevice == true) 
    { 
     DA.SetDataList(0, myReceivedLines); 
    } 

    if (sendtoprint == true) 
    { 
     port.WriteLine(gcode); 
    } 
} 

private void portdatareceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
{ 
    myReceivedLines = port.ReadExisting(); 
} 

回答

1
foreach(string s in gcode) 
{ 
    port.WriteLine(s); 
} 

的SerialPort知道如何編寫字符串,但沒有Seri​​alPort方法接受List<string>

編輯。 試試這個:

StringBuilder sb = new StringBuilder(); 
foreach(string s in gcode) 
{ 
    sb.Append(s) 
} 
port.WriteLine(sb.ToString()); 
+0

非常感謝@AlexFarber你可以解決它,用在這種情況下,每個循環的問題是,它輸出的單獨的行每個字符,有沒有辦法在一行上保留句子? –

+1

使用SerialPort.Write - 它不會追加新行。 –

+0

嗨,亞歷克斯,我嘗試使用相同的循環SerialPort.Write,但它不遺憾地改變結果。 –

1
if (sendtoprint == true) 
{ 
    for (int i = 0; i < gcode.Count(); i++) 
    { 
     port.WriteLine(gcode[i]); 
    } 
} 

像你說的

相關問題