C#中的极点显示问题

发布于 2024-07-11 00:13:42 字数 1038 浏览 7 评论 0原文

我在我的 POS c# 应用程序中使用了杆显示(E POS)。我有两个主要问题, 1.我无法完美清除显示。 2. 无法设置光标位置。

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

}  

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

}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-07-18 00:13:42

您的问题是您正在调用 Write 来清除第 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.

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.
断爱 2024-07-18 00:13:42

发送十六进制代码 0C 来清除屏幕,它适用于大多数显示器,

这里是代码示例:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文