⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nettask.c

📁 Vxworks 下重矿设备应用开发
💻 C
📖 第 1 页 / 共 3 页
字号:
/**********************************************************************
 项目:科委建设路桥重矿设备控制器
 文件:nettask.c
 描述:ReWorks下建立的单锤破端服务端的程序,接受来自上位机和板喂机的连接请求,包括网络监测模块。
 版本历史:12.07.2006  <石磊>  		
 **********************************************************************/
#include <stdio.h>
#include <string.h>
#include <rtos.h>
#include <ucore/error.h>
#include <ucore/rtos_bsdnet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "vxworks.h"
#include "nettask.h"
#include "taskLib.h"
#include "ioLib.h"
#include "logLib.h"
#include "string.h"
#include "stdio.h"
#include "netinet\\tcp.h"
#include "semLib.h"
#include "board.h"

//#include "panel.h"



#define MYIPADDR "90.0.0.90" 
#define MAX_CONNECTIONS 4		/*同时连接的最大客户端数*/
#define TCP_PORT 5000				/*端口号*/
#define TCP_CLIENT_PORT 5001

/*定义网络缓冲的长度*/
#define NET_MAX_LEN 4096    
#define NET_TX_LEN  4096 +12  //1     
#define NET_RX_LEN  4096 +12    
#define NET_APPHEADER_LEN 4

//定义连接状态
#define LINK_UP         1
#define LINK_DOWN       0

char msg[12];
float current;
float recv_buffer[10];


char ConnectState =0;

