C#SerialPort WriteLine工作但不写

时间:2013-07-16 17:21:33

标签: 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();
  }

3 个答案:

答案 0 :(得分:1)

WriteLine会在字符串中添加换行符。也许这会“刷新”缓冲区以使打印机响应输入。

答案 1 :(得分:1)

你是如何发现它的写作?我的意思是,如果另一端是一个应用程序听力?那么它在寻找CrLf吗?你可以在字符串中添加New Line并将其发送到Write并查看吗?如果有效?除了新行之外,Coz writeline和write都应该类似。

答案 2 :(得分:0)

SerialPort对象使用Stream对象在较低级别进行读写。可通过酒店BaseStream访问。 也许如果在Write指令之前刷新流,它将强制在流中进行更新。

...
//write the text to the printer
com5.Write(printString);
com5.BaseStream.Flush();
Thread.Sleep(100);
...

艾丽西亚。