这几天的问题依旧没解决,我是否不太适合做这一行了。我是一名普通学校的实习生,这个其实是我的第一个项目任务,完成一个上位机开发。我现在把我写的Visual studio代码放在这了。如果今天还是没完成,我在考虑是否要辞职了。可惜了我这三周的工作时长了,但是我真的受不了天天对着完成不了的任务了。
望有人个给我个方向,或者帮我看看有没有解决的方法。 一名物联网工程专业人未毕业但是想从事嵌入式的学生
这是我的数据显示函数
private void analysisdata()//数据接收分析
{
//if ((Readbuff[0] == 0x55) && (Readbuff[19] == 0xAA))
//{
//int checksum = 0;
//for (int m = 1; m < 18; m++)
//{
// checksum += Readbuff[m];
//}
//byte check = (byte)checksum;
//if ((check == Readbuff[18]) && ((Readbuff[3] & 0X08) == 0X08))
{
int rev;
int stack;
int softstart;
textBox1.Text = Readbuff[1].ToString();
textBox5.Text = Readbuff[2].ToString();
textBox7.Text = Readbuff[4].ToString();
textBox9.Text = Readbuff[5].ToString();
textBox10.Text = Readbuff[6].ToString();
textBox11.Text = Readbuff[15].ToString();
rev = Readbuff[3];
rev &= 0x01;
if (rev == 0x01)
{
textBox8.Text = 1.ToString();
}
else
{
textBox8.Text = 0.ToString();
}
stack = Readbuff[3];
stack &= 0x10;
if (stack == 0x10)
{
textBox13.Text = 1.ToString();
}
else
{
textBox13.Text = 0.ToString();
}
softstart = Readbuff[3];
softstart = softstart & 0X20;
if (softstart == 0X20)
{
textBox14.Text = 1.ToString();
}
else
{
textBox14.Text = 0.ToString();
}
textBox2.Text = (Readbuff[11] + (Readbuff[12] << 8)).ToString();
textBox3.Text = (Readbuff[7] + (Readbuff[8] << 8)).ToString();
textBox4.Text = (Readbuff[9] + (Readbuff[10] << 8)).ToString();
textBox6.Text = (Readbuff[13] + (Readbuff[14] << 8)).ToString();
textBox12.Text = (Readbuff[16] + (Readbuff[17] << 8)).ToString();
textBox15.Text = (textBox15.ToString() + "读取成功!!");
readbit = false;
readornot = false;
return;
}
//}
}
这个是我的读取数据按钮
private void button3_Click(object sender, EventArgs e)//参数读取
{
panel1.Visible=false;
Thread.Sleep(100);
panel1.Visible = true;
byte[] command = new byte[] { 0x55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xAA };
int receiveData = serialPort1.BytesToRead; //读取缓存区字节数目
textBox15.Text = Convert.ToString(receiveData);
DateTime ReadStartTime = DateTime.Now;
//int i = 0;
// for(i=0;i<18;i++)
//{
// command[i+1] = Readbuff[i+1];
//}
if (serialPort1.IsOpen)
{
try
{
serialPort1.Close();
//MessageBox.Show("请检查串口!!");
}
catch
{
}
}
try
{
serialPort1.PortName=comboBox1.Text;
serialPort1.Open();
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
}
catch
{
textBox15.Text = (textBox15.ToString() + "打开串口失败!!");
return;
}
if(serialPort1.IsOpen)
{
try
{
analysisdata();
//serialPort1.Write(command, 0, command.Length);
}
catch
{
MessageBox.Show("读取失败,请检查串口!!");
return ;
}
Thread.Sleep(100);
serialPort1.DiscardInBuffer();
readbit = true;
Thread.Sleep(100);
}
else
{
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
MessageBox.Show("读取失败,请检查串口!!");
return;
}
}
这个是我的串口接收函数
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)//数据处理并判断
{
int receiveData = serialPort1.BytesToRead; //读取缓存区字节数目
textBox15.Text = Convert.ToString(receiveData);
DateTime ReadStartTime = DateTime.Now;
byte[] rdata = null;
Thread.Sleep(100);
// if (serialPort1.BytesToRead < 20)
// return;
rdata = new byte[serialPort1.BytesToRead];
try
{
serialPort1.Read(rdata, 0, rdata.Length);
}
catch
{
MessageBox.Show("写入数据失败,请检查串口号", "失误");
}
int i = 0;
foreach (byte member in rdata)
{
Readbuff[i] = (byte)member;
i++;
}
if ((Readbuff[0] == 0X55) && (Readbuff[19] == 0XAA))
{
int icnt;
int checksum = 0;
for (icnt = 1; icnt < 18; icnt++)
{
checksum += Readbuff[icnt];
}
byte check = (byte)checksum;
if ((Readbuff[18] == check) && ((Readbuff[3] & 0X04) == 0X04))
{
writeornot = true;
}
}
else
{
MessageBox.Show("写入数据失败,请检查数据", "失误");
}
// Thread.Sleep(100);
//serialPort1.DiscardInBuffer();
//serialPort1.DiscardOutBuffer();
//analysisdata();
//Thread.Sleep(100);
}