//创建一个bmp格式位图 用于测试
char video[4096]={
   0xfc, 0x7e, 0x40, 0x20, 0x90, 0x00, 0x07, 0x80, 0x23, 0x00, 0x00, 0xc6,
   0xc1, 0x41, 0x98, 0xb8, 0x01, 0x07, 0x66, 0x00, 0x15, 0x9f, 0x03, 0x47,
   0x8c, 0xc6, 0xdc, 0x7b, 0xcc, 0x00, 0xb0, 0x71, 0x0e, 0x4d, 0x06, 0x66,
   0x73, 0x8e, 0x8f, 0x01, 0x18, 0xc4, 0x39, 0x4b, 0x02, 0x23, 0x0c, 0x04,
   0x1e, 0x03, 0x0c, 0x08, 0xc7, 0xef, 0x08, 0x30, 0x06, 0x07, 0x1c, 0x02,
   0x06, 0x30, 0x18, 0xae, 0xc8, 0x98, 0x3f, 0x78, 0x20, 0x06, 0x02, 0x20,
   0x60, 0xa0, 0xc4, 0x1d, 0xc0, 0xff, 0x41, 0x04, 0xfa, 0x63, 0x80, 0xa1,
   0xa4, 0x3d, 0x00, 0x84, 0xbf, 0x04, 0x0f, 0x06, 0xfc, 0xa1, 0x34, 0x6b,
   0x01, 0x1c, 0xc9, 0x05, 0x06, 0xc7, 0x06, 0xbe, 0x11, 0x1e, 0x43, 0x30,
   0x91, 0x05, 0xc3, 0x61, 0x02, 0x30, 0x1b, 0x30, 0xcc, 0x20, 0x11, 0x00,
   0xc1, 0x3c, 0x03, 0x20, 0x0a, 0x00, 0xe8, 0x60, 0x21, 0x00, 0x61, 0x1b,
   0xc1, 0x63, 0x08, 0xf0, 0xc6, 0xc7, 0x21, 0x03, 0xf8, 0x08, 0xe1, 0xcf,
   0x0a, 0xfc, 0x4d, 0x99, 0x43, 0x07, 0x3c, 0x0c, 0xf1, 0x9f, 0x0b, 0xfc,
   0x5b, 0x81, 0x47, 0x02, 0x16, 0x04, 0x31, 0x1c, 0x0b, 0x1f, 0x17, 0x89,
   0x4d, 0x06, 0x1a, 0x04, 0x31, 0x38, 0x02, 0x07, 0x56, 0x89, 0x49, 0x04,
   0x0b, 0x04, 0xb1, 0x72, 0x82, 0xa1, 0x54, 0x9a, 0x49, 0x04, 0x1d, 0x66,
   0x50, 0xe7, 0xc2, 0xf0, 0x54, 0x9a, 0x58, 0x04, 0x0d, 0x62, 0xc1, 0x1f,
   0x44, 0xfc, 0x51, 0x90, 0x90, 0x04, 0x86, 0x63, 0xe0, 0x74, 0x04, 0xef,
   0x31, 0x1a, 0x91, 0x00, 0x02, 0xe2, 0xc1, 0xfd, 0x84, 0xf9, 0x30, 0x0a,
   0x91, 0x00, 0x82, 0xa9, 0xc0, 0xb9, 0x84, 0xf9, 0x31, 0x16, 0x81, 0x00,
   0x42, 0xa9, 0xdb, 0x7f, 0x0c, 0xff, 0x1c, 0x16, 0x11, 0x00, 0x02, 0x28,
   0x0b, 0x07, 0x08, 0x60, 0x1c, 0x02, 0x91, 0x00, 0x46, 0x29, 0x0e, 0x00,
   0x00, 0x00, 0x10, 0x16, 0x11, 0x02, 0x06, 0x29, 0x04, 0x00, 0x00, 0x00,
   0x10, 0x16, 0x91, 0x06, 0xa6, 0x2a, 0x04, 0x00, 0x00, 0x00, 0x18, 0x24,
   0x91, 0x04, 0x86, 0x2a, 0x04, 0x00, 0x00, 0x00, 0x18, 0x27, 0x93, 0x04,
   0x96, 0x4a, 0x04, 0x00, 0x00, 0x00, 0x04, 0x02, 0x91, 0x04, 0x86, 0x4a,
   0x0c, 0x00, 0x00, 0x00, 0x1e, 0x23, 0x93, 0x04, 0x56, 0x88, 0x08, 0x00,
   0x00, 0x00, 0x90, 0x21, 0x93, 0x04, 0x52, 0x0a, 0x09, 0x80, 0x01, 0x00,
   0xd0, 0x21, 0x95, 0x04, 0x57, 0x0a, 0x0f, 0x80, 0x27, 0x00, 0xd8, 0x20,
   0x9d, 0x04, 0x5d, 0x08, 0x1c, 0x80, 0x67, 0x00, 0xe4, 0x01, 0x85, 0x04,
   0x79, 0x8a, 0x3f, 0x00, 0x00, 0x00, 0xf4, 0x11, 0x85, 0x06, 0x39, 0x08,
   0x7d, 0x00, 0x00, 0x18, 0xb7, 0x10, 0x81, 0x03, 0x29, 0x12, 0xcb, 0x00,
   0x7e, 0x30, 0x28, 0x00, 0x85, 0x03, 0x29, 0x10, 0xbe, 0x81, 0xff, 0x27,
   0x0c, 0x10, 0x85, 0x03, 0x29, 0x32, 0xfa, 0xc1, 0xff, 0x27, 0x94, 0x11,
   0x85, 0x03, 0x28, 0x20, 0x6c, 0xe1, 0xff, 0x07, 0x0c, 0x01, 0x85, 0x01,
   0x28, 0x62, 0x5c, 0xe3, 0x8f, 0x03, 0x4e, 0x91, 0x80, 0x05, 0x39, 0x40,
   0xf4, 0xc2, 0xff, 0x00, 0x9f, 0x91, 0x84, 0x05, 0x31, 0xc6, 0xe8, 0x07,
   0x7f, 0x80, 0xcd, 0x00, 0xc4, 0x04, 0x31, 0x06, 0xc9, 0x0e, 0x00, 0xc0,
   0x48, 0x88, 0xe0, 0x04, 0x79, 0x04, 0xdb, 0x12, 0x00, 0x30, 0x0c, 0xc8,
   0xe4, 0x04, 0x6d, 0x06, 0xb6, 0x23, 0x00, 0x18, 0x1c, 0xc0, 0x84, 0x04,
   0x25, 0x0c, 0xff, 0xc2, 0x00, 0x4e, 0x06, 0xb0, 0x80, 0x04, 0x3f, 0x8a,
   0xb3, 0x83, 0xff, 0xc3, 0x03, 0x91, 0x84, 0x04, 0x2e, 0xd8, 0x0f, 0x3f,
   0x00, 0x00, 0x5f, 0x83, 0x84, 0x04, 0x2a, 0x70, 0xfd, 0x7f, 0x00, 0x00,
   0xc8, 0xc0, 0x84, 0x04, 0x4b, 0xe2, 0x2f, 0x01, 0x00, 0x08, 0x58, 0x60,
   0x80, 0x04, 0x5b, 0x82, 0xff, 0x01, 0x00, 0x08, 0xd0, 0xa0, 0x84, 0x04,
   0x72, 0x80, 0xe5, 0x00, 0x00, 0x08, 0xd2, 0x20, 0x44, 0x04, 0xca, 0x02,
   0xff, 0x00, 0x00, 0x08, 0xde, 0xa0, 0x44, 0x04, 0x82, 0x02, 0x6d, 0x00,
   0x00, 0x08, 0xf6, 0xb0, 0x40, 0x02, 0x82, 0x07, 0x3f, 0x00, 0x00, 0x08,
   0x44, 0x58, 0x44, 0x02, 0x93, 0x3f, 0x1f, 0x00, 0x00, 0x30, 0x88, 0x4f,
   0x44, 0x03, 0x83, 0x23, 0x3e, 0x00, 0x00, 0x00, 0x18, 0x60, 0xe0, 0x07,
   0xe3, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x70, 0x70, 0xe4, 0x07, 0xc7, 0x1b,
   0xfe, 0x01, 0x00, 0x00, 0xe0, 0x3c, 0xe4, 0x07, 0xc7, 0xe3, 0xfe, 0x1f,
   0x00, 0x00, 0xff, 0x1f, 0xfc, 0x07, 0xc7, 0x03, 0xf8, 0x33, 0x00, 0xc0,
   0xf0, 0x07, 0xff, 0x07, 0x87, 0x02, 0xfc, 0x43, 0x00, 0x60, 0xf0, 0xff,
   0xff, 0x07, 0x8f, 0x06, 0xbe, 0x87, 0x00, 0x30, 0xf8, 0xff, 0xff, 0x07,
   0x8f, 0x14, 0x9c, 0x8f, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x9f, 0x8d,
   0x8a, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x07, 0xbf, 0x0b, 0x80, 0x1f,
   0x00, 0x00, 0xff, 0xff, 0xff, 0x07, 0x7f, 0x3a, 0x80, 0x3f, 0x00, 0x80,
   0xff, 0xff, 0xff, 0x07, 0xff, 0x20, 0xc0, 0x3f, 0x00, 0x80, 0xff, 0xff,
   0xff, 0x07, 0xff, 0x01, 0xe0, 0x7f, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x07,
   0xff, 0x0f, 0xf8, 0xff, 0x40, 0xe0, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff,
   0xff, 0xff, 0x40, 0xf0, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff, 0xff,
   0x41, 0xf0, 0xff, 0xff, 0xff, 0x07};


