Pole display problem in C #

I have used pole mapping (E POS) in a C # POS application. I have two main problems: 1. I cannot completely clear the display. 2. I cannot 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);
    }

}  

      

0


source to share


2 answers


Your problem is that you call Write to clear line 1 and WriteLine to clear line 2.

It doesn't make any sense. The only difference between these methods is that WriteLine appends the line to the end. All you really do is the output of this line:

  "                                  "\r\n

      

Without knowing the brand of display pole you are using, I cannot tell you how to do this, 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. Did you find a link for the terminal you are working with? Most displays will be cleared if you send CHR (12) to them.

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



In C #, the destructor is called when the object is garbage collected, so there is no deterministic way of knowing when a resource (in this case, a COM port) will be collected and closed.

Instead, implement the IDisposable interface in your class.

To do this, you need to add a Dispose method to your class. This will serve the same purpose as your current destructor.

Thus, you can use a built-in language function in C # to release resources when an 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.

      

0


source


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

here is some sample code:



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

      

0


source







All Articles