之前一直没做过跟硬件打交道的,现在在做一个图书自动化信息管理系统,采用RFID技术,当我在借书时,由设备把图书信息读到左边的DataGridView中,然后点击“借书”,根据左边DataGridView中的数据,即标签信息,给标签添加已借标志,写标签成功后,添加到右边的DataGridView中。想过用三个Timer控件和一个线程,第一个Timer1控件读取图书信息,写入左边的DataGridView中(这个没问题),点"借书"时,同时启用Timer2,Timer3,Timer2用来写标签“已借”状态,,由于命令发到设备到标签写完要几秒时间,写标签时,加入了Thread.Sleep(5000),Timer3用于读取写状态成功的标签。可这样有的标签写成功了,有的却没有,写成功的也没有在右边的DataGridView中显示出来
private void Form1_Load(object sender, EventArgs e) //打开串口
{
device_no = Open_Comm_Port(0, 1, 57600);
}
private void timer1_Tick(object sender, EventArgs e) //读取图书信息
{
if (device_no > 0)
{
int state = 1;
int valid = 0;
int frame_type = 0;
byte[] output = new byte[256];
while (state == 1)
{
state = Read_Device_Data(device_no, ref frame_type, ref valid, ref output[0]);
if (state > 0 && valid > 0 && frame_type == 54)
{
string s1 = SpaceHandle.ConvertByteToString(output);
if (!s.Contains(s1))
{
s += s1;
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].Value = s1;
}
}
} } }
private void button2_Click(object sender, EventArgs e) //确认借书按钮事件,停用timer1,启用th,timer3
{
timer1.Enabled = false;
timer2.Enabled = true;
Read_Borrow_Tag(device_no);
timer3.Enabled = true;
} private void timer2_Tick(object sender, EventArgs e)
{
if (dataGridView1.RowCount > 0)
{
byte[] output = new byte[20];
output = System.Text.Encoding.Default.GetBytes(dataGridView1.Rows[0].Cells[0].Value.ToString()); Write_Borrow_Tag(device_no, ref output[0]); //写借书标志
System.Threading.Thread.Sleep(5000);
}
}
private void timer3_Tick(object sender, EventArgs e)
{
if (dataGridView1.RowCount > 0)
{
if (device_no > 0)
{
int state = 1;
int valid = 0;
int frame_type = 0;
byte[] output = new byte[256];
while (state == 1)
{
state = Read_Device_Data(device_no, ref frame_type, ref valid, ref output[0]);
if (state > 0 && valid > 0 && frame_type == 55)
{
string s1 = SpaceHandle.ConvertByteToString(output);
if (!s2.Contains(s1))
{
s2 += s1;
dataGridView2.Rows.Add();
dataGridView2.Rows[dataGridView2.RowCount - 1].Cells[0].Value = s1;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == s1)
{
dataGridView1.Rows.RemoveAt(i);
break;
}
}
} }
}
}
}
else
{
timer3.Enabled = false;
}
}想想完全可以用三个线程来完成,第一个线程用于读图书借书信息,在From_Load()中启用,在单击"确认借书"后停用,
第二个线程写借书状态标签,第三个线程用于读取写成功的标签到右边的DataGridView.
private void Form1_Load(object sender, EventArgs e) //打开串口
{
device_no = Open_Comm_Port(0, 1, 57600);
}
private void timer1_Tick(object sender, EventArgs e) //读取图书信息
{
if (device_no > 0)
{
int state = 1;
int valid = 0;
int frame_type = 0;
byte[] output = new byte[256];
while (state == 1)
{
state = Read_Device_Data(device_no, ref frame_type, ref valid, ref output[0]);
if (state > 0 && valid > 0 && frame_type == 54)
{
string s1 = SpaceHandle.ConvertByteToString(output);
if (!s.Contains(s1))
{
s += s1;
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].Value = s1;
}
}
} } }
private void button2_Click(object sender, EventArgs e) //确认借书按钮事件,停用timer1,启用th,timer3
{
timer1.Enabled = false;
timer2.Enabled = true;
Read_Borrow_Tag(device_no);
timer3.Enabled = true;
} private void timer2_Tick(object sender, EventArgs e)
{
if (dataGridView1.RowCount > 0)
{
byte[] output = new byte[20];
output = System.Text.Encoding.Default.GetBytes(dataGridView1.Rows[0].Cells[0].Value.ToString()); Write_Borrow_Tag(device_no, ref output[0]); //写借书标志
System.Threading.Thread.Sleep(5000);
}
}
private void timer3_Tick(object sender, EventArgs e)
{
if (dataGridView1.RowCount > 0)
{
if (device_no > 0)
{
int state = 1;
int valid = 0;
int frame_type = 0;
byte[] output = new byte[256];
while (state == 1)
{
state = Read_Device_Data(device_no, ref frame_type, ref valid, ref output[0]);
if (state > 0 && valid > 0 && frame_type == 55)
{
string s1 = SpaceHandle.ConvertByteToString(output);
if (!s2.Contains(s1))
{
s2 += s1;
dataGridView2.Rows.Add();
dataGridView2.Rows[dataGridView2.RowCount - 1].Cells[0].Value = s1;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == s1)
{
dataGridView1.Rows.RemoveAt(i);
break;
}
}
} }
}
}
}
else
{
timer3.Enabled = false;
}
}想想完全可以用三个线程来完成,第一个线程用于读图书借书信息,在From_Load()中启用,在单击"确认借书"后停用,
第二个线程写借书状态标签,第三个线程用于读取写成功的标签到右边的DataGridView.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货