日期:2014-05-20  浏览次数:20851 次

高分求教~!!!如何控制并口打印机!!!!!!(C#)
并口通讯需要一个inpout32.DLL
在程序中引用
class   IO
{

  [DllImport( "inpout32.dll ",   EntryPoint   =   "Out32 ")]
    public   static   extern   void   Output(int   adress,   int   value);

  [DllImport( "inpout32.dll ",   EntryPoint   =   "Inp32 ")]
    public   static   extern   int   Input(int   adress);  
}

现在想实现向并口传送指令,让打印机切纸。走纸一类的操作
但是我现在传送指令总是没放映
代码如下:
IO.Output(0x378,   0x1B6400);     //第一个参数传递LPT1地址,第二个为指令
0x1B6400指令为切纸,但每次都没反应。
有人写过类似的DEMO么。
感激不尽~




------解决方案--------------------
private void SendCommand(string sCmd)
{
SerialPort sp = null;
try
{
m.WaitOne();
sp = new SerialPort(sPort);
sp.BaudRate = 9600;
sp.Open();
sp.WriteTimeout = 3000;
sp.WriteLine(sCmd);
sp.Close();
m.ReleaseMutex();
}
catch (Exception e)
{
if (sp != null) sp.Close();
m.ReleaseMutex();
throw e;
}
}


/// <summary>
/// Print String
/// </summary>
/// <param name= "sPrint "> </param>
public void PrintString(string sPrint)
{
StringBuilder sb = new StringBuilder();
sb.Append((char)10);
sb.Append(sPrint);
this.SendCommand(sb.ToString());
}

/// <summary>
/// Full Cut
/// </summary>
public void FullCut()
{
StringBuilder sb = new StringBuilder();
sb.Append((char)29);
sb.Append((char)86);
sb.Append(66);
sb.Append(0);
this.SendCommand(sb.ToString());
}

/// <summary>
/// Half Cut
/// </summary>
public void HalfCut()
{
StringBuilder sb = new StringBuilder();
sb.Append((char)10);
sb.Append((char)10);
sb.Append((char)10);
sb.Append((char)10);
sb.Append((char)27);
sb.Append((char)109);
this.SendCommand(sb.ToString());
}