2008-12-25 19 views
0

我在我的POS c#應用程序中使用了極點顯示(E POS)。我有兩個主要問題, 1.我無法完美地清除顯示。 2.我無法設置光標位置。c中的極點顯示問題#

 I used some dirty tricks to do these.But I am not satisfied with that code.The following code i used. 

代碼: -

class PoleDisplay : SerialPort 
{ 
    private SerialPort srPort = null; 

    public PoleDisplay() 
    { 
     try 
     { 
      srPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); 
      if (!srPort.IsOpen) srPort.Open(); 
     } 
     catch { } 
    } 

    ~PoleDisplay() 
    { 
     srPort.Close(); 
    } 

    //To clear Display..... 
    public void ClearDisplay() 
    { 
     srPort.Write("     "); 
     srPort.WriteLine("     "); 

    } 

    //Display Function 
    //'line' 1 for First line and 0 For second line 
    public void Display(string textToDisplay, int line) 
    { 
     if (line == 0) 
      srPort.Write(textToDisplay); 
     else 
      srPort.WriteLine(textToDisplay); 
    } 

} 
+0

poledisplay-C#標籤是太具體,加入C#和EPOS標籤。 – stukelly 2009-03-01 01:40:17

回答

0

你的問題是,你在呼喚寫操作清除1號線,和WriteLine清除線路2

這沒有任何意義。這兩種方法之間的唯一區別是WriteLine將換行添加到最後。所有你真的做的是這樣的輸出字符串:

"         "\r\n 

不知道你的品牌所使用的杆顯示器的,我不能告訴你正確的方式做到這一點,但您要的方式做它永遠不會工作。大多數終端接受特殊字符代碼來移動光標或清除顯示。你有沒有找到你正在使用的終端的參考?如果您將CHR(12)發送給他們,大多數顯示器都會清除。

除此之外,課堂設計中存在一個主要問題。你不應該依賴析構函數來釋放C#中的資源。

在C#中,當垃圾收集器收集對象時會調用析構函數,所以沒有確定性的方式來知道資源(在這種情況下是一個Com端口)何時會被收集和關閉。

而是在您的類上實現接口IDisposable。

這需要您爲您的課程添加Dispose方法。這與您當前的析構函數具有相同的用途。

通過這樣做,您可以利用C#中內置的語言功能在對象超出範圍時釋放資源。

using (PoleDisplay p = new PoleDisplay()) 
{ 
    // Do Stuff with p 
} 
// When the focus has left the using block, Dispose() will be called on p. 
0

發送十六進制代碼0℃至清屏,它適用於大多數顯示器

這裏是一個代碼示例:

byte[] bytesToSend = new byte[1] { 0x0C }; // send hex code 0C to clear screen 
srPort.Write(bytesToSend, 0, 1);