详细代码
using System;
 using System.Drawing;
 using System.Collections;
 using System.ComponentModel;
 using System.Windows.Forms;
 using System.Data; 
 using System.Text;
 namespace POS机C井例子代码
 {
  /// <summary>
  /// Form2 的摘要说明。
  /// </summary>
  public class Form2 : System.Windows.Forms.Form
  {
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.Button button2;
   private System.Windows.Forms.Button button3;
   private System.Windows.Forms.Button button4;
   private System.Windows.Forms.Button button5;
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Label label2;
   private System.Windows.Forms.Label label3;
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.TextBox textBox2;
   private System.Windows.Forms.TextBox textBox3;
   private System.Windows.Forms.Button button6;
   private System.Windows.Forms.Label label4;
   private System.Windows.Forms.Button button7;
   private System.Windows.Forms.Label label5;
   private System.Windows.Forms.ListView listView1;
   private Microsoft.VisualBasic.Compatibility.VB6.DriveListBox driveListBox1;
   private System.Windows.Forms.ProgressBar progressBar2;
   private System.Windows.Forms.ColumnHeader columnHeader1;
   private System.Windows.Forms.ColumnHeader columnHeader2;
   private System.Windows.Forms.ColumnHeader columnHeader3;
   private System.Windows.Forms.ColumnHeader columnHeader4;
   private System.Windows.Forms.ColumnHeader columnHeader5;
   private System.Windows.Forms.ColumnHeader columnHeader6;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;
//  bool iscontinue ;     //定义全局变量,继续功能
   
  public Form2()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
   //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }
  /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if(components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }
  #region Windows 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    this.button5 = new System.Windows.Forms.Button();
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.button6 = new System.Windows.Forms.Button();
    this.label4 = new System.Windows.Forms.Label();
    this.button7 = new System.Windows.Forms.Button();
    this.label5 = new System.Windows.Forms.Label();
    this.progressBar2 = new System.Windows.Forms.ProgressBar();
    this.listView1 = new System.Windows.Forms.ListView();
    this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
    this.driveListBox1 = new Microsoft.VisualBasic.Compatibility.VB6.DriveListBox();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(16, 16);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(80, 32);
    this.button1.TabIndex = 0;
    this.button1.Text = "采集记录";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(16, 104);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(80, 32);
    this.button2.TabIndex = 1;
    this.button2.Text = "删除记录";
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(120, 104);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(88, 32);
    this.button3.TabIndex = 2;
    this.button3.Text = "清空列表";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // button4
    // 
    this.button4.Location = new System.Drawing.Point(664, 56);
    this.button4.Name = "button4";
    this.button4.Size = new System.Drawing.Size(88, 32);
    this.button4.TabIndex = 3;
    this.button4.Text = "暂停";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    // 
    // button5
    // 
    this.button5.Location = new System.Drawing.Point(352, 104);
    this.button5.Name = "button5";
    this.button5.Size = new System.Drawing.Size(80, 32);
    this.button5.TabIndex = 4;
    this.button5.Text = "刷 新";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(112, 24);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(80, 23);
    this.label1.TabIndex = 5;
    this.label1.Text = "记录总数";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(112, 64);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(88, 23);
    this.label2.TabIndex = 6;
    this.label2.Text = "未上传总金额";
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(272, 24);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(72, 23);
    this.label3.TabIndex = 7;
    this.label3.Text = "总上传帧数";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(168, 24);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(80, 21);
    this.textBox1.TabIndex = 8;
    this.textBox1.Text = "";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(192, 64);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(88, 21);
    this.textBox2.TabIndex = 9;
    this.textBox2.Text = "";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(336, 24);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(64, 21);
    this.textBox3.TabIndex = 10;
    this.textBox3.Text = "";
    // 
    // button6
    // 
    this.button6.Location = new System.Drawing.Point(536, 104);
    this.button6.Name = "button6";
    this.button6.Size = new System.Drawing.Size(96, 40);
    this.button6.TabIndex = 11;
    this.button6.Text = "从U盘提取记录";
    this.button6.Click += new System.EventHandler(this.button6_Click);
    // 
    // label4
    // 
    this.label4.Location = new System.Drawing.Point(280, 64);
    this.label4.Name = "label4";
    this.label4.Size = new System.Drawing.Size(48, 23);
    this.label4.TabIndex = 12;
    this.label4.Text = "分钱";
    // 
    // button7
    // 
    this.button7.Location = new System.Drawing.Point(664, 104);
    this.button7.Name = "button7";
    this.button7.Size = new System.Drawing.Size(88, 40);
    this.button7.TabIndex = 14;
    this.button7.Text = "生成删除记录许可文件";
    this.button7.Click += new System.EventHandler(this.button7_Click);
    // 
    // label5
    // 
    this.label5.Location = new System.Drawing.Point(568, 32);
    this.label5.Name = "label5";
    this.label5.Size = new System.Drawing.Size(136, 23);
    this.label5.TabIndex = 16;
    this.label5.Text = "已采集";
    // 
    // progressBar2
    // 
    this.progressBar2.Location = new System.Drawing.Point(432, 24);
    this.progressBar2.Maximum = 10000;
    this.progressBar2.Name = "progressBar2";
    this.progressBar2.Size = new System.Drawing.Size(120, 24);
    this.progressBar2.TabIndex = 17;
    // 
    // listView1
    // 
    this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                       this.columnHeader1,
                       this.columnHeader2,
                       this.columnHeader3,
                       this.columnHeader4,
                       this.columnHeader5,
                       this.columnHeader6});
    this.listView1.GridLines = true;
    this.listView1.Location = new System.Drawing.Point(16, 160);
    this.listView1.Name = "listView1";
    this.listView1.Size = new System.Drawing.Size(736, 296);
    this.listView1.TabIndex = 18;
    this.listView1.View = System.Windows.Forms.View.Details;
    // 
    // columnHeader1
    // 
    this.columnHeader1.Text = "卡号";
    // 
    // columnHeader2
    // 
    this.columnHeader2.Text = "充值次数";
    // 
    // columnHeader3
    // 
    this.columnHeader3.Text = "余额";
    // 
    // columnHeader4
    // 
    this.columnHeader4.Text = "消费额";
    // 
    // columnHeader5
    // 
    this.columnHeader5.Text = "消费时间";
    this.columnHeader5.Width = 111;
    // 
    // columnHeader6
    // 
    this.columnHeader6.Text = "餐次";
    this.columnHeader6.Width = 67;
    // 
    // driveListBox1
    // 
    this.driveListBox1.Location = new System.Drawing.Point(440, 104);
    this.driveListBox1.Name = "driveListBox1";
    this.driveListBox1.Size = new System.Drawing.Size(88, 22);
    this.driveListBox1.TabIndex = 19;
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(840, 478);
    this.Controls.Add(this.driveListBox1);
    this.Controls.Add(this.listView1);
    this.Controls.Add(this.progressBar2);
    this.Controls.Add(this.label5);
    this.Controls.Add(this.button7);
    this.Controls.Add(this.label4);
    this.Controls.Add(this.button6);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.button5);
    this.Controls.Add(this.button4);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.Load += new System.EventHandler(this.Form2_Load);
    this.ResumeLayout(false);
  }
   #endregion
   bool iscontinue1 ;
  private void button4_Click(object sender, System.EventArgs e)
   {
    iscontinue1 =false;
   }
  private void button1_Click(object sender, System.EventArgs e)//采集记录
   {
   
    Form1 f1 ;
    f1 = (Form1)this.Owner;
    f1.Refresh();
   ListViewItem  list1;
    int i;
if (f1.axPOS011.DevComPortOpen == 1)
   {
    
      f1.axPOS011.DevNumber = Convert.ToInt32(f1.textBox16.Text);
      f1.axPOS011.DevRecTotalRead();
                    if(f1.axPOS011.OperatingResults == 0)
       {
                  textBox2.Text = Convert.ToString(f1.axPOS011.DevRecAmountTotal);        // 累计金额数
                   
         textBox1.Text =Convert.ToString( f1.axPOS011.DevRecTotal) ;             // 总记录数
         textBox3.Text =Convert.ToString( f1.axPOS011.DevRecFrames ) ;           //记录总帧数p
                                  //以下代码开始采集记录
         if  (f1.axPOS011.DevRecFrames > 0)
         {
          iscontinue1=true;
          
          this.progressBar2.Maximum =f1.axPOS011.DevRecFrames;
          for(i=0;i<=f1.axPOS011.DevRecFrames -1 ;i++)
          {
           f1.axPOS011.DevRecFramePoint =i ;       //设定当前需要采集的帧指针
           f1.axPOS011.DevRecFrameRead()   ;         //执行采集通讯
           if(f1.axPOS011.OperatingResults != 0)        //表示通讯错误,采集中断
             {
              MessageBox.Show ( "通讯错误,退出,错误代码为" + Convert.ToString(f1.axPOS011.OperatingResults));
             }
           if (iscontinue1==false)
             {
              break ;
            }
           //以下可以做进度条算法
          this.progressBar2.Value = i + 1;
           label5.Text = "已采集" + Convert.ToString(i + 1);
          
          }
          if  (this.progressBar2.Value == f1.axPOS011.DevRecFrames)
           {
              if (f1.axPOS011.OperatingResults == 0) 
              {
               //采集全部成功
               if (f1.axPOS011.DevRecTotal > 0)
               {
                //下面为提取数据
                
                                 
                for (i = 0; i<=f1.axPOS011.DevRecTotal - 1;i++)
                {
                 f1.axPOS011.DevRecPoint = i     ;     //设定当前记录指针,用于提取记录数据,
                 //执行完本语句后,以下数据才有效
                                     list1 = new ListViewItem(f1.axPOS011.DevRecCustomNumberStr);//卡号
        
                 list1.SubItems.Add(Convert.ToString(f1.axPOS011.DevRecRechargeTimes)) ;       //充值次数
                                      
                 list1.SubItems.Add(Convert.ToString (f1.axPOS011.DevRecCardMoney));        //余额
                                      
                 list1.SubItems.Add(Convert.ToString(f1.axPOS011.DevRecConsumption) )  ;        //当次消费额
                 list1.SubItems.Add(  Convert.ToString(f1.axPOS011.DevRecTimeYear) + "-" + Convert.ToString(f1.axPOS011.DevRecTimeMonth) + "-" + Convert.ToString(f1.axPOS011.DevRecTimeDay) + " " + Convert.ToString(f1.axPOS011.DevRecTimeHour) + ":" + Convert.ToString(f1.axPOS011.DevRecTimeMinute) + ":" + Convert.ToString(f1.axPOS011.DevRecTimeSecond));
                 list1.SubItems.Add (Convert.ToString(f1.axPOS011.DevRecMealType));
                 listView1.Items.Add(list1);  
                                                                           
                }
                       }
             
}
}
          }
        }
      else
      {
       MessageBox.Show("设备通讯不通!");
}
}
    else 
    {
     MessageBox.Show("端口未打开!");
   }
                  
                           
                           
            
                       
             
   
          
   }
  private void button3_Click(object sender, System.EventArgs e)
   {
    listView1.Items.Clear();
   }
  private void button6_Click(object sender, System.EventArgs e)//从U盘提取记录
   {
    ListViewItem  list1;
    int i;
   Form1 f1 ;
    f1 = (Form1)this.Owner;
    f1.Refresh();
   f1.axPOS011.DevDriverStr = driveListBox1.Drive;                     //U盘
    f1.axPOS011.DevNumber = Convert.ToInt32(f1.textBox16.Text)  ;       //机号
f1.axPOS011.DevRecReadFromDisk();
   if (f1.axPOS011.OperatingResults == 0 )
    {
     textBox1.Text = Convert.ToString (f1.axPOS011.DevRecTotal);          //总记录数
textBox2.Text = Convert.ToString(f1.axPOS011.DevRecAmountTotal) ; //累计金额数
    for (i = 0;i< f1.axPOS011.DevRecTotal;i++)
          {
      
        f1.axPOS011.DevRecPoint = i ;                            //设定当前记录指针,用于提取记录数据,
        //执行完本语句后,以下数据才有效
       list1 = new ListViewItem(f1.axPOS011.DevRecCustomNumberStr);                  //卡号
         
        list1.SubItems.Add(Convert.ToString(f1.axPOS011.DevRecRechargeTimes)) ;       //充值次数
                     
        list1.SubItems.Add(Convert.ToString (f1.axPOS011.DevRecCardMoney));           //余额
                     
        list1.SubItems.Add(Convert.ToString(f1.axPOS011.DevRecConsumption) )  ;       //当次消费额
        list1.SubItems.Add(  Convert.ToString(f1.axPOS011.DevRecTimeYear) + "-" + Convert.ToString(f1.axPOS011.DevRecTimeMonth) + "-" + Convert.ToString(f1.axPOS011.DevRecTimeDay) + " " + Convert.ToString(f1.axPOS011.DevRecTimeHour) + ":" + Convert.ToString(f1.axPOS011.DevRecTimeMinute) + ":" + Convert.ToString(f1.axPOS011.DevRecTimeSecond));
        list1.SubItems.Add (Convert.ToString(f1.axPOS011.DevRecMealType));
        listView1.Items.Add(list1);  
      
           
  
           }
     MessageBox.Show ( "从U盘提取记录成功!");
    }
    else if (f1.axPOS011.OperatingResults == 40)
     {
      MessageBox.Show("该机号对应的记录文件不存在");
    }
    else if (f1.axPOS011.OperatingResults == 45)
     {
      MessageBox.Show("文件不是该机号设备生成的文件!" );
     }
    else
     {
      MessageBox.Show( Convert.ToString (f1.axPOS011.OperatingResults));
     }
    
   }
