我正在尝试使用 C#(在 Mac 10.8 下运行 Mono 3.2)通过 FTDI Basic 板在我的笔记本电脑和热敏打印机(从 Sparkfun 购买)之间建立链接。我一直在使用来自以下位置的 .net 库:
http://electronicfields.wordpress.com/2011/09/29/thermal-printer-dot-net/
https://github.com/yukimizake/ThermalDotNet
该代码似乎没有任何错误(我已经更改了串行端口和波特率以匹配我的设置)并且它似乎在终端上运行了整个程序。但是,它无法与打印机交互,因此没有打印任何内容。
这是我正在使用的确切代码:
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.IO.Ports;
//using System.Collections.Generic;
//using System.Drawing;
using ThermalDotNet;
using Microsoft.SPOT;
namespace ThermalPrinterTestApp
{
class PrinterClass
{
SerialPort printerPort;
ThermalPrinter printer;
public PrinterClass(string printerPortName = "/dev/tty.usbserial-AD025HP0")
{
//Serial port init
printerPort = new SerialPort(printerPortName, 19200);
if (printerPort != null)
{
Debug.Print("Port ok");
if (printerPort.IsOpen)
{
printerPort.Close();
}
}
Debug.Print("Opening port");
try
{
printerPort.Open();
}
catch
{
Debug.Print("I/O error");
//Environment.Exit(0);
}
//Printer init
printer = new ThermalPrinter(printerPort, 9, 110, 10);
printer.Reset();
}
public void TestBarcode()
{
printer.WakeUp();
ThermalPrinter.BarcodeType myType = ThermalPrinter.BarcodeType.ean13;
string myData = "3350030103392";
printer.SetBarcodeLeftSpace(25);
printer.WriteLine(myType.ToString() + ", data: " + myData);
printer.SetLargeBarcode(true);
printer.LineFeed();
printer.PrintBarcode(myType,myData);
printer.LineFeed(2);
}
/*
static void TestImage(ThermalPrinter printer)
{
printer.WriteLine("Test image:");
Bitmap img = new Bitmap("../../../mono-logo.png");
printer.LineFeed();
printer.PrintImage(img);
printer.LineFeed();
printer.WriteLine("Image OK");
}*/
public void PrintTest()
{
printer.WakeUp();
Debug.Print(printer.ToString());
//System.Threading.Thread.Sleep(5000);
printer.SetBarcodeLeftSpace(25);
TestBarcode();
printer.LineFeed(3);
//System.Threading.Thread.Sleep(5000);
//TestImage();
//System.Threading.Thread.Sleep(5000);
printer.WriteLineSleepTimeMs = 200;
printer.WriteLine("Default style");
printer.WriteLine("PrintingStyle.Bold",ThermalPrinter.PrintingStyle.Bold);
printer.WriteLine("PrintingStyle.DeleteLine",ThermalPrinter.PrintingStyle.DeleteLine);
printer.WriteLine("PrintingStyle.DoubleHeight",ThermalPrinter.PrintingStyle.DoubleHeight);
printer.WriteLine("PrintingStyle.DoubleWidth",ThermalPrinter.PrintingStyle.DoubleWidth);
printer.WriteLine("PrintingStyle.Reverse",ThermalPrinter.PrintingStyle.Reverse);
printer.WriteLine("PrintingStyle.Underline",ThermalPrinter.PrintingStyle.Underline);
printer.WriteLine("PrintingStyle.Updown",ThermalPrinter.PrintingStyle.Updown);
printer.WriteLine("PrintingStyle.ThickUnderline",ThermalPrinter.PrintingStyle.ThickUnderline);
printer.SetAlignCenter();
printer.WriteLine("BIG TEXT!",((byte)ThermalPrinter.PrintingStyle.Bold +
(byte)ThermalPrinter.PrintingStyle.DoubleHeight +
(byte)ThermalPrinter.PrintingStyle.DoubleWidth));
printer.SetAlignLeft();
printer.WriteLine("Default style again");
printer.LineFeed(3);
printer.Sleep();
}
}
}
这是我运行程序后得到的终端日志:
Port ok
Opening port
ThermalPrinter:
_serialPort=/dev/tty.usbserial-AD025HP0,
_maxPrintingDots=2,
_heatingTime=180,
_heatingInterval=2,
PictureLineSleepTimeMs=40,
WriteLineSleepTimeMs=0,
Encoding=ibm850
Printer is now offline.
Press any key to continue...
任何想法是什么问题?
有几点需要注意:
- 打印机已经能够打印出样本,因此它似乎可以正常工作。
- 当我播放该程序时,我注意到在 FTDI 上只有 TX(发送?)亮起,而 RX(接收?)保持不亮。我检查了接线,一切似乎都井井有条,所以不确定是否有任何问题(我附上了图像)[编辑:图像的代表点不足!]
- 我也尝试使用 Arduino 作为比较,但有类似的错误(调试正常但没有交互)
- 我是一个初学者,所以为过度简化或重大疏忽道歉!
谢谢, 芬恩