C#中的极点显示问题
我在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是您正在调用 Write 来清除第 1 行,并调用 WriteLine 来清除第 2 行。
这没有任何意义。 这些方法之间的唯一区别是 WriteLine 在末尾添加了换行符。 您真正要做的就是输出这个字符串:
在不知道您正在使用的杆式显示器的品牌的情况下,我无法告诉您正确的方法,但您尝试的方法永远不会起作用。 大多数终端接受特殊字符代码来移动光标或清除显示。 您是否找到了您正在使用的终端的参考资料? 如果您发送 CHR(12),大多数显示都会清除。
除此之外,您的类设计存在一个重大问题。 在 C# 中,永远不应该依赖析构函数来释放资源。
在 C# 中,当垃圾收集器收集对象时,将调用析构函数,因此没有确定的方法来知道资源(在本例中为 Com 端口)何时将被收集和关闭。
相反,在您的类上实现 IDisposable 接口。
这需要您向类中添加 Dispose 方法。 这与您当前的析构函数具有相同的用途。
通过这样做,您可以利用 C# 中的内置语言功能在对象超出范围时释放资源。
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:
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.
发送十六进制代码 0C 来清除屏幕,它适用于大多数显示器,
这里是代码示例:
Send hex code 0C to clear the screen, it works for most displays
here is a code sample: