我正在嘗試使用簡單寫入命令將字符串寫入Com5上的內部打印機。 WriteLine方法打印出來很好,但Write不會。以下是我打電話的代碼。它只是調用.NET函數的簡單包裝函數。C#SerialPort WriteLine有效,但不能寫
public static void Write(string printString)
{
//intialize the com port
SerialPort com5 = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
com5.Handshake = Handshake.XOnXOff;
com5.Open();
//write the text to the printer
com5.Write(printString);
Thread.Sleep(100);
com5.Close();
}
public static void WriteLine(string printString)
{
//intialize the com port
SerialPort com5 = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
com5.Handshake = Handshake.XOnXOff;
com5.Open();
//write the text to the printer
com5.WriteLine(printString);
Thread.Sleep(100);
com5.Close();
}
爲什麼'WriteLine()'對你不好?否則我不知道你的問題是什麼。 – CSJ
是否有可能打印機正在緩衝,直到有完整的一行?如果在'Write'之後調用'WriteLine',會發生什麼? –
您需要避免重複打開和關閉端口。對於SerialPort.Close()的MSDN文章特別警告這一點。在代碼中避免非常危險的Thread.Sleep()也是一個好方法。 –