什么是上位机
上位机是一台可以发出特定操控命令的计算机,通过操作预先设定好的命令,将命令传递给下位机,通过下位机来控制设备完成各项操作(应用最多的地方如:工业、水利、供电部门、交通等方面)。上位机都有特定的编程,有专门的开发系统,或以上是已经规定好任务,只能按特定的任务进行操作。简单说上位机就是命令的下达者,而下位机则是命令的执行者。
上位机软件介绍
下面介绍一种软件:物联网应用中收集感知节点信息,进行显示,控制的应用控制台。此软件主要有两部分组成,数据收发部分和显示部分
1、上位机在系统中的位置:处于网络层中,与嵌入式网关通过网线相连。实际应用时是放置在实验室的老师工作台上,供实验室的老师使用。
2、上位机的功能:此软件实时监视实验实中每个实验台的状况,包括上电或断电状态,实验台是否发出了警告,是否发出了求助信号,并对这些信号做出应答,还控制LED屏的文字显示。
3、上位机的设计实现: 此软件主要有两部分组成,数据收发部分和显示部分。数据收发指的是和嵌入式网关的数据收发。两者是通过网线连接的,所以应用的是TCP/IP 的Socket 套接字编程,嵌入式网关的设计中已经提到过,它在和上位机通信中是作为服务器的,那么上位机就是作为连接发起方。为了能保证网络连接的稳定性,我们把Socket读写的程序代码放在了try{} catch(){} 块中,一旦网络连接不正常,就会捕获到该异常,从而关闭程序。
4、关键代码如下:
private void ReadFromArm()
??????? {
??????????? byte[] buffertocheck = new byte[1];
??????????? int bytesize = 0;
??????????? do
??????????? {
??????????????? byte[] bufferofread = new byte[1024];
??????????????? try { bytesize = stream.Read(bufferofread, 0, bufferofread.Length); }
??????????????? catch (Exception ex) { connection = Indicator.Unconnected; MessageBox.Show("连接中断,程序将退出。");? Application.Exit(); read.Abort(); }
??????????????? //MessageBox.Show(BitConverter.ToString(bufferofread));
??????????????? byte[] buffertochecktemp = buffertocheck;
??????????????? buffertocheck = new byte[buffertochecktemp.Length + bytesize];
??????????????? Array.Copy(buffertochecktemp, 0, buffertocheck, 0, buffertochecktemp.Length);
??????????????? Array.Copy(bufferofread, 0, buffertocheck, buffertochecktemp.Length, bytesize);
??????????????? int index_1, index_2;
??????????????? while (Check7E(buffertocheck, out index_1, out index_2))
??????????????? {
??????????????????? byte[] buffertodecode = new byte[index_2 + 1 - index_1];
??????????????????? Array.Copy(buffertocheck, index_1, buffertodecode, 0, index_2 + 1 - index_1);
??????????????????? byte[] bufferofdecode = PPP.Decode(buffertodecode);
??????????????????? //MessageBox.Show(BitConverter.ToString(bufferofdecode));
??????????????????? if (bufferofdecode[0] == bufferofdecode.Length && CheckSum(bufferofdecode))//保证从ARM来的帧是正确的
??????????????????? {
??????????????????????? byte[] Zuohao = new byte[2];
??????????????????????? int subindex = 1, index = bufferofdecode[1]*256+bufferofdecode[2]-1;
??????????????????????? string status=null;
??????????????????????? switch (bufferofdecode[3])//分别处理从ARM来的帧
??????????????????????? {
??????????????????????????? case POWER_ON://从ARM来的上电状态
??????????????????????????????? status = "上电";
??????????????????????????????? subindex = 1;
??????????????????????????????? break;
??????????????????????????? case POWER_OFF://从ARM来的断电状态
??????????????????????????????? status = "断电";
??????????????????????????????? subindex = 1;
??????????????????????????????? break;
??????????????????????????? case ABNORMAL://从ARM来的异常状态
??????????????????????????????? status = "异常";
??????????????????????????????? subindex = 1;
??????????????????????????????? break;
??????????????????????????? case WARNING://从ARM来的警告信号
??????????????????????????????? status = "有";
??????????????????????????????? subindex = 2;
??????????????????????????????? break;
??????????????????????????? case NEED_HELP://从ARM来的求救信号
??????????????????????????????? status = "有";
??????????????????????????????? subindex = 3;
??????????????????????????????? break;
??????????????????????? }
??????????????????????? if (0 <= index && index <= 49)
??????????????????????? {
??????????????????????????? listView1.Items[index].SubItems[subindex].Text = status;
??????????????????????????? if (status == "异常" || status == "有")
??????????????????????????????? listView1.Items[index].SubItems[subindex].ForeColor = Color.Red;
??????????????????????????? else
??????????????????????????????? listView1.Items[index].SubItems[subindex].ForeColor = Color.Black;
??????????????????????? }
??????????????????? }
??????????????????? int newlength = buffertocheck.Length - 1 - index_2;
??????????????????? if (newlength == 0)
??????????????????? {
??????????????????????? buffertocheck = new byte[1];
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? buffertochecktemp = buffertocheck;
??????????????????????? buffertocheck = new byte[newlength];
??????????????????????? Array.Copy(buffertochecktemp, index_2 + 1, buffertocheck, 0, newlength);
??????????????????? }
??????????????? }
??????????? } while (connection == Indicator.Connected);
??????? }
??????? ///
??????? /// 向ARM发送
??????? ///
??????? /// 座号 2字节
??????? /// 命令字 1字节
??????? public void WriteToArm(byte[] Zuohao, byte mingling)
??????? {
??????????? if (connection == Indicator.Connected)//在与ARM保持连接的情况下可写
??????????? {
??????????????? byte[] frame = new byte[5];
??????????????? frame[0] = 0x05;
??????????????? Array.Copy(Zuohao, 0, frame, 1, 2);
??????????????? frame[3] = mingling;
??????????????? byte temp = 0;
??????????????? foreach (byte item in frame)
??????????????????? temp += item;
??????????????? frame[4] = (byte)(0 - temp);
??????????????? byte[] frametosend = PPP.Encode(frame);
??????????????? Console.WriteLine(BitConverter.ToString(frametosend));
??????????????? try { stream.Write(frametosend, 0, frametosend.Length); }
??????????????? catch (Exception ex) { }
??????????? }
??????? }
??????? ///
??????? /// 向ARM发送
??????? ///
??????? /// 座号 2字节
??????? /// 命令字加ASC码 n字节
??????? public void WriteToArm(byte[] Zuohao, byte[] minglingandASC)
??????? {
??????????? if (connection == Indicator.Connected)//在与ARM保持连接的情况下可写
??????????? {
??????????????? byte[] frame = new byte[4+minglingandASC.Length];
??????????????? frame[0] = (byte)frame.Length;
??????????????? Array.Copy(Zuohao, 0, frame, 1, 2);
??????????????? Array.Copy(minglingandASC,0,frame,3,minglingandASC.Length);
??????????????? byte temp = 0;
??????????????? foreach (byte item in frame)
??????????????????? temp += item;
??????????????? frame[frame.Length-1] = (byte)(0 - temp);
??????????????? byte[] frametosend = PPP.Encode(frame);
??????????????? Console.WriteLine(BitConverter.ToString(frametosend));
??????????????? try { stream.Write(frametosend, 0, frametosend.Length); }
??????????????? catch (Exception ex) { }
??????????? }
??????? }
另外数据收发还需要协议,和嵌入式网关通信的指令和协议定制如下:
因此数据量较少,协议也就比较简单,此协议在发送接收时用PPP封装。显示部分使用C#编写的运行于Windows .Net Framework 上的窗体应用程序,根据实际需求,对每个实验台状态的显示使用ListView控件实现,当出现异常情况,或实验台出现警告、求助信号时,ListView的相应字段文字会通过改变颜色来提醒监视人员,监视人员还可以通过一个文字发送窗体来向LED屏发送要显示的文字。总之界面清晰直观,简便易用。
?
评论