如何用C#监听多个串口

发布于 2024-09-10 05:43:30 字数 21342 浏览 11 评论 0原文

我正在使用C# 中的串行 COM > 作者:Noah Coad,我的机器上有可用端口 COM1、COM2、COM6、COM7、COM8、COM9、COM10、COM11、COM12、COM13,我使用 COM6、COM7、COM8、COM9、COM10、COM11, COM12 与 ZyXEL Omni 56K 调制解调器。我将这七个端口与七个调制解调器一起使用。

有些客户希望通过这些调制解调器获得答案。我使用 mscomm.ocx 来监听 COM 端口。我的应用程序运行成功,但有时一个调制解调器正在处理 50-100 个请求并应答,然后它会停止工作,并且我的客户端会超时,因为它们没有从服务器获得应答(使用 COM 端口调制解调器)。首先它得到“RING”,然后得到“CONNECT 1200/V.22b 2400/NONE”,然后得到我的客户端的原始包,我处理这个包并向客户端生成一个答案。

我使用七个 EXE 文件来打开和监听 COM 端口,因为当我使用 我不同时处理一个 EXE 文件。我知道我必须使用线程,但我不知道如何使用。

如何同时监听七个 COM 端口并毫无问题地完成工作?我始终希望我的应用程序能够正常工作并同时回复所有客户。这是我的代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace Serial_Demo
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label8;

        private Sistem sistemim = new Sistem();
        private ParcaKontorRaporu parcaKontorRaporu = new ParcaKontorRaporu();
        private ParcaKontorYukleme parcaKontorYukleme = new ParcaKontorYukleme();
        private FaturaOdeme faturaOdeme = new FaturaOdeme();
        private FaturaRaporu faturaRaporu = new FaturaRaporu();
        private FaturaSorgulama faturaSorgulama = new FaturaSorgulama();
        private NumaraSorgulama numaraSorgulama = new NumaraSorgulama();
        private TamKontorRaporu tamKontorRaporu = new TamKontorRaporu();
        private TamKontorYkleme tamKontorYukleme = new TamKontorYkleme();
        private BakiyeSorgulama bakiyeSorgulama = new BakiyeSorgulama();
        private HavaleBildirimi havale = new HavaleBildirimi();
        private KartKontorSifresi kartSifresi = new KartKontorSifresi();
        private KartKontorRaporu kartRapor = new KartKontorRaporu();
        private FiyatListesi fiyatListesi = new FiyatListesi();
        private AylikKontorRaporu aylikKontorRapor = new AylikKontorRaporu();
        private AylikFaturaRaporu aylikFaturaRapor = new AylikFaturaRaporu();
        private SifreDegirtirme sifreDegistir = new SifreDegirtirme();
        private BankaExtra BankExtra = new BankaExtra();
        private Duyurular duyuru = new Duyurular();
        private DuyurularExtra duyuruExtra = new DuyurularExtra();
        private BankaListesi Bankalar = new BankaListesi();
        private KontorFiyatSorgula KontorFiyat = new KontorFiyatSorgula();

        string bugun = DateTime.Now.ToShortDateString();
        private AxMSCommLib.AxMSComm com;
        private Button btn_disconnect;
        private Label label1;
        private Panel panel1;
        string GidenToPos = null;
        private int count = 0;
        private IContainer components;
        private System.Windows.Forms.Timer timer1;
        private ListBox listBox1;
        private Label sayiLbl;
        private Label label2;
        Char chr = (char)13;

        public Form1()
        {
            InitializeComponent();

            InitComPort();
        }

        private void InitComPort()
        {
            // Set the com port to be 1
            com.CommPort = 6;

            // This port is already open, then close.
            if (com.PortOpen)
                com.PortOpen = false;

            // Trigger the OnComm event whenever data is received
            com.RThreshold = 1;

            // Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard)
            com.Settings = "1200,n,8,1";

            // Force the DTR line high, used sometimes to hang up modems
            //com.DTREnable = true;

            com.RTSEnable = true;

            com.DTREnable = true;

            // No handshaking is used
            com.Handshaking = MSCommLib.HandshakeConstants.comNone;

            // Use this line instead for byte array input, best for most communications
            com.InputMode = MSCommLib.InputModeConstants.comInputModeText;

            // Read the entire waiting data when com.Input is used
            com.InputLen = 0;

            // Don't discard nulls, 0x00 is a useful byte
            com.NullDiscard = false;

            // Attach the event handler
            com.OnComm += new System.EventHandler(this.OnComm);

            com.PortOpen = true;

            com.Output = "AT&F S0=1" + chr;
            com.Output = "ATM0" + chr;
            com.Output = "ATS7=90 +++ATH0" + chr;
        }

        private void OnComm(object sender, EventArgs e)  //  MSCommLib OnComm Event Handler
        {
           Thread.Sleep(400);

            if (com.InBufferCount > 0)
            {
                string response = (string)com.Input;

                ProcessResponseText(response);
            }
            com.PortOpen = true;
        }

        private void ProcessResponseText(string input)
        {
            string IslemSaati = DateTime.Now.ToShortTimeString();

            if (input.Trim().Equals("RING"))
            {
                listBox1.Items.Add(IslemSaati + " - ringing...");
            }
            else
                if (input.Trim().Equals("CONNECT 1200/V.22b 2400/NONE"))
                {
                    listBox1.Items.Add(IslemSaati + " - connecting...");

                }
                else
                {
                    char[] c = input.Trim().ToCharArray();
                    if (c[0].ToString() == "\x02")
                    {
                        count++;
                        sayiLbl.Text = "number of process : " + Convert.ToString(count);

                        string gidecekStr = null;
                        gidecekStr = GeleniTemizle(input);
                        GidenToPos = null;

                        char[] k = gidecekStr.ToCharArray();

                        if (k[0].ToString() == "X")
                        {
                            for (int i = 1; i < k.Length; i++)
                            {
                                GidenToPos += k[i];
                            }
                        }
                        else
                        {
                            GidenToPos = paketFiltre(gidecekStr);
                        }

                        listBox1.Items.Add(IslemSaati + " - Coming Package : " + input);

                        com.Output = GidenToPos;

                        listBox1.Items.Add(IslemSaati + " - Going Package : " + GidenToPos);
                    }
                    else
                    {
                        sayiLbl.Text = "number of process : " + Convert.ToString(count);
                    }
                }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {

            }
            base.Dispose(disposing);
        }

        public string paketFiltre(string gelen)
        {
            char[] c = gelen.ToCharArray();
            string gidecekStr = null;
            int secim = 0;

            if (c[18].ToString() == "1")
            {
                secim = 1;
            }
            if (c[18].ToString() == "2")
            {
                secim = 2;
            }
            if (c[18].ToString() == "3")
            {
                secim = 3;
            }
            if (c[18].ToString() == "4")
            {
                secim = 4;
            }
            if (c[18].ToString() == "5")
            {
                secim = 5;
            }
            if (c[18].ToString() == "6")
            {
                secim = 6;
            }
            if (c[18].ToString() == "7")
            {
                secim = 7;
            }
            if (c[18].ToString() == "8")
            {
                secim = 8;
            }
            if (c[18].ToString() == "9")
            {
                secim = 9;
            }
            if (c[18].ToString() == "A")
            {
                secim = 10;
            }
            if (c[18].ToString() == "B")
            {
                secim = 11;
            }
            if (c[18].ToString() == "C")
            {
                secim = 12;
            }
            if (c[18].ToString() == "D")
            {
                secim = 13;
            }
            if (c[18].ToString() == "E")
            {
                secim = 14;
            }
            if (c[18].ToString() == "F")
            {
                secim = 15;
            }
            if (c[18].ToString() == "G")
            {
                secim = 16;
            }
            if (c[18].ToString() == "H")
            {
                secim = 17;
            }
            if (c[18].ToString() == "I")
            {
                secim = 18;
            }
            if (c[18].ToString() == "J")
            {
                secim = 19;
            }
            if (c[18].ToString() == "K")
            {
                secim = 20;
            }
            if (c[18].ToString() == "L")
            {
                secim = 21;
            }

            switch (secim)
            {
                case 1:  // Bakiye Sorgulama
                    gidecekStr = bakiyeSorgulama.BakiyeSorgula(gelen);
                    break;

                case 2:  // Para Kontr Talimati Gnderme
                    gidecekStr = parcaKontorYukleme.ParcaKontorYukle(gelen);
                    break;

                case 3: // Para Kontr Raporu
                    gidecekStr = parcaKontorRaporu.Rapor(gelen);
                    break;

                case 4: // Tam Kontr Talimati Gnderme
                    gidecekStr = tamKontorYukleme.KontorYukle(gelen);
                    break;

                case 5: // Tam Kontr Raporu
                    gidecekStr = tamKontorRaporu.Rapor(gelen);
                    break;

                case 6: // Numara Sorgulama
                    gidecekStr = numaraSorgulama.Sorgula(gelen);
                    break;

                case 7: // Fatura Sorgulama
                    gidecekStr = faturaSorgulama.Sorgulama(gelen);
                    break;

                case 8: // Fatura deme
                    gidecekStr = faturaOdeme.FaturaOde(gelen);
                    break;

                case 9: // Fatura Raporu
                    gidecekStr = faturaRaporu.Rapor(gelen);
                    break;

                case 10: // HAVALE/EFT BILDIRIMI
                    gidecekStr = havale.HavaleEftBildirimi(gelen);
                    break;

                case 11: // KART KONTR SIFRESI ALMA
                    gidecekStr = kartSifresi.KartKontorSifresiAlma(gelen);
                    break;

                case 12: // KART KONTR RAPORU
                    gidecekStr = kartRapor.Rapor(gelen);
                    break;

                case 13: // FIYAT LISTESI
                    gidecekStr = fiyatListesi.Liste(gelen);
                    break;

                case 14: // GNLK/HAFTALIK/AYLIK KONTR RAPORU ALMA
                    gidecekStr = aylikKontorRapor.Rapor(gelen);
                    break;

                case 15: // GNLK/HAFTALIK/AYLIK FATURA RAPORU ALMA
                    gidecekStr = aylikFaturaRapor.Rapor(gelen);
                    break;

                case 16: // SIFRE DEGISTIRME
                    gidecekStr = sifreDegistir.SifreDegistir(gelen);
                    break;

                case 17: // BANKA LISTESI
                    gidecekStr = Bankalar.Liste(gelen);
                    break;

                case 18: // BANKA EXTRA
                    gidecekStr = BankExtra.Liste(gelen);
                    break;

                case 19: // DUYURULAR
                    gidecekStr = duyuru.Liste(gelen);
                    break;

                case 20: // DUYURULAR EXTRA
                    gidecekStr = duyuruExtra.Liste(gelen);
                    break;

                case 21: // KONTR FIYAT SORGULA
                    gidecekStr = KontorFiyat.Sorgula(gelen);
                    break;
            }
            return gidecekStr;
        }

        public string ConvertToHex(string asciiString)
        {
            string hex = "";
            foreach (char c in asciiString)
            {
                int tmp = c;
                hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
            }
            return hex;
        }

        private string HexAsciiConvert(string hex)
        {

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i <= hex.Length - 2; i += 2)
            {

                sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i, 2),

                System.Globalization.NumberStyles.HexNumber))));

            }

            return sb.ToString();

        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.label8 = new System.Windows.Forms.Label();
            this.com = new AxMSCommLib.AxMSComm();
            this.btn_disconnect = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label2 = new System.Windows.Forms.Label();
            this.sayiLbl = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.listBox1 = new System.Windows.Forms.ListBox();
            ((System.ComponentModel.ISupportInitialize)(this.com)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            //
            // label8
            //
            this.label8.BackColor = System.Drawing.Color.Transparent;
            this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label8.ForeColor = System.Drawing.SystemColors.Highlight;
            this.label8.Location = new System.Drawing.Point(0, 109);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(160, 24);
            this.label8.TabIndex = 22;
            this.label8.Text = "Gelen/Giden Paketler :";
            //
            // com
            //
            this.com.Enabled = true;
            this.com.Location = new System.Drawing.Point(491, 109);
            this.com.Name = "com";
            this.com.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("com.OcxState")));
            this.com.Size = new System.Drawing.Size(38, 38);
            this.com.TabIndex = 14;
            //
            // btn_disconnect
            //
            this.btn_disconnect.Location = new System.Drawing.Point(533, 72);
            this.btn_disconnect.Name = "btn_disconnect";
            this.btn_disconnect.Size = new System.Drawing.Size(86, 23);
            this.btn_disconnect.TabIndex = 17;
            this.btn_disconnect.Text = "Baglantiyi kes";
            this.btn_disconnect.Click += new System.EventHandler(this.btn_disconnect_Click);
            //
            // label1
            //
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.Red;
            this.label1.Location = new System.Drawing.Point(251, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(148, 24);
            this.label1.TabIndex = 24;
            this.label1.Text = "DARKOCEAN";
            //
            // panel1
            //
            this.panel1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.sayiLbl);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.btn_disconnect);
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(634, 104);
            this.panel1.TabIndex = 23;
            //
            // label2
            //
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.Red;
            this.label2.Location = new System.Drawing.Point(251, 41);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(133, 24);
            this.label2.TabIndex = 26;
            this.label2.Text = " MODEM - 2";
            //
            // sayiLbl
            //
            this.sayiLbl.AutoSize = true;
            this.sayiLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.sayiLbl.ForeColor = System.Drawing.Color.Navy;
            this.sayiLbl.Location = new System.Drawing.Point(11, 72);
            this.sayiLbl.Name = "sayiLbl";
            this.sayiLbl.Size = new System.Drawing.Size(0, 13);
            this.sayiLbl.TabIndex = 25;
            //
            // listBox1
            //
            this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(0, 136);
            this.listBox1.Name = "listBox1";
            this.listBox1.ScrollAlwaysVisible = true;
            this.listBox1.Size = new System.Drawing.Size(634, 134);
            this.listBox1.TabIndex = 27;
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(632, 270);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.com);
            this.Controls.Add(this.label8);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "DARKOCEAN";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.com)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void btn_disconnect_Click(object sender, System.EventArgs e)
        {
            // If com port is open then close it
            // to disconnect connection

            if (com.PortOpen)
            {
                com.PortOpen = false;
                MessageBox.Show("Disconnected...", this.Text,
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public string GeleniTemizle(string gelen)
        {
            string DATA = null;
            string LRC = null;

            string temp1 = gelen.Replace("\x02", "");
            string[] tempArray = temp1.Split('\x03');


            string kontrolLRC = "";

            string gelenLRC = tempArray[1];

            sbyte[] array;

            array = sistemim.PrepareLRCPacket(tempArray[0]);

            foreach (sbyte ch in array)
                LRC += Convert.ToChar(ch);

            kontrolLRC = LRC[LRC.Length - 1].ToString();

            if (gelenLRC == kontrolLRC )
            {
                DATA = tempArray[0];
            }
            else
            {
                sbyte[] array2;
                string LRC2 = null;

                array2 = sistemim.PrepareLRCPacket("000000000000215" + "package problem");
                DATA = null;

                foreach (sbyte ch2 in array2)
                    LRC2 += Convert.ToChar(ch2);

                DATA = "X" + LRC2;
            }

            return DATA;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

I am using Serial COM Simply in C# by Noah Coad, and I have available ports on my machine which is COM1, COM2, COM6, COM7, COM8, COM9, COM10, COM11, COM12, COM13, and I use COM6, COM7, COM8, COM9, COM10, COM11, COM12 with the ZyXEL Omni 56K modem. I use these seven ports with seven modems.

There are clients for getting an answer with these modems. I use the mscomm.ocx to listen to COM ports. My application is running successfully, but sometimes one modem is processing 50-100 requests and answer then it stops working and my clients are getting a timeout because they do not get the answer from the server (using a COM-port modem). First it is getting "RING" and then getting "CONNECT 1200/V.22b 2400/NONE" then getting orginal package of my client and I process this package and produce an answer to the client.

I use seven EXE files for open and listen to COM ports because when I use
one EXE file I do not process same time. I know I must use a thread, but I don't know how.

How can I listen to seven COM ports at the same time and doing my work with no problems? Always I want to my application work correctly and reply to all clients at the same time. This is my code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace Serial_Demo
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label8;

        private Sistem sistemim = new Sistem();
        private ParcaKontorRaporu parcaKontorRaporu = new ParcaKontorRaporu();
        private ParcaKontorYukleme parcaKontorYukleme = new ParcaKontorYukleme();
        private FaturaOdeme faturaOdeme = new FaturaOdeme();
        private FaturaRaporu faturaRaporu = new FaturaRaporu();
        private FaturaSorgulama faturaSorgulama = new FaturaSorgulama();
        private NumaraSorgulama numaraSorgulama = new NumaraSorgulama();
        private TamKontorRaporu tamKontorRaporu = new TamKontorRaporu();
        private TamKontorYkleme tamKontorYukleme = new TamKontorYkleme();
        private BakiyeSorgulama bakiyeSorgulama = new BakiyeSorgulama();
        private HavaleBildirimi havale = new HavaleBildirimi();
        private KartKontorSifresi kartSifresi = new KartKontorSifresi();
        private KartKontorRaporu kartRapor = new KartKontorRaporu();
        private FiyatListesi fiyatListesi = new FiyatListesi();
        private AylikKontorRaporu aylikKontorRapor = new AylikKontorRaporu();
        private AylikFaturaRaporu aylikFaturaRapor = new AylikFaturaRaporu();
        private SifreDegirtirme sifreDegistir = new SifreDegirtirme();
        private BankaExtra BankExtra = new BankaExtra();
        private Duyurular duyuru = new Duyurular();
        private DuyurularExtra duyuruExtra = new DuyurularExtra();
        private BankaListesi Bankalar = new BankaListesi();
        private KontorFiyatSorgula KontorFiyat = new KontorFiyatSorgula();

        string bugun = DateTime.Now.ToShortDateString();
        private AxMSCommLib.AxMSComm com;
        private Button btn_disconnect;
        private Label label1;
        private Panel panel1;
        string GidenToPos = null;
        private int count = 0;
        private IContainer components;
        private System.Windows.Forms.Timer timer1;
        private ListBox listBox1;
        private Label sayiLbl;
        private Label label2;
        Char chr = (char)13;

        public Form1()
        {
            InitializeComponent();

            InitComPort();
        }

        private void InitComPort()
        {
            // Set the com port to be 1
            com.CommPort = 6;

            // This port is already open, then close.
            if (com.PortOpen)
                com.PortOpen = false;

            // Trigger the OnComm event whenever data is received
            com.RThreshold = 1;

            // Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard)
            com.Settings = "1200,n,8,1";

            // Force the DTR line high, used sometimes to hang up modems
            //com.DTREnable = true;

            com.RTSEnable = true;

            com.DTREnable = true;

            // No handshaking is used
            com.Handshaking = MSCommLib.HandshakeConstants.comNone;

            // Use this line instead for byte array input, best for most communications
            com.InputMode = MSCommLib.InputModeConstants.comInputModeText;

            // Read the entire waiting data when com.Input is used
            com.InputLen = 0;

            // Don't discard nulls, 0x00 is a useful byte
            com.NullDiscard = false;

            // Attach the event handler
            com.OnComm += new System.EventHandler(this.OnComm);

            com.PortOpen = true;

            com.Output = "AT&F S0=1" + chr;
            com.Output = "ATM0" + chr;
            com.Output = "ATS7=90 +++ATH0" + chr;
        }

        private void OnComm(object sender, EventArgs e)  //  MSCommLib OnComm Event Handler
        {
           Thread.Sleep(400);

            if (com.InBufferCount > 0)
            {
                string response = (string)com.Input;

                ProcessResponseText(response);
            }
            com.PortOpen = true;
        }

        private void ProcessResponseText(string input)
        {
            string IslemSaati = DateTime.Now.ToShortTimeString();

            if (input.Trim().Equals("RING"))
            {
                listBox1.Items.Add(IslemSaati + " - ringing...");
            }
            else
                if (input.Trim().Equals("CONNECT 1200/V.22b 2400/NONE"))
                {
                    listBox1.Items.Add(IslemSaati + " - connecting...");

                }
                else
                {
                    char[] c = input.Trim().ToCharArray();
                    if (c[0].ToString() == "\x02")
                    {
                        count++;
                        sayiLbl.Text = "number of process : " + Convert.ToString(count);

                        string gidecekStr = null;
                        gidecekStr = GeleniTemizle(input);
                        GidenToPos = null;

                        char[] k = gidecekStr.ToCharArray();

                        if (k[0].ToString() == "X")
                        {
                            for (int i = 1; i < k.Length; i++)
                            {
                                GidenToPos += k[i];
                            }
                        }
                        else
                        {
                            GidenToPos = paketFiltre(gidecekStr);
                        }

                        listBox1.Items.Add(IslemSaati + " - Coming Package : " + input);

                        com.Output = GidenToPos;

                        listBox1.Items.Add(IslemSaati + " - Going Package : " + GidenToPos);
                    }
                    else
                    {
                        sayiLbl.Text = "number of process : " + Convert.ToString(count);
                    }
                }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {

            }
            base.Dispose(disposing);
        }

        public string paketFiltre(string gelen)
        {
            char[] c = gelen.ToCharArray();
            string gidecekStr = null;
            int secim = 0;

            if (c[18].ToString() == "1")
            {
                secim = 1;
            }
            if (c[18].ToString() == "2")
            {
                secim = 2;
            }
            if (c[18].ToString() == "3")
            {
                secim = 3;
            }
            if (c[18].ToString() == "4")
            {
                secim = 4;
            }
            if (c[18].ToString() == "5")
            {
                secim = 5;
            }
            if (c[18].ToString() == "6")
            {
                secim = 6;
            }
            if (c[18].ToString() == "7")
            {
                secim = 7;
            }
            if (c[18].ToString() == "8")
            {
                secim = 8;
            }
            if (c[18].ToString() == "9")
            {
                secim = 9;
            }
            if (c[18].ToString() == "A")
            {
                secim = 10;
            }
            if (c[18].ToString() == "B")
            {
                secim = 11;
            }
            if (c[18].ToString() == "C")
            {
                secim = 12;
            }
            if (c[18].ToString() == "D")
            {
                secim = 13;
            }
            if (c[18].ToString() == "E")
            {
                secim = 14;
            }
            if (c[18].ToString() == "F")
            {
                secim = 15;
            }
            if (c[18].ToString() == "G")
            {
                secim = 16;
            }
            if (c[18].ToString() == "H")
            {
                secim = 17;
            }
            if (c[18].ToString() == "I")
            {
                secim = 18;
            }
            if (c[18].ToString() == "J")
            {
                secim = 19;
            }
            if (c[18].ToString() == "K")
            {
                secim = 20;
            }
            if (c[18].ToString() == "L")
            {
                secim = 21;
            }

            switch (secim)
            {
                case 1:  // Bakiye Sorgulama
                    gidecekStr = bakiyeSorgulama.BakiyeSorgula(gelen);
                    break;

                case 2:  // Para Kontr Talimati Gnderme
                    gidecekStr = parcaKontorYukleme.ParcaKontorYukle(gelen);
                    break;

                case 3: // Para Kontr Raporu
                    gidecekStr = parcaKontorRaporu.Rapor(gelen);
                    break;

                case 4: // Tam Kontr Talimati Gnderme
                    gidecekStr = tamKontorYukleme.KontorYukle(gelen);
                    break;

                case 5: // Tam Kontr Raporu
                    gidecekStr = tamKontorRaporu.Rapor(gelen);
                    break;

                case 6: // Numara Sorgulama
                    gidecekStr = numaraSorgulama.Sorgula(gelen);
                    break;

                case 7: // Fatura Sorgulama
                    gidecekStr = faturaSorgulama.Sorgulama(gelen);
                    break;

                case 8: // Fatura deme
                    gidecekStr = faturaOdeme.FaturaOde(gelen);
                    break;

                case 9: // Fatura Raporu
                    gidecekStr = faturaRaporu.Rapor(gelen);
                    break;

                case 10: // HAVALE/EFT BILDIRIMI
                    gidecekStr = havale.HavaleEftBildirimi(gelen);
                    break;

                case 11: // KART KONTR SIFRESI ALMA
                    gidecekStr = kartSifresi.KartKontorSifresiAlma(gelen);
                    break;

                case 12: // KART KONTR RAPORU
                    gidecekStr = kartRapor.Rapor(gelen);
                    break;

                case 13: // FIYAT LISTESI
                    gidecekStr = fiyatListesi.Liste(gelen);
                    break;

                case 14: // GNLK/HAFTALIK/AYLIK KONTR RAPORU ALMA
                    gidecekStr = aylikKontorRapor.Rapor(gelen);
                    break;

                case 15: // GNLK/HAFTALIK/AYLIK FATURA RAPORU ALMA
                    gidecekStr = aylikFaturaRapor.Rapor(gelen);
                    break;

                case 16: // SIFRE DEGISTIRME
                    gidecekStr = sifreDegistir.SifreDegistir(gelen);
                    break;

                case 17: // BANKA LISTESI
                    gidecekStr = Bankalar.Liste(gelen);
                    break;

                case 18: // BANKA EXTRA
                    gidecekStr = BankExtra.Liste(gelen);
                    break;

                case 19: // DUYURULAR
                    gidecekStr = duyuru.Liste(gelen);
                    break;

                case 20: // DUYURULAR EXTRA
                    gidecekStr = duyuruExtra.Liste(gelen);
                    break;

                case 21: // KONTR FIYAT SORGULA
                    gidecekStr = KontorFiyat.Sorgula(gelen);
                    break;
            }
            return gidecekStr;
        }

        public string ConvertToHex(string asciiString)
        {
            string hex = "";
            foreach (char c in asciiString)
            {
                int tmp = c;
                hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
            }
            return hex;
        }

        private string HexAsciiConvert(string hex)
        {

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i <= hex.Length - 2; i += 2)
            {

                sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i, 2),

                System.Globalization.NumberStyles.HexNumber))));

            }

            return sb.ToString();

        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.label8 = new System.Windows.Forms.Label();
            this.com = new AxMSCommLib.AxMSComm();
            this.btn_disconnect = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label2 = new System.Windows.Forms.Label();
            this.sayiLbl = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.listBox1 = new System.Windows.Forms.ListBox();
            ((System.ComponentModel.ISupportInitialize)(this.com)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            //
            // label8
            //
            this.label8.BackColor = System.Drawing.Color.Transparent;
            this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label8.ForeColor = System.Drawing.SystemColors.Highlight;
            this.label8.Location = new System.Drawing.Point(0, 109);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(160, 24);
            this.label8.TabIndex = 22;
            this.label8.Text = "Gelen/Giden Paketler :";
            //
            // com
            //
            this.com.Enabled = true;
            this.com.Location = new System.Drawing.Point(491, 109);
            this.com.Name = "com";
            this.com.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("com.OcxState")));
            this.com.Size = new System.Drawing.Size(38, 38);
            this.com.TabIndex = 14;
            //
            // btn_disconnect
            //
            this.btn_disconnect.Location = new System.Drawing.Point(533, 72);
            this.btn_disconnect.Name = "btn_disconnect";
            this.btn_disconnect.Size = new System.Drawing.Size(86, 23);
            this.btn_disconnect.TabIndex = 17;
            this.btn_disconnect.Text = "Baglantiyi kes";
            this.btn_disconnect.Click += new System.EventHandler(this.btn_disconnect_Click);
            //
            // label1
            //
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.Red;
            this.label1.Location = new System.Drawing.Point(251, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(148, 24);
            this.label1.TabIndex = 24;
            this.label1.Text = "DARKOCEAN";
            //
            // panel1
            //
            this.panel1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.sayiLbl);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.btn_disconnect);
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(634, 104);
            this.panel1.TabIndex = 23;
            //
            // label2
            //
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.Red;
            this.label2.Location = new System.Drawing.Point(251, 41);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(133, 24);
            this.label2.TabIndex = 26;
            this.label2.Text = " MODEM - 2";
            //
            // sayiLbl
            //
            this.sayiLbl.AutoSize = true;
            this.sayiLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.sayiLbl.ForeColor = System.Drawing.Color.Navy;
            this.sayiLbl.Location = new System.Drawing.Point(11, 72);
            this.sayiLbl.Name = "sayiLbl";
            this.sayiLbl.Size = new System.Drawing.Size(0, 13);
            this.sayiLbl.TabIndex = 25;
            //
            // listBox1
            //
            this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(0, 136);
            this.listBox1.Name = "listBox1";
            this.listBox1.ScrollAlwaysVisible = true;
            this.listBox1.Size = new System.Drawing.Size(634, 134);
            this.listBox1.TabIndex = 27;
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(632, 270);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.com);
            this.Controls.Add(this.label8);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "DARKOCEAN";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.com)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void btn_disconnect_Click(object sender, System.EventArgs e)
        {
            // If com port is open then close it
            // to disconnect connection

            if (com.PortOpen)
            {
                com.PortOpen = false;
                MessageBox.Show("Disconnected...", this.Text,
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public string GeleniTemizle(string gelen)
        {
            string DATA = null;
            string LRC = null;

            string temp1 = gelen.Replace("\x02", "");
            string[] tempArray = temp1.Split('\x03');


            string kontrolLRC = "";

            string gelenLRC = tempArray[1];

            sbyte[] array;

            array = sistemim.PrepareLRCPacket(tempArray[0]);

            foreach (sbyte ch in array)
                LRC += Convert.ToChar(ch);

            kontrolLRC = LRC[LRC.Length - 1].ToString();

            if (gelenLRC == kontrolLRC )
            {
                DATA = tempArray[0];
            }
            else
            {
                sbyte[] array2;
                string LRC2 = null;

                array2 = sistemim.PrepareLRCPacket("000000000000215" + "package problem");
                DATA = null;

                foreach (sbyte ch2 in array2)
                    LRC2 += Convert.ToChar(ch2);

                DATA = "X" + LRC2;
            }

            return DATA;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

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

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

发布评论

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

评论(1

ぽ尐不点ル 2024-09-17 05:43:30

您应该将 COM 端口 处理包装到一个单独的类中。该类将有自己的 COM 端口实例,然后您可以创建包装类的多个实例,每个实例对应您要访问的每个 COM 端口。

You should wrap your COM port handling into a separate class. The class will have its own instance of the COM port and then you can create multiple instances of your wrapper class, one for each COM port you want to access.

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