110,533
社区成员
发帖
与我相关
我的任务
分享
[StructLayout(LayoutKind.Explicit)]
public struct Union
{
[FieldOffset(0)]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.Struct)]
public bool[] bit;
[FieldOffset(0)]
public int BitComb;
}
public struct U
{
public int BitComb;
public bool[] bit()
{
bool[] bit = new bool[32];
for (int i = 1;i <= 32;i++)
{
int idx_bool = i - 1;
int cardinal = 1;
bit[idx_bool] = (BitComb & (cardinal<<idx_bool)) == 0 ? false:true;
}
return bit;
}
}
public void DataRead(U type1,U type2)
{
int cur_Node = Globals.Tags.addr_node.Value;
switch(cur_Node)
{
case 1:
//pressure set value read
if(type1.bit[0])
{
if (type2.bit[0])
{
Globals.Tags.sv_prepressure801.Read();
DvInfo.wrSV_float[0] = Globals.Tags.sv_prepressure801.Value;
}
if (type2.bit[1])
{
Globals.Tags.sv_brushpressure801.Read();
DvInfo.wrSV_float[1] = Globals.Tags.sv_brushpressure801.Value;
}
if (type2.bit[2])
{
Globals.Tags.sv_postpressure801.Read();
DvInfo.wrSV_float[2] = Globals.Tags.sv_postpressure801.Value;
}
}
//counter & time set value read
if(type1.bit[1])
{
if(type2.bit[0])
{
Globals.Tags.sv_brushcnt801.Read();
DvInfo.wrSV_int[0] = Globals.Tags.sv_brushcnt801.Value;
}
if(type2.bit[1])
{
Globals.Tags.sv_pretime801.Read();
DvInfo.wrSV_int[1] = Globals.Tags.sv_pretime801.Value;
}
if(type2.bit[2])
{
Globals.Tags.sv_brushtime801.Read();
DvInfo.wrSV_int[2] = Globals.Tags.sv_brushtime801.Value;
}
if(type2.bit[3])
{
Globals.Tags.sv_posttime801.Read();
DvInfo.wrSV_int[3] = Globals.Tags.sv_posttime801.Value;
}
}
//password set value read
if(type1.bit[2])
{
if(type2.bit[0])
{
Globals.Tags.pw801_Admin.Read();
DvInfo.wrPW[0] = Globals.Tags.pw801_Admin.Value;
}
if(type2.bit[1])
{
Globals.Tags.pw801_Eng.Read();
DvInfo.wrPW[1] = Globals.Tags.pw801_Eng.Value;
}
if(type2.bit[2])
{
Globals.Tags.pw801_Oper.Read();
DvInfo.wrPW[2] = Globals.Tags.pw801_Oper.Value;
}
}
// button trig read
if(type1.bit[3])
{
if(type2.bit[0])
{
Globals.Tags.btn_prepurge801.Read();
DvInfo.wBTN[0] = Globals.Tags.btn_prepurge801.Value;
}
if(type2.bit[1])
{
Globals.Tags.btn_brush801.Read();
DvInfo.wBTN[1] = Globals.Tags.btn_brush801.Value;
}
if(type2.bit[2])
{
Globals.Tags.btn_postpurge801.Read();
DvInfo.wBTN[2] = Globals.Tags.btn_postpurge801.Value;
}
}
//current parameter read
if(type1.bit[4])
{
if(type2.bit[0])
{
Globals.Tags.cv_pressure801.Read();
DvInfo.rCV_float[0] = Globals.Tags.cv_pressure801.Value;
}
if(type2.bit[1])
{
Globals.Tags.cv_flowm801.Read();
DvInfo.rCV_float[1] = Globals.Tags.cv_flowm801.Value;
}
if(type2.bit[2])
{
Globals.Tags.cv_flowb801.Read();
DvInfo.rCV_float[2] = Globals.Tags.cv_flowb801.Value;
}
if(type2.bit[3])
{
Globals.Tags.cv_sound801.Read();
DvInfo.rCV_float[3] = Globals.Tags.cv_sound801.Value;
}
}
//cycle count and time current value read
if(type1.bit[5])
{
if(type2.bit[0])
{
Globals.Tags.cv_brushcnt801.Read();
DvInfo.rCV_int[0] = Globals.Tags.cv_brushcnt801.Value;
}
if(type2.bit[1])
{
Globals.Tags.cv_pretime801.Read();
DvInfo.rCV_int[1] = Globals.Tags.cv_pretime801.Value;
}
if(type2.bit[2])
{
Globals.Tags.cv_brushtime801.Read();
DvInfo.rCV_int[2] = Globals.Tags.cv_brushtime801.Value;
}
if(type2.bit[3])
{
Globals.Tags.cv_posttime801.Read();
DvInfo.rCV_int[3] = Globals.Tags.cv_posttime801.Value;
}
}
//button lamp read
if(type1.bit[6])
{
if(type2.bit[0])
{
Globals.Tags.lp_prepurge801.Read();
DvInfo.rLP[0] = Globals.Tags.lp_prepurge801.Value;
}
if(type2.bit[1])
{
Globals.Tags.lp_brush801.Read();
DvInfo.rLP[1] = Globals.Tags.lp_brush801.Value;
}
if(type2.bit[2])
{
Globals.Tags.lp_postpurge801.Read();
DvInfo.rLP[2] = Globals.Tags.lp_postpurge801.Value;
}
}
//signal read
if(type1.bit[7])
{
if(type2.bit[0])
{
Globals.Tags.sig_purge801.Read();
DvInfo.rSIG[0] = Globals.Tags.sig_purge801.Value;
}
if(type2.bit[1])
{
Globals.Tags.sig_brush801.Read();
DvInfo.rSIG[1] = Globals.Tags.sig_brush801.Value;
}
}
//
break;
default:
if(type1.bit[0])
{
if(type2.bit[0])
{
DvInfo.wrSV_float[0] = Convert.ToSingle(System.Math.PI);
}
if(type2.bit[1])
{
DvInfo.wrSV_float[1] = 2*DvInfo.wrSV_float[1];
}
if(type2.bit[2])
{
DvInfo.wrSV_float[2] = Convert.ToSingle(1.5*DvInfo.wrSV_float[1]);
}
if(type2.bit[3])
{
DvInfo.wrSV_float[3] = Convert.ToSingle(0.5*DvInfo.wrSV_float[1]);
}
}
break;
}
}
[StructLayout(LayoutKind.Explicit)]
public struct Union
{
[FieldOffset(0)]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.Struct)]
public bool[] bit;
[FieldOffset(0)]
public int BitComb;
}
能达到对BitComb赋值后,bool数组的值就已经出来的效果吗?[/quote]
不能 c#里bool是4字节大小,无法对应到位[/quote]
哦,这样啊。
后来我在其他界面引用了之前定义的方法,结果运行直接崩溃了,能看看怎么回事吗?连Log都没有,无从查起啊。
[StructLayout(LayoutKind.Explicit)]
public struct Union
{
[FieldOffset(0)]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.Struct)]
public bool[] bit;
[FieldOffset(0)]
public int BitComb;
}
能达到对BitComb赋值后,bool数组的值就已经出来的效果吗?[/quote]
不能 c#里bool是4字节大小,无法对应到位
[StructLayout(LayoutKind.Explicit)]
public struct Union
{
[FieldOffset(0)]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.Struct)]
public bool[] bit;
[FieldOffset(0)]
public int BitComb;
}
能达到对BitComb赋值后,bool数组的值就已经出来的效果吗?
public partial class tempdsScr
{
private Timer t;
private float[] ValStr = {0,0,0,0};
public void StartTimer()
{
t = new Timer();
t.Enabled = true;
t.Interval = Globals.Tags.refreshCT.Value;
t.Tick += myTick;
}
public void myTick(object obj,EventArgs ea)
{
if(!Globals.Tags.flag_LoginOpen.Value)
{
ValStr = Globals.UserModule.DevSts();
disp_Pressure.Value = ValStr[0];
disp_Flow1.Value = ValStr[1];
disp_Flow2.Value = ValStr[2];
disp_Sound.Value = ValStr[3];
string exeInd = Globals.UserModule.ExecuteInd();
executionIndicate.Text = exeInd;
U t1 = new U();
U t2 = new U();
t1.BitComb = 1;
t2.BitComb = 15;
//executionIndicate.FontColor = Color.Beige;
Globals.UserModule.DataRead(t1,t2);
//AnalogNumeric.Value = Globals.UserModule.DvInfo.wrSV_float[0];
//AnalogNumeric.Value = Globals.UserModule.DvInfo.wrSV_float[1];
//AnalogNumeric.Value = Globals.UserModule.DvInfo.wrSV_float[2];
//AnalogNumeric.Value = Globals.UserModule.DvInfo.wrSV_float[3];
}
}
void tempdsScr_Opened(System.Object sender, System.EventArgs e)
{
// define prefix & suffix
disp_Pressure.Prefix = "当前气压:";
disp_Pressure.Suffix = " psi";
disp_Flow1.Prefix = "主管道流量:";
disp_Flow1.Suffix = " l/min";
disp_Flow2.Prefix = "支管道流量:";
disp_Flow2.Suffix = " l/min";
disp_Sound.Prefix = "汽笛声强:";
disp_Sound.Suffix = " dB";
//type1.BitComb = 1;
//type2.BitComb = 15;
StartTimer();
}
void tempdsScr_Closed(System.Object sender, System.EventArgs e)
{
t.Enabled = false;
t.Tick -= myTick;
}
}
结果运行起来直接崩溃了。
什么原因?但是把 Globals.UserModule.DataRead(t1,t2); 注释掉就不会有问题。
这里的DataRead就是上文的DataRead