views:

625

answers:

2

Hi, I used pole display(E POS) in my POS c# application.I have two major problem in that, 1. I can't clear the display perfectly. 2. I can't set the cursor position.

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

Code :-

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);
    }

}
A: 

Your problem is that you are calling Write to clear line 1, and WriteLine to clear line 2.

This doesn't make any sense. The only difference between the methods is that WriteLine adds a linebreak to the end. All you are really doing is this outputting this string:

  "                                  "\r\n

Without knowing the brand of the pole display you are using, I can't tell you the proper way to do it, but the way you are trying to do it will never work. Most terminals accept special character codes to move the cursor, or clear the display. Have you found a reference for the terminal you are working with? Most displays will clear if you send them CHR(12).

All that aside, there is a major problem with your class design. You should never rely on destructors to free resources in C#.

In C#, the destructor will be called when the garbage collector collects the object, so there is no deterministic way to know when the resource (in this case a Com port), will be collected and closed.

Instead, implement the interface IDisposable on your class.

This requires you to add a Dispose method to your class. This would serve the same purpose as your current destructor.

By doing that, you can utilize a built in language feature in C# to release your resources when the object goes out of scope.

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

Thank you... The Pole Display I used "EPOS". This display have two line with each line have 20 character. Thats Why I used write- 1st line and writeline- 2nd line. I don't have any manual for that device. I heard that some character code used to clear and focus the device. But I don't know which are they.

Anyway I ll try with code what u sent me.

I can accept the suggested code for disposing the port.

Anoop