[问题] short整数资料overflow解决方式

楼主: popo14777 (草草)   2022-07-23 18:11:40
我使用MX component里面的ActUtlTypeLib类别库,去读写三菱PLC的数据寄存器,
(参考网址:https://zhuanlan.zhihu.com/p/446264427)
C#程式码如下:
using ActUtlTypeLib;
public partial class Form1 : Form
{
ActUtlTypeClass textPLC = new ActUtlTypeClass();
private void button1_Click(object sender, EventArgs e)
{
WritePLCY = 1;
textPLC.WriteDeviceRandom("X0", 1, ref WritePLCY);
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
short Readwordbcd;
textPLC.ReadDeviceRandom2("D300", 1, out Readwordbcd);
Console.WriteLine(Readwordbcd);
}
}
PLC从1计数到100万,每计数一次就存入数据寄存器D300,
计数到32768时,发生了overflow(溢位)
C#执行时,就印出-32768,接着-32767、-32766、-32765....
但我PLC的D300显示值是正常的(无overflow)
我使用强制转型
Console.WriteLine((int)Readwordbcd);
Console.WriteLine((long)Readwordbcd);
无论怎么转型都还是会印出-32768、-32767、-32766
1.不晓得是不是ActUtlTypeLib类别库中,
ReadDeviceRandom2方法回传值型别只能为short
2.想请问还有什么计算的方式,可以让读出的数值变正常
例如-32768变成32768、-32767变成-32769..等
谢谢
作者: annies5 (念渝)   2022-07-23 22:52:00
为何一定要用short?
作者: antpro (-_*|| 宅)   2022-07-26 12:24:00
用两个 short 组成一个 int?

Links booklink

Contact Us: admin [ a t ] ucptt.com