发送记录3次的逻辑。 [英] logic for sending a record 3 times.

查看:91
本文介绍了发送记录3次的逻辑。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的c#winforms中,我被击中,发送了3次记录。



i的数据为:$ M121,响应(0/1 /任何东西)



if(responce == 0)

//从excel发送记录

else if(responce == 1)

//停止发送记录

else

(对于任何事情)

发送当前记录3次,每次需要检查响应是否是0/1 /任何东西。
$ 3 $ b 3次后再次响应是然后跳过该记录发送任何内容,并继续使用excel中的下一条记录。

这里是代码:

hi

in my c# winforms , am strucked at ,to send a record 3 times.

i have data as:$M121,responce(0/1/ anything)

if(responce==0)
//send record from excel
else if(responce==1)
//stop sending records
else
(for anything)
send the current record 3 times ,for each time need to check the responce whether it's 0/1/anything.
after 3 times if again responce is anything then skip that record sending,and continue with next record in excel.
here is the code:

  private void Upload_Click(object sender, EventArgs e)
       {

           Excel.Application xlApp = new Excel.Application();
           Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
           Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
           Excel.Range xlRange = xlWorksheet.UsedRange;


           int rowCount = xlRange.Rows.Count;
           int colCount = xlRange.Columns.Count;
           // progressBar1.Visible = true;

           int k = 100 / rowCount;
           int i = 2;

           PostCmdStart = 1;
           rspCode=0;

           if (rspCode == 0 )
           {
               PostCmdStart = 0;
               for (int j = 1; j <= colCount; j++)
               {

                  // if (j == 1)
                      // machId = xlRange.Cells[i, j].Value.ToString();


                   //upload1 = xlRange.Cells[i, j].Value.ToString();
                   if (j == 1)
                       custId = xlRange.Cells[i, j].Value.ToString();


                   if (j == 2)
                       name = xlRange.Cells[i, j].Value.ToString();

                   if (j == 3)
                       totalBal = xlRange.Cells[i, j].Value.ToString();

                   if (j == 4)
                       paid = xlRange.Cells[i, j].Value.ToString();

                   if (j == 5)
                       lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy");
                   //MessageBox.Show("lastpaid:"+lastPaid);

                   if (j == 6)
                       due = xlRange.Cells[i, j].Value.ToString();


                   final = "$ POST," + textBox3.Text.ToString() + "," + i + "," + rowCount + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # ";


               }

               if (serialPort1.IsOpen)
               {
                   String machId = textBox3.Text.ToString();
                   if (machId.Length <= 5 && custId.Length <= 10 && name.Length <= 15 && totalBal.Length <= 7 && paid.Length <= 7 && lastPaid.Length <= 14 && due.Length <= 7)
                       serialPort1.Write(final);


               }
               progressBar1.Invoke((MethodInvoker)delegate
               {
                   if (progressBar1.Value < 100)
                       progressBar1.Value += k;
               });




           }

           Thread uploadThread = new Thread(new ThreadStart
         (threadUpload));

           uploadThread.Start();
        //   run.Set();


          // MessageBox.Show("current thread:"+Thread.CurrentContext);
        // MessageBox.Show("current thread123:" + Thread.CurrentThread);
           //run.WaitOne();
           //run.Set();

       }
