POS系統介接與信用卡機連線的作法

Delphi或C#與信用卡連接的方式,有直接透過com port通訊或是透過呼叫exe的方式,利用in.txt、out.txt做溝通。然後回傳信用卡卡號/授權碼/刷卡金額等資訊。
示範一下Delphi與C#如何做信用卡線上刷卡。
Delphi

  public
    { Public declarations }
    ExecInfo : TShellExecuteInfo;   // use shellapi
    i:integer;
  end;
procedure TForm1.Button1Click(Sender: TObject);
var 
  s:String;
  ts:TStringlist;
begin
  ZeroMemory(@ExecInfo,SizeOf(ExecInfo));
  with ExecInfo do begin
    cbSize := SizeOf(ExecInfo);
    fMask := SEE_MASK_NOCLOSEPROCESS;
    lpVerb := 'open';
    lpFile := 'ecr.exe'; 
      Wnd := self.Handle;
    nShow := SW_HIDE; 
  end;
  s:='xxxxxxxxxxxxxxxxxxxx'; // 填上信用卡的溝通格式
  ts := Tstringlist.Create;
  ts.Clear;
  ts.Add(s);
  ts.SaveToFile('in.dat');
  ts.Free; 
  ShellExecuteEx(@ExecInfo);
  deletefile('out.dat');
  caption := '刷卡中...';
  timer1.Enabled := True;
  i:=0;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var ts1:Tstringlist;
sstatus,smoney,scard,sappno:String;
begin
  i:=i+1;
  if fileexists('out.dat') then begin
     Timer1.Enabled := false;
     ts1 := Tstringlist.Create;
     ts1.LoadFromFile('out.dat');
     if ts1.Count >0 then begin
       // 讀入檔案,解析格式
     end;
     ts1.Free;
  end;
end;
string dir = System.Windows.Forms.Application.StartupPath;
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        private void uForm1_Load(object sender, EventArgs e)
        {
            try
            {
                if (File.Exists(dir + "/out.txt")) //刪除out.txt
                    File.Delete(dir + "/out.txt");
                string code = "";  // in.txt格式
                using (StreamWriter sw = new StreamWriter(dir + "/in.txt"))   //小寫TXT     
                sw.Write(code);    
                IntPtr PDC = FindWindow(null, "ecr");  //開啟PosDataCom
                if (PDC == (IntPtr)0)
                {
                    try
                    {
                        Process p = new Process();
                        p.StartInfo.FileName = dir + "/ecrnccc.exe";
                        p.StartInfo.WorkingDirectory = dir;
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardInput = true;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.RedirectStandardError = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.Start();
                    }
                    catch (Exception exp)
                    {
                        return;
                    }
                }
               
               
                this.timer1.Enabled = true;
               
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (times == 5)
            {                
                times = 0;
                i++;
                if (i > 4)
                {
                    i = 0;
                    try
                {
                if (File.Exists(dir + "/out.txt"))
                {
                   
                    using (StreamReader sr = new StreamReader(dir + "/out.txt"))     //小寫TXT
                    {
                        String line;
                      
                        if ((line = sr.ReadLine()) != null)
                        {
                          // 解析
                        }
                        else
                            return false;
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;           
            }
                }
            }
            times++;
        }