因規定格式只能使用short,但short又只能支援32767...
沒辦法像PLC可以自動存入至下一個暫存資料庫且合併...
因小弟涉世未深,懇請江湖各大好手協助解決小弟撞牆期QQ
原因如下:
Code如下:
private void buffAddBnt_Click(object sender, EventArgs e)
{
int iRet = 0;
int iStartIO = 0;
int iAddress = 1518;
int iwriteSize = 1;
short sData;
sData = Convert.ToInt16(buffValue.Text);
iRet = plc.WriteBuffer(iStartIO,iAddress,iwriteSize,ref sData);
}
private void buffValueBnt_Click(object sender, EventArgs e)
{
int iRet = 0;
short sDataa;
iRet = plc.ReadBuffer(0, 1518, 1, out sDataa);
label5.Text = Convert.ToString(sDataa);
}
不知道這個是不是你要的,可以將 int
轉成 short[]
。
public static short[] intToArray(int val)
{
var arr = new short[2];
arr[0] = (short)(val & 0xFFFF);
arr[1] = (short)((val >> 16) & 0xFFFF);
return arr;
}
public static int arrayToInt(short[] arr)
{
int val;
val = (int)((arr[0] & 0xFFFF)
| ((arr[1] & 0xFFFF) << 16));
return val;
}
測試程式
int a = -500000;
short[] arr = intToArray(a);
int b = arrayToInt(arr);
//b=-500000
看指令應該適用三菱的?
以下是Dword轉word方法
public void DWord_to_Word(int totalValue, ref short[] value, int startIndex)
{
value[startIndex+1] = BitConverter.ToInt16(BitConverter.GetBytes(totalValue), 0);//hi
value[startIndex] = BitConverter.ToInt16(BitConverter.GetBytes(totalValue), 2);//low
}