PLC读取数据高低位交换,批量保存到list集合,方便调用。
PLC读取数据高低位交换,批量保存到list集合,方便调用。
private void button1_Click(object sender, EventArgs e) { label1.Text = null; ushort[] nums = new ushort[] { 12, 34, 56, 78, 99, 11, 22, 33, 44, 26, 38 }; for (int i = 0; i < nums.Length - 1; i += 2) { fun1(ref nums[i], ref nums[i + 1]); }
if (nums.Length % 2 == 0) { for (int i = 0; i < nums.Length; i++) { label1.Text += nums[i].ToString() + "--"; } } else { for (int i = 0; i < nums.Length - 1; i++) { label1.Text += nums[i].ToString() + "--"; } } List<uint> res = new List<uint>(); for (int j = 0; j < nums.Length - 1; j += 2) { // MessageBox.Show(MakeLong(nums[j],nums[j+1]).ToString()); res.Add(MakeLong(nums[j], nums[j + 1])); } //foreach (var item in res) //{ // MessageBox.Show(item.ToString()); //} for (int i = 0; i < res.Count; i++) { MessageBox.Show(res[i].ToString()); } } public void fun1( ref ushort a, ref ushort b ) { ushort temp; temp = a; a = b; b = temp; } private uint MakeLong(ushort x, ushort y) { return (uint)x << 16 | y; }