//  private void button8_Click(object sender, System.EventArgs e)
 //  {
 //   ListViewItem  list1;
 //   list1 = new ListViewItem("0");     //卡号
 //   list1.SubItems.Add("1") ;          //充值次数
 //   list1.SubItems.Add("2");           //余额
 //   list1.SubItems.Add("3");           //当次消费额
 //   list1.SubItems.Add("4");
 //   list1.SubItems.Add("5");
 //   listView1.Items.Add(list1); 
 //  }
  private void Form2_Load(object sender, System.EventArgs e)
   {
   
   }
  private void button7_Click(object sender, System.EventArgs e)
   {
   
    Form1 f1 ;
    f1 = (Form1)this.Owner;
    f1.Refresh();
      f1.axPOS011.DevDriverStr = driveListBox1.Drive;
    f1.axPOS011.DevNumber = Convert.ToInt32(f1.textBox16.Text);
    f1.axPOS011.DevRecClearFileToDisk();
       
    if (f1.axPOS011.OperatingResults == 0)
     {
        MessageBox.Show ( "写记录删除许可文件到U盘成功!");
     }
    else  if( f1.axPOS011.OperatingResults == 46 )
       {
        MessageBox.Show( "尚未提取文件记录!");
      }
    else
       {
        MessageBox.Show(Convert.ToString (f1.axPOS011.OperatingResults));
       }
   
  }
   
  }
 }
转载于:https://blog.51cto.com/reader/597052