Question

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

}  
Was it helpful?

Solution

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.

OTHER TIPS

Send hex code 0C to clear the screen, it works for most displays

here is a code sample:

byte[] bytesToSend = new byte[1] { 0x0C }; // send hex code 0C to clear screen
srPort.Write(bytesToSend, 0, 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top