急!求教我写的程序运行一段时间后,程序未响应,如果您继续等待或许响应。

兔子家族-二哥 2015-04-09 02:27:52
源代码如下:
public WorkForm()
{
InitializeComponent();
}
System.Threading.Timer t = null;
private void button2_Click(object sender, EventArgs e)
{


//MessageBox.Show(list[comboBox2.SelectedIndex]);
if (button2.Text == "开始扫描")
{
try
{
serialPort1.PortName = SearchExcelCom();
serialPort1.Open();
//使用Timer循环
t = new System.Threading.Timer(new System.Threading.TimerCallback(timerRead), null, 0, 4000);

comboBox1.Enabled = false;
comboBox2.Enabled = false;
listView1.Enabled = false;
button2.Text = "停止扫描";
}
catch (Exception ex)
{
MessageBox.Show("C+" + ex.Message);
}


}
else
{
DialogResult dr = MessageBox.Show("停止扫描将会清空列表,是否继续?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
try
{
t.Dispose();

serialPort1.Close();
comboBox1.Enabled = true;
comboBox2.Enabled = true;
button2.Text = "开始扫描";
listView1.Enabled = true;

DataTable dt = new DataTable("WorkList");

DataColumn dc4 = new DataColumn("编号", Type.GetType("System.String"));
DataColumn dc1 = new DataColumn("品号", Type.GetType("System.String"));
DataColumn dc2 = new DataColumn("名称", Type.GetType("System.String"));
DataColumn dc3 = new DataColumn("打印时间", Type.GetType("System.String"));

dt.Columns.Add(dc4);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);

dataGridView1.DataSource = dt;

//label5.ForeColor = Color.Blue;
label8.Text = "0";
}
catch (Exception ec)
{
MessageBox.Show("D+" + ec.Message);
}


}
}



}

void timerRead(object obj)
{

try
{
byte[] byteBuffer = new byte[] { 0x02, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x03, 0x35, 0x35 };

serialPort1.Write(byteBuffer, 0, byteBuffer.Length);
}
catch (Exception mes)
{
MessageBox.Show("B+" + mes.Message);
return;
}

}
void b_LabelPrinted(object sender, EventArgs e)
{

}
private void serialPort1_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (dataGridView1.Rows.Count - 1 > 0)
{
DateTime dtime = Convert.ToDateTime(dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[3].Value.ToString());

TimeSpan ts = DateTime.Now.Subtract(dtime);

if (ts.Seconds <= 5)
{
return;
}
}
byte[] rs = new byte[serialPort1.BytesToRead];
serialPort1.Read(rs, 0, rs.Length);

string qsss = "";
for (int i = 0; i < rs.Length; i++)
{
qsss += rs[i].ToString("X2") + " ";
}

rs = null;

//serialPort1.Close();
if (qsss.Equals("02 30 31 03 36 34 "))//!string.IsNullOrEmpty(rs[0].ToString())
{

string Count = "0";
if (label9.Text.Equals("晚班"))
{
Count = "1";
}

DataTable dt = (DataTable)dataGridView1.DataSource;

DataRow dr = dt.NewRow();

dr["编号"] = dataGridView1.Rows.Count.ToString();
dr["品号"] = comboBox3.Text;
dr["名称"] = comboBox2.Text;
dr["打印时间"] = DateTime.Now.ToString();
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;

this.dataGridView1.FirstDisplayedScrollingRowIndex = dt.Rows.Count - 1;
label8.Text = (dataGridView1.Rows.Count - 1).ToString();
//打印条码 }
}
catch (Exception ex)
{
MessageBox.Show("A+" + ex.Message);
}

}

...全文
628 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
以上代码会出现问题 就是有时候接收不到数据,时间长了会卡死,本来是想先write 写一个数据 硬件返回一个数据然后读取,就有时候读取不到, 他的信号存在5秒钟 我每2秒读取一次 按理说都应该读取得到的啊,而且 因为频率高我做了个防重复的验证,求指教
  • 打赏
  • 举报
回复
唉,没有大神回复了么,现在我倒是不怎么卡死了,但是有时间接收不到数据,贴出代码:

 System.Threading.Timer TimerItem;
private void button2_Click(object sender, EventArgs e)
   {
            try
                {
                    serialPort1.PortName = SearchExcelCom();
                    serialPort1.Open();
                    System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(timerRead);
                    TimerItem = new System.Threading.Timer(TimerDelegate, null, 0, 1500);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("C+" + ex.Message);
                }
            }