public void threadUpload()
       {
           Excel.Application xlApp = new Excel.Application();
           Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
           Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
           Excel.Range xlRange = xlWorksheet.UsedRange;
           string[] lines;


           int rowCount = xlRange.Rows.Count;
           int colCount = xlRange.Columns.Count;
          // progressBar1.Visible = true;

           int k = 100 / rowCount;
           int i = 3;
           int count = 0;

              run.WaitOne();
               if (PostCmdStart==0)
               {
                   s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                   lines = Regex.Split(s, "[$#,]");
                   rspCode=Convert.ToInt32(lines[2]);
               }

               if (rspCode == 0 && i <= rowCount+3)
               {
                   PostCmdStart = 0;
                   for (int j = 1; j <= colCount; j++)
                   {

                       // if (j == 1)
                       // machId = xlRange.Cells[i, j].Value.ToString();


                       //upload1 = xlRange.Cells[i, j].Value.ToString();
                       if (j == 1)
                           custId = xlRange.Cells[i, j].Value.ToString();


                       if (j == 2)
                           name = xlRange.Cells[i, j].Value.ToString();

                       if (j == 3)
                           totalBal = xlRange.Cells[i, j].Value.ToString();

                       if (j == 4)
                           paid = xlRange.Cells[i, j].Value.ToString();

                       if (j == 5)
                           lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy");
                       //MessageBox.Show("lastpaid:"+lastPaid);

                       if (j == 6)
                           due = xlRange.Cells[i, j].Value.ToString();


                       final = "$ POST," + textBox3.Text.ToString() + "," + i + "," + rowCount + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # ";


                   }

                   if (serialPort1.IsOpen)
                   {
                       String machId = textBox3.Text.ToString();

                       if (machId.Length <= 5 && custId.Length <= 10 && name.Length <= 15 && totalBal.Length <= 7 && paid.Length <= 7 && lastPaid.Length <= 14 && due.Length <= 7)
                           serialPort1.Write(final);


                   }
                   progressBar1.Invoke((MethodInvoker)delegate
                   {
                       if (progressBar1.Value < 100)
                           progressBar1.Value += k;
                   });


                 rowno= i++;

               }

               else
                   if (rspCode == 1 && i <= rowCount)
                       return;

                   else
                   {//here i need to send the current record 3 times each time check the responce
                       if (count == 3)
                           return;
                       count++;

                       i = rowno;


                   }   // progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value += 100; });


       }

推荐答案

M121,响应(0/1 /任何东西)



if(responce == 0)

//从excel发送记录

否则if(responce == 1)

//停止发送记录

else

(任何东西)

发送当前记录3次,每次需要检查响应是否是0/1 /任何东西。
3次后
再次响应是什么然后跳过那个记录发送,并继续使用excel中的下一条记录。

这里是代码:
M121,responce(0/1/ anything)

