餐飲POS新利器-取餐呼叫器與程式連線結合範例

_sE_4843323437

11221600_10153061581601541_7866645605585747097_n

12191428_10153061581676541_4081491775779063707_n

11200883_395736973959542_2078442132751771489_n

在國外行之有年的取餐呼叫器(Pager)最近也在台灣風行起來,看到了二款取餐呼叫器,一個是國產亞克高科(ARCT)由銥特爾科技(02-29974000)代理,一個是韓國(Syscall)製造由鋐鈦科技代理。
二款都可以透過COM PORT設定~~ 以Delphi以及C#針對韓國的做範例。
Delphi

var iPagerno   :Integer;
    sStr,s :AnsiString;
begin
   Try
     Pager_COM.StopComm;  // 先將Pager Com關閉
     Pager_COM.CommName := ComName;
     Pager_COM.StartComm;
     Sleep(50);  //開啟COM需等待
     iPagerno := StrToIntDef(PagerNo,1);
     sStr := copy(IntToStr(iPagerno),1,4)
     s:=  Ansichar(#開始指令)+sStr+Ansichar(#結束指令);
     Pager_COM.WriteCommData(PAnsiChar(s),Length(s));
   Except
   End;

C#

       public void Pager_Send(String COMName,String PagerNo)
        {
public static System.IO.Ports.SerialPort PAGERPORT = new System.IO.Ports.SerialPort();
            byte[] PAGERSTART = { 開始指令 };       
            byte[] PAGEREND = { 結束指令 };  
            try
            {
                if (PAGERPORT.IsOpen)
                    PAGERPORT.Close();
                PAGERPORT.PortName = COMName;
                PAGERPORT.BaudRate = 9600;
                PAGERPORT.Parity = System.IO.Ports.Parity.None;
                PAGERPORT.DataBits = 8;
                PAGERPORT.StopBits = System.IO.Ports.StopBits.One;
                PAGERPORT.Open();
             if (!PAGERPORT.IsOpen)
                PAGERPORT.Open();
            for (int i = 0; i < PAGERSTART.Length; i++)
            {
                PAGERPORT.Write(PAGERSTART , i, 1);
            }
                PagerNo = PagerNo.PadLeft(4, '0').ToString();
                byte[] PagerNoData= Encoding.Default.GetBytes(PagerNo);
            if (!PAGERPORT.IsOpen)
                PAGERPORT.Open();
            for (int i = 0; i < PagerNoData.Length; i++)
            {
                PAGERPORT.Write(PagerNoData, i, 1);
            }
                         if (!PAGERPORT.IsOpen)
                PAGERPORT.Open();
            for (int i = 0; i < PAGEREND .Length; i++)
            {
                PAGERPORT.Write(PAGEREND , i, 1);
            }
            }
            catch
            {    
               
             
                return;
            }
        }

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++;
        }