private void timerRead(object obj)
        {
            try
            {
                //Invoke(new Action(() =>
                      // {
                           byte[] byteBuffer = new byte[] { 0x02, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x03, 0x35, 0x35 };

                           serialPort1.Write(byteBuffer, 0, byteBuffer.Length);
                      // }));
                
                if (serialPort1.BytesToRead > 0)
                {
                    try
                    {
                        if (dataGridView1.Rows.Count - 1 > 0)
                        {
                            DateTime dtime = Convert.ToDateTime(dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[3].Value.ToString());

                            TimeSpan ts = DateTime.Now.Subtract(dtime);

                            if (ts.Seconds <= 5)
                            {
                                return;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Invoke(new Action(() =>
                        {
                            DataTable dt = (DataTable)dataGridView1.DataSource;
                            dt.Rows.RemoveAt(dt.Rows.Count - 1);
                            dataGridView1.DataSource = dt;
                            label8.Text = dt.Rows.Count.ToString();
                            timerRead(null);
                        }));
                    }

                    byte[] rs = new byte[serialPort1.BytesToRead];
                    serialPort1.Read(rs, 0, rs.Length);

                    string qsss = "";
                    for (int i = 0; i < rs.Length; i++)
                    {
                        qsss += rs[i].ToString("X2") + " ";
                    }

                    rs = null;

                    //serialPort1.Close();
                    if (qsss.Equals("02 30 31 03 36 34 "))//!string.IsNullOrEmpty(rs[0].ToString())
                    {

                        string Count = "0";
                        if (label9.Text.Equals("晚班"))
                        {
                            Count = "1";
                        }
                        Invoke(new Action(() =>
                        {
                            DataTable dt = (DataTable)dataGridView1.DataSource;

                            DataRow dr = dt.NewRow();

                            dr["编号"] = dataGridView1.Rows.Count.ToString();
                            dr["品号"] = comboBox3.Text;
                            dr["名称"] = comboBox2.Text;
                            dr["打印时间"] = DateTime.Now.ToString();
                            dt.Rows.Add(dr);
                            dataGridView1.DataSource = dt;

                            this.dataGridView1.FirstDisplayedScrollingRowIndex = dt.Rows.Count - 1;
                            label8.Text = (dataGridView1.Rows.Count - 1).ToString();

                        }));

                        ENPOT.Bartender.Print.BartenderPrinter b = new ENPOT.Bartender.Print.BartenderPrinter();
                        b.LabelPrinted += new EventHandler<EventArgs>(b_LabelPrinted);
                        string printString = "\"品号\",\"灯位\",\"条码\"";
                        printString += "\r\n\"" + list[comboBox2.SelectedIndex] + "\",";
                        printString += "\"" + PCode[comboBox2.SelectedIndex] + "\",";
                        printString += "\"" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day.ToString() + Count + Convert.ToInt32(label8.Text).ToString("000") + "\"";
                        b.Print(printString, url + @"\大茂软件\" + listView1.SelectedItems[0].Text.Split('.')[0] + ".btw", new System.Drawing.Printing.PrintDocument().PrinterSettings.PrinterName, false);
                    }
                }
            }
            catch (Exception mes)
            {
                MessageBox.Show("B+" + mes.Message);
                return;
            }
            
            
            
        }
  • 打赏
  • 举报
回复
引用 5 楼 ccm1818168 的回复:
线程 每天回帖即可获得10分可用分!小技巧:教您如何更快获得可用分
if (qsss.Equals("02 30 31 03 36 34 "))//!string.IsNullOrEmpty(rs[0].ToString()) { string Count = "0"; if (label9.Text.Equals("晚班")) { Count = "1"; } Invoke(new Action(() => { DataTable dt = (DataTable)dataGridView1.DataSource; DataRow dr = dt.NewRow(); dr["编号"] = dataGridView1.Rows.Count.ToString(); dr["品号"] = comboBox3.Text; dr["名称"] = comboBox2.Text; dr["打印时间"] = DateTime.Now.ToString(); dt.Rows.Add(dr); dataGridView1.DataSource = dt; this.dataGridView1.FirstDisplayedScrollingRowIndex = dt.Rows.Count - 1; label8.Text = (dataGridView1.Rows.Count - 1).ToString(); })); } } 我是这样 用的 对不对 啊
  • 打赏
  • 举报
回复
由于使用了System.threading.timer 我这边加 Invoke(new Action(() => { DataTable dt = (DataTable)dataGridView1.DataSource; DataRow dr = dt.NewRow(); dr["编号"] = dataGridView1.Rows.Count.ToString(); dr["品号"] = comboBox3.Text; dr["名称"] = comboBox2.Text; dr["打印时间"] = DateTime.Now.ToString(); dt.Rows.Add(dr); dataGridView1.DataSource = dt; this.dataGridView1.FirstDisplayedScrollingRowIndex = dt.Rows.Count - 1; label8.Text = (dataGridView1.Rows.Count - 1).ToString(); })); 为什么运行一段时间之后 这个 dataGridView1 会添加空的东西进去? 其他的还好说, dr["打印时间"] = DateTime.Now.ToString(); 这个也成空的了。。。。
  • 打赏
  • 举报
回复
太高端了……不懂,加日志什么的看下
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
又从端口接收数据,又通过异步向端口写数据,为什么要这么做啊?
循环从端口用serialPort1 去发送数据 获取到数据之后进行判断。 自带的Timer会导致程序界面无法响应,所以 使用System.threading.timer
  • 打赏
  • 举报
回复
又从端口接收数据,又通过异步向端口写数据,为什么要这么做啊?

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