2

基本上我需要与一个COM serial printer. 有 2 个计时器,一个会一次打印一张票,另一个会挂机检查错误并在错误消失后恢复打印。

例如:打印机正在打印,但如果我按下暂停按钮,一个错误(在缓冲区中读取)将使一些布尔值变为 1,在我读取它们之后,我能够判断错误是什么并停止打印。

我的问题出ErrorTimer_Elapsed()在计时器已用事件的方法中,并且可能与 ErrorCheck 和 SendToCom (这是打印方法)相关联。问题是程序可以很好地检测错误,但是当ErrorTimer_Elapsed()我尝试重复检查错误状态更改时,我会收到如下 IO 错误:

-无法访问 COM PORT -您需要的资源正在使用中

等等。

你能帮助我吗?谢谢,这是代码(大约 180 行),从 3 天以来我一直在努力解决这个问题 :( :

using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {

        #region Variabili di istanza

        private ArrayList _filesToPrint = new ArrayList();  
        private static SerialPort _serialPort1 = new SerialPort();  
        private string _portName = ConfigurationSettings.AppSettings["COM"];    
        private string FilesPath = ConfigurationSettings.AppSettings["Path"];   
        private System.Timers.Timer PrintTimer = new System.Timers.Timer(2000);
        private System.Timers.Timer ErrorTimer = new System.Timers.Timer(3500); 
        private bool PrintTimerIsActive = true; 
        private string HsCOMerrorStrings, PrintErrors;  

        #endregion

        public Form1() 
        {
            _serialPort1.PortName = _portName; 
            _serialPort1.ReadTimeout = 1000;
            _IfFileExistsPrint();
        }

        private void _IfFileExistsPrint()
        {
            foreach (string file in Directory.GetFiles(FilesPath))
                _filesToPrint.Add(file); 

            FileSystemWatcher SysWatch = new FileSystemWatcher(FilesPath, "*.*");
            SysWatch.Created += new FileSystemEventHandler(SysWatch_Created);
            SysWatch.EnableRaisingEvents = true;

            PrintTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
            PrintTimer.Start();

            ErrorTimer.Elapsed += new System.Timers.ElapsedEventHandler(ErrorTimer_Elapsed);
        }

        private void SysWatch_Created(object Sender, FileSystemEventArgs w)
        {
            _filesToPrint.Add(w.FullPath);
        }


        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (_filesToPrint.Count > 0 && PrintTimerIsActive)
            {
                PrintTimer.Stop();  
                SendFilesToCom(_filesToPrint[0].ToString());
                MessageBox.Show("Printing file :" + _filesToPrint[0].ToString());
                File.Delete(_filesToPrint[0].ToString());   
                _filesToPrint.RemoveAt(0);  
                _filesToPrint.TrimToSize(); 
                Console.Write(HsCOMerrorStrings);
                PrintTimer.Start(); 

                PrintErrors = ErrorCheck(HsCOMerrorStrings);
            }
        }

        private void ErrorTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs d)
        {
            if (!_serialPort1.IsOpen)
            {
                try
                {
                    _serialPort1.Open();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Data);
                }
            }

            try
            {
                HsCOMerrorStrings = _serialPort1.ReadExisting();
                _serialPort1.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Data);
            }
            Console.WriteLine(ErrorCheck(HsCOMerrorStrings));
        }

        private string ErrorCheck(string COMHostStatusReturn)
        {
            string ErrorsReturn = System.String.Empty;
            string[] COMCode = COMHostStatusReturn.Split(',');

            if (COMCode[1] == "1") { ErrorsReturn += "Carta Esaurita" + Environment.NewLine; }
            if (COMCode[2] == "1") { ErrorsReturn += "Stampante in Pausa" + Environment.NewLine; }
            if (COMCode[5] == "1") { ErrorsReturn += "Memoria Stampante Esaurita" + Environment.NewLine; }
            if (COMCode[9] == "1") { ErrorsReturn += "Memoria RAM corrotta" + Environment.NewLine; }
            if (COMCode[10] == "1") { ErrorsReturn += "Temperatura troppo bassa" + Environment.NewLine; }
            if (COMCode[11] == "1") { ErrorsReturn += "Temperatura troppo alta" + Environment.NewLine; }

            if (COMCode[7] == "1") { ErrorsReturn += "Partial Format Flag" + Environment.NewLine; }

            if (ErrorsReturn != System.String.Empty)    
            {

                if (PrintTimerIsActive == true)         
                {
                    PrintTimer.Stop();                  

                    if (!ErrorTimer.Enabled)             
                        ErrorTimer.Start();             

                    MessageBox.Show(ErrorsReturn);      
                }
                PrintTimerIsActive = false;             
            }
            else                                        
            {                                   
                if (PrintTimerIsActive == false)        
                {
                    ErrorTimer.Stop();                  
                    PrintTimer.Start();                 
                    PrintTimerIsActive = true;          
                }
            }

            return ErrorsReturn;
        }

        private void SendFilesToCom(string filePath)
        {
            if (!_serialPort1.IsOpen)
            {
                try
                {
                    _serialPort1.Open();
                }
                catch (Exception ex)
                {
                }
            }
            try
            {
                using (System.IO.TextReader reader = File.OpenText(filePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        _serialPort1.WriteLine(line);
                    }

                    HsCOMerrorStrings = _serialPort1.ReadLine();
                    reader.Close(); 
                    _serialPort1.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
4

0 回答 0