C# SerialPort WriteLine 可以工作但不能写入

标签 c# .net printing serial-port

我正在尝试使用简单的写入命令将字符串写入 Com5 上的内部打印机。 WriteLine 方法打印效果很好,但 Write 方法却不行。下面是我调用的代码。它只是调用 .NET 函数的简单包装函数。

  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 将在字符串中追加一个新行字符。也许这会“刷新”缓冲区以使打印机响应输入。

关于C# SerialPort WriteLine 可以工作但不能写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17683103/

相关文章:

c# - 尝试从 Visual Studio 2013 update 4 发布 azure webjob

c# - 如何在构造函数中使用 AutoMapper 映射到 "this"

C# find函数问题(不能高亮)

c# - 为什么这段代码消耗越来越多的内存?

c# - Silverlight、Wpf Web App (xbap) 还是单击一次?优点和缺点

c# - 修改每个请求的请求 header C# HttpClient PCL

html - 如何根据溢出元素的宽度将内容居中?

java - 打印不同长度的数组

c# - 在 C# 中实现 SNMP Reporter(代理)(必须在 Mono 上运行)?

python-3.x - python : Printing a list with a line break after every nth item