我正在使用Noah Coad的 Serial COM Simply in C# ,我的机器上有可用的端口,COM1,COM2,COM6,COM7,COM8,COM9,COM10,COM11,COM12, COM13,我使用COM6,COM7,COM8,COM9,COM10,COM11,COM12和ZyXEL Omni 56K调制解调器。我使用这7个端口和7个调制解调器。
有些客户可以通过这些调制解调器获得答案。我使用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)
{
}
}
}