//extern int CPI_FLAG;

extern int Flag_SPLC_NET;
extern int Flag_SPLC_NET_NO;

extern int Flag_Chgil;
extern float databuf[8];                  	//按照上面的顺序存放各个状态量
extern float x1buf[1024]; 					//存放振动加速度值
extern float x2buf[1024];
extern float y1buf[1024];
extern float y2buf[1024];
//extern int CPI[8];
extern int CPO2[8];
extern volatile SNetErr errcount; 
extern int netclose_id;
extern int Flag_BW ;
//extern void cb_DCpoweron(void);
//extern void cb_DCpoweroff(void);
//extern void cb_DCstart(void);
//extern void cb_DCstop(void);
//extern void cb_DCreset(void);
//extern void cb_DCemcstop(void);
//extern void cb_BWstart(void);
//extern void cb_BWstop(void);

//创建socket失败次数
static unsigned char sockcrtnum = 0;
static int net_writesocket(int newFd,int len);
static char netrecvsize(int handle1,unsigned char *pbuff,int len);

//tcp头定义  
typedef struct
{
   unsigned char   u8Cmd1;    // Command byte 1
   unsigned char   u8Cmd2;    // Command byte 2
   unsigned char   u8Len1;
   unsigned char   u8Len2;
   unsigned char   u8Len3;
   unsigned char   u8Len4;
}STCPHeader;