if(responce==0)
//send record from excel
else if(responce==1)
//stop sending records
else
(for anything)
send the current record 3 times ,for each time need to check the responce whether it's 0/1/anything.
after 3 times if again responce is anything then skip that record sending,and continue with next record in excel.
here is the code:
  private void Upload_Click(object sender, EventArgs e)
       {

           Excel.Application xlApp = new Excel.Application();
           Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
           Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
           Excel.Range xlRange = xlWorksheet.UsedRange;


           int rowCount = xlRange.Rows.Count;
           int colCount = xlRange.Columns.Count;
           // progressBar1.Visible = true;

           int k = 100 / rowCount;
           int i = 2;

           PostCmdStart = 1;
           rspCode=0;

           if (rspCode == 0 )
           {
               PostCmdStart = 0;
               for (int j = 1; j <= colCount; j++)
               {

                  // if (j == 1)
                      // machId = xlRange.Cells[i, j].Value.ToString();


                   //upload1 = xlRange.Cells[i, j].Value.ToString();
                   if (j == 1)
                       custId = xlRange.Cells[i, j].Value.ToString();


                   if (j == 2)
                       name = xlRange.Cells[i, j].Value.ToString();

                   if (j == 3)
                       totalBal = xlRange.Cells[i, j].Value.ToString();

                   if (j == 4)
                       paid = xlRange.Cells[i, j].Value.ToString();

                   if (j == 5)
                       lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy");
                   //MessageBox.Show("lastpaid:"+lastPaid);

                   if (j == 6)
                       due = xlRange.Cells[i, j].Value.ToString();


                   final = "


POST, + textBox3.Text.ToString()+ + i + + rowCount + < span class =code-string>, + custId + + name + + totalBal + +付费+ + lastPaid + + due + ;


}

if (serialPort1.IsOpen)
{
String machId = textBox3.Text.ToString();
if (machId.Length < = 5 && custId.Length < = 10 && name。长度< = 15 && totalBal.Length < = 7 && paid.Length < = 7 && lastPaid.Length < = 14 && due.Length < = 7
serialPort1.Write(final) ;


}
progressBar1.Invoke((MethodInvoker)委托
{
if (progressBar1.Value < 100
progressBar1.Value + = k;
});




}

线程uploadThread = 线程(< span class =code-keyword> new ThreadStart
(threadUpload));

uploadThread.Start();
// run.Set();


// MessageBox.Show(当前主题:+ Thread.CurrentContext);
// MessageBox.Show(当前thread123:+ Thread.CurrentThread);
// run.WaitOne();
// run.Set();

}
public void threadUpload()
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0 true 5 true ,Excel.XlPlatform.xlWindows , \t false false 0 true ,< span class =code-digit> 1 , 0 );
Excel._Worksheet xlWorksheet =(Excel._Worksheet)xlWorkbook.Sheets [ 1 ];
Excel.Range xlRange = xlWorksheet.UsedRange;
string []行;


int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
// progressBar1.Visible = true;

int k = 100 / rowCount;
int i = 3 ;
int count = 0 ;

run.WaitOne();
if (PostCmdStart == 0
{
s = System .Text.ASCIIEncoding.ASCII.GetString(缓冲液);
lines = Regex.Split(s,
POST," + textBox3.Text.ToString() + "," + i + "," + rowCount + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # "; } if (serialPort1.IsOpen) { String machId = textBox3.Text.ToString(); if (machId.Length <= 5 && custId.Length <= 10 && name.Length <= 15 && totalBal.Length <= 7 && paid.Length <= 7 && lastPaid.Length <= 14 && due.Length <= 7) serialPort1.Write(final); } progressBar1.Invoke((MethodInvoker)delegate { if (progressBar1.Value < 100) progressBar1.Value += k; }); } Thread uploadThread = new Thread(new ThreadStart (threadUpload)); uploadThread.Start(); // run.Set(); // MessageBox.Show("current thread:"+Thread.CurrentContext); // MessageBox.Show("current thread123:" + Thread.CurrentThread); //run.WaitOne(); //run.Set(); } public void threadUpload() { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1]; Excel.Range xlRange = xlWorksheet.UsedRange; string[] lines; int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; // progressBar1.Visible = true; int k = 100 / rowCount; int i = 3; int count = 0; run.WaitOne(); if (PostCmdStart==0) { s = System.Text.ASCIIEncoding.ASCII.GetString(buffer); lines = Regex.Split(s, "


#, ]);
rspCode = Convert.ToInt32(行[ 2 ]);
}

如果(rspCode == 0 && ; i < = rowCount + 3)
{
PostCmdStart = 0 ;
for int j = 1 ; j < = colCount; j ++)
{

// if(j == 1)
// machId = xlRange.Cells [i,j] .Value.ToString();


// upload1 = xlRange.Cells [i,j] .Value.ToString();
if (j == 1
custId = xlRange.Cells [i,j] .Value.ToString();


if (j == 2
name = xlRange.Cells [i,j] .Value.ToString();

if (j == 3
totalBal = xlRange .Cells [i,j] .Value.ToString();

if (j == 4
paid = xlRange .Cells [i,j] .Value.ToString();

if (j == 5
lastPaid = xlRange .Cells [i,j] .Value.ToString( dd / MM / yyyy);
// MessageBox.Show(lastpaid:+ lastPaid);

if (j == 6
到期= xlRange.Cells [i ,j] .Value.ToString();


final =
#,]"); rspCode=Convert.ToInt32(lines[2]); } if (rspCode == 0 && i <= rowCount+3) { PostCmdStart = 0; for (int j = 1; j <= colCount; j++) { // if (j == 1) // machId = xlRange.Cells[i, j].Value.ToString(); //upload1 = xlRange.Cells[i, j].Value.ToString(); if (j == 1) custId = xlRange.Cells[i, j].Value.ToString(); if (j == 2) name = xlRange.Cells[i, j].Value.ToString(); if (j == 3) totalBal = xlRange.Cells[i, j].Value.ToString(); if (j == 4) paid = xlRange.Cells[i, j].Value.ToString(); if (j == 5) lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy"); //MessageBox.Show("lastpaid:"+lastPaid); if (j == 6) due = xlRange.Cells[i, j].Value.ToString(); final = "


这篇关于发送记录3次的逻辑。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