Embarcadero官方訊息釋出2016年的Roadmap,
標籤: C#
餐飲POS新利器-取餐呼叫器與程式連線結合範例
在國外行之有年的取餐呼叫器(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++;
}
c#簡體版列印指令注意
簡體版列印指令注意
System.Text.Encoding.GetEncoding(“gb2312”).GetBytes(“简体文字测试”);
不能使用
System.Text.Encoding.Default.GetBytes(“简体文字测试”);
會依據作業系統的encoding~~
this.serialPort1.PortName = textBox1.Text;
this.serialPort1.Open();
byte[] bData = System.Text.Encoding.GetEncoding("gb2312").GetBytes("简体文字测试");
this.serialPort1.Write(bData, 0, bData.Length);
byte[] CutPaper = new byte[] { 0x1d, 0x56, 0x42, 0x00 };
this.serialPort1.Write(CutPaper, 0, CutPaper.Length);
ASP.net執行SQL逾時等候
ASP.net執行可能比較久的SQL語法時,會出現下面的訊息。

已超過連接逾時的設定。在作業完成之前超過逾時等待的時間,或者是伺服器未回應。
描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。
例外詳細資訊: System.Data.SqlClient.SqlException: 已超過連接逾時的設定。在作業完成之前超過逾時等待的時間,或者是伺服器未回應。
原因是預設SQL執行時間為30秒,那要拉長執行時間如何做呢?
在SqlDataSource1的Selecting事件加上程式碼

程式碼如下
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.CommandTimeout = 90;
}