/*此处定义各种消息类型包括,确定其长度
 * SDC_COMMAND_REQ  12+4
 * SDC_INFOR_REQ
 * SDC_SPLC_REQ
 * SDC_SPLC_ACK
 * SDC_VIDEO_REQ
 * SDC_VIDEO_ACK
 * SHEALTH_REPORT_ACK
 * SDC_COMMAND_ACK        12+10
 * SDC_SLOWDATA_INFOR     12+3×4
 * SDC_FASTDATA_INFORX1   12+3×1024
 * SDC_FASTDATA_INFORX2    
 * SDC_FASTDATA_INFORY1
 * SDC_FASTDATA_INFORY2
 * */



typedef struct
{
  //控制命令变量   
   char CmdId; 
	  
 }SDC_COMMAND_REQ;

typedef struct
{
  //控制命令变量   
   char CmdId; 
	  
 }SBW_COMMAND_REQ;

typedef struct
{
   unsigned char DeviceId;    
   unsigned char   u8DescriptText[80];
   
}SHEALTH_REPORT_REQ;

typedef struct
{
  unsigned char s3;                      
}SDC_INFOR_REQ;

typedef struct
{
	char s4[4096];                  
}SDC_SPLC_REQ;


typedef struct
{
       char s5[4096];                   
}SDC_SPLC_ACK;

typedef struct
{
	char s6;                                             
}SDC_VIDEO_REQ;

typedef struct
{
	char s7[4096];      
                                              
}SDC_VIDEO_ACK;

typedef struct
{
    char state; 
                                                                             //控制命令变量
}SDC_COMMAND_ACK;

typedef struct
{
   
   unsigned char u8DescriptText[5];
}SHEALTH_REPORT_ACK;

typedef struct
{   
   float slowdata[4];//包括
   
}SDC_SLOWDATA_INFOR;

typedef struct
{
   float fastdatax1[1024];
                     
}SDC_FASTDATA_INFORX1;

typedef struct
{
   float fastdatax2[1024];
                     
}SDC_FASTDATA_INFORX2;

typedef struct
{
   float fastdatay1[1024];
                     
}SDC_FASTDATA_INFORY1;

typedef struct
{
   float fastdatay2[1024];
                     
}SDC_FASTDATA_INFORY2;

typedef struct
{
   float bwinf;
                     
}BW_INFOR_ACK;


 typedef union
{
   unsigned char u8DataBuffer[NET_MAX_LEN];
   struct
   {
      unsigned char u8PktNum;        // Package num
      unsigned char u8FormatNum;     // Format  num
      unsigned char u8FormatVer;     // Format  version 
      union
      {
           
        SDC_COMMAND_REQ           a;
	    SDC_INFOR_REQ             b;
	    SDC_SPLC_REQ              c;
	    SHEALTH_REPORT_REQ        d;
        SHEALTH_REPORT_ACK        e;
        SDC_COMMAND_ACK           f;
	    SDC_SPLC_ACK	          g;
	    SDC_SLOWDATA_INFOR        k;
	    SDC_FASTDATA_INFORX1      x1;
	    SDC_FASTDATA_INFORX2      x2;
	    SDC_FASTDATA_INFORY1      y1;
	    SDC_FASTDATA_INFORY2      y2;
        SDC_VIDEO_REQ             videor;
        SDC_VIDEO_ACK             videoa;		
	    BW_INFOR_ACK              h;
	   // BW_INFOR_REQ              bi;	
		SBW_COMMAND_REQ            bwcmd;
      }MsgData;                   // Message body
   }Msg;
}UNETMessage;
      
typedef struct
{
  // int  u8TxLen;                   // TX len
   union
   {
      unsigned char u8TxDataBuf[NET_TX_LEN];// TX data buf
      struct
      {
         STCPHeader  sTcpHeader; // TCP header
         UNETMessage uNetMsg;    //  message
      }sMsg;
   }uTx;
}STx;

