this.serialPort1.Write(SendBy, 0, 24);
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string poor="";
if (goor != null)
{
for (int i = 8; i < 10; i++)
{
poor += goor[i].ToString("X2");//就这里提示异常
}
}
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string poor="";
if (goor != null)
{
for (int i = 8; i < 10; i++)
{
poor += goor[i].ToString("X2");//就这里提示异常
}
}
改为
for (int i = 8; i < goor.Length; i++)
或
if(goor.Length<10) return;
for (int i = 8; i < 10; i++)
1,serialPort1.Read(goor, 0, serialPort1.BytesToRead);
2,string poor="";
不用F11,就F10就行了
当F10到1,处时,你看看goor 的length是多少?
当F10到2,处时,你看看goor 的length是多少?
如果1处是>0的,2处是goor{维数:[0]},就证明serialPort1.Read有问题,需要回零再读,因为很多端口的BytesToRead读操作只能读一次,这时候就需要你int iread=serialPort1.BytesToRead;serialPort1.Read(goor, 0, iread);如果1处是goor{维数:[0]},你自己琢磨吧,serialPort1端口要么没有数据,要么就是不是通过BytesToRead方法读长度
{
if (!this.serialPort1.IsOpen)
{
this.serialPort1.Open();
}
Byte[] Send = new Byte[1];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
byte[] tmpbuf = new byte[serialPort1.BytesToRead];
serialPort1.Read(tmpbuf, 0, serialPort1.BytesToRead);
Byte[] SendBy = new Byte[24];
byte Sum = 0;
SendBy[0] = 0x01;
SendBy[2] = 0x01;
SendBy[3] = 0x02;
SendBy[4] = 0x01;
for (int i = 0; i < 24; i++)
{
Sum = (byte)(SendBy[i] + Sum);
}
SendBy[23] = Sum;
this.serialPort1.Write(SendBy, 0, 24);
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string poor="";
if (goor != null)
{
if (goor.Length < 10) return;
for (int i = 8; i < 10; i++)
{
poor += goor[i].ToString("X2");
//"FE010001020100000744000000000000000000000000000050"
}
}
int ppo = Convert.ToInt32(poor,16);//"07 44"
string oop = Convert.ToString(ppo);
textBox2.Text = oop;
}
原代码是这样的啊...
{
if (!this.serialPort1.IsOpen)
{
this.serialPort1.Open();
Byte[] Send = new Byte[1];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
byte[] tmpbuf = new byte[serialPort1.BytesToRead];
serialPort1.Read(tmpbuf, 0, serialPort1.BytesToRead);
Byte[] SendBy = new Byte[24];
byte Sum = 0;
SendBy[0] = 0x01;
SendBy[2] = 0x01;
SendBy[3] = 0x02;
SendBy[4] = 0x01;
for (int i = 0; i < 24; i++)
{
Sum = (byte)(SendBy[i] + Sum);
}
SendBy[23] = Sum;
this.serialPort1.Write(SendBy, 0, 24);
}
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string poor="";
if (goor != null)
{
if (goor.Length < 10) return;
for (int i = 8; i < 10; i++)
{
poor += goor[i].ToString("X2");
//"FE010001020100000744000000000000000000000000000050"
}
}
int ppo = Convert.ToInt32(poor,16);//"07 44"
string oop = Convert.ToString(ppo);
textBox2.Text = oop;
}
刚刚运行的结果,我把它改了下才实现的啊...
SendBy[23] = Sum;
this.serialPort1.Write(SendBy, 0, 24);
----加个延时看看
byte[] goor = new byte[serialPort1.BytesToRead];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
byte[] tmpbuf = new byte[serialPort1.BytesToRead];
serialPort1.Read(tmpbuf, 0, serialPort1.BytesToRead);
如果你上次写入0xAA,读取后
this.serialPort1.Write(SendBy, 0, 24);
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
再次写入SendBy,读取也正常的话
应该没有问题,你可以在后面再重复这么一段
Byte[] Send = new Byte[1];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
byte[] tmpbuf = new byte[serialPort1.BytesToRead];
serialPort1.Read(tmpbuf, 0, serialPort1.BytesToRead);
this.serialPort1.Write(SendBy, 0, 24);
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);再看看