typedef struct
{
   union
   {
      unsigned char u8RxDataBuf[NET_RX_LEN];// RX data buf
      struct
      {
         STCPHeader  sTcpHeader; // TCP header
         UNETMessage uNetMsg;    // message
      }sMsg;
   }uRx;
}SRx;  
   
static void net_applayer(int fd);    
   
volatile STx      net_gtx;                // Tx buffer
volatile SRx      net_grx;                // Rx buffer
volatile SNetErr     net_errcount;        // error record   
/*定义消息类型*/
#define     HEALTH_REPORT_REQ       1
#define     DC_COMMAND_REQ          2
#define	 DC_INFOR_REQ            3
#define	 DC_SPLC_REQ 	         4
#define     HEALTH_REPORT_ACK       5
#define     DC_COMMAND_ACK          6
#define	 DC_SPLC_ACK	         7
#define	 DC_SLOWDATA_INFOR       8
#define	 DC_FASTDATA_INFORX1     9
#define	 DC_FASTDATA_INFORX2     10
#define	 DC_FASTDATA_INFORY1     11
#define	 DC_FASTDATA_INFORY2     12
#define	 BW_INFOR_REQ             13
#define	 DC_VIDEO_REQ            14
#define	 DC_VIDEO_ACK            15
#define	 BW_COMMAND_REQ          16


//state machine for recieve
#define STATE_INITIAL_DC             0     //wait for receive
#define STATE_CMD1_DC                1
#define STATE_CMD2_DC                2
#define STATE_VER_DC                 3
#define STATE_READY_DC               4

#define ASCII_CMD1                   0xd
#define ASCII_CMD2                   0xb
#define ASCII_VER                    0x0

#define HEALTH_REPORT_PKTNUM           182
#define HEALTH_REPORT_REQ_FMTNUM       0x02
#define HEALTH_REPORT_ACK_FMTNUM       0x05
#define HEALTH_REPORT_FMTVER           0x01


#define DC_INFOR_PKTNUM                169
#define DC_FASTINFOR_PKTNUM            161
#define DC_FASTINFOR_REQ_FMTNUM        0x2
#define DC_FASTDATA_INFOR_FMTNUM       0x2
#define DC_FASTINFOR_FMTVERX1          0x01
#define DC_FASTINFOR_FMTVERX2          0x02
#define DC_FASTINFOR_FMTVERY1          0x03
#define DC_FASTINFOR_FMTVERY2          0x04


#define DC_SLOWINFOR_PKTNUM            168
#define DC_SLOWINFOR_REQ_FMTNUM        0x02
#define DC_SLOWDATA_INFOR_FMTNUM       0x02
#define DC_SLOWINFOR_FMTVER            0x02


#define DC_COMMAND_PKTNUM              172
#define DC_COMMAND_REQ_FMTNUM          0x01
#define DC_COMMAND_ACK_FMTNUM          0x01
#define DC_COMMAND_FMTVER              0x03

#define DC_SPLC_PKTNUM                 187
#define DC_SPLC_REQ_FMTNUM             0x02
#define DC_SPLC_ACK_FMTNUM             0x02
#define DC_SPLC_FMTVER                 0x04

#define BW_INFOR_PKTNUM                    156
#define BW_INFOR_REQ_FMTNUM            0x02
#define BW_DATA_INFOR_FMTNUM           0x02
#define BW_STATE_INFOR_FMTNUM          0x02
#define BW_INFOR_FMTVER                0x05


#define DC_VIDEO_PKTNUM                 176
#define DC_VIDEO_REQ_FMTNUM             0x01
#define DC_VIDEO_ACK_FMTNUM             0x02
#define DC_VIDEO_FMTVER                 0x04



#define BW_COMMAND_PKTNUM               164
#define BW_COMMAND_REQ_FMTNUM           0x01
#define BW_COMMAND_ACK_FMTNUM           0x01
#define BW_COMMAND_FMTVER               0x06






 
static unsigned char dc_msgrecv = FALSE;
  /*ASCII */
#define ASCII_DLE 0x10    // 
#define ASCII_STX 0x02    // 
#define ASCII_ETX 0x03    // 

// TCP头的长度,共9个字节
#define BYTENUM_LENGTH         9
// TCP尾的长度,共3个字节
#define DC_APPHEADER_LEN      3

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -