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

📄 demo.c

📁 西门子provade通讯代码Sample_VC.rar,版本6.0
💻 C
📖 第 1 页 / 共 4 页
字号:

		if ((char)datatype == 't') 
			{
#ifdef S7_200
			buffer_byte[0] = 0;
			buffer_byte[1] = 0;
			buffer_byte[2] = 0;
			buffer_byte[3] = (unsigned char)((value >> 8) & 0xff);
			buffer_byte[4] = (unsigned char)(value & 0xff);
#endif
#ifdef S7_300
			buffer_byte[1] = (unsigned char)(value & 0xff);
			buffer_byte[0] = (unsigned char)((value >> 8) & 0xff);
#endif
			}

		if ((char)datatype == 'z') 
			{
#ifdef S7_200
			buffer_byte[0] = 0;
			buffer_byte[2] = (unsigned char)(value & 0xff);
			buffer_byte[1] = (unsigned char)((value >> 8) & 0xff);
#endif
#ifdef S7_300
			buffer_byte[1] = (unsigned char)(value & 0xff);
			buffer_byte[0] = (unsigned char)((value >> 8) & 0xff);
#endif
			}

			// set connection no
			if (IsDlgButtonChecked(hDlg, IDC_VERB1)) i=0;
			if (IsDlgButtonChecked(hDlg, IDC_VERB2)) i=1;
			if (IsDlgButtonChecked(hDlg, IDC_VERB3)) i=2;
			if (IsDlgButtonChecked(hDlg, IDC_VERB4)) i=3;
			res = new_ss((char)(i+1));

			if (res == 0)
			{
/**
			if (datatype == 'e')
#ifdef S7_200
	      	    res = as200_e_field_write(no,amount,buffer);
#endif
#ifdef S7_300
	      	    res = e_field_write(no,amount,buffer);
#endif

**/
			if (datatype == 'a')
#ifdef S7_200
				res = as200_a_field_write(no,amount,buffer);
#endif
#ifdef S7_300
				res = a_field_write(no,amount,buffer);
#endif
      		if (datatype == 'm')
#ifdef S7_200
	      	    res = as200_m_field_write(no,amount,buffer);
#endif
#ifdef S7_300
	      	    res = m_field_write(no,amount,buffer);
#endif

/***
			if (datatype == 't')
#ifdef S7_200
				res = as200_t_field_write(no,amount,buffer);
#endif
#ifdef S7_300
				res = t_field_write(no,amount,buffer);
#endif

***/
      		if (datatype == 'z')
#ifdef S7_200
	      	    res = as200_z_field_write(no,amount,buffer);
#endif
#ifdef S7_300
	      	    res = z_field_write(no,amount,buffer);
#endif
			if (datatype == 's')
#ifdef S7_200
	      	    res = as200_sm_field_write(no,amount,buffer);
#endif
#ifdef S7_300
				res = 0x8104;
#endif

      		if (datatype == 'v')
#ifdef S7_200
	      	    res = as200_vs_field_write(no,amount,buffer);
#endif
#ifdef S7_300
				res = 0x8104;
#endif
      		if (datatype == 'd')
#ifdef S7_200
				res = 0x8104;
#endif
#ifdef S7_300
	      	    res = d_field_write(blockno,no,amount,buffer);
#endif
			}

			if (res != 0) 
				{
				DialogBox(hInst,MAKEINTRESOURCE(IDD_ERROR),hDlg,ERRORBOX);
	 			EndDialog(hDlg, TRUE);
				return (TRUE);
				}
	     
			show = 1;
			break;
			}


      if (wParam == IDCANCEL)
         {
	 	 EndDialog(hDlg, TRUE);
	     return (TRUE);
         }

	default:
		return(FALSE);
   }



if (show == 1)
	{                   
	if (IsDlgButtonChecked(hDlg, IDC_HEX))    output='h';
	if (IsDlgButtonChecked(hDlg, IDC_DECIMAL)) output='d';
	if (IsDlgButtonChecked(hDlg, IDC_BINARY))  output='b';

	str[0] = 0;
	
	if (((char)datatype == 'e') || 
		((char)datatype == 'a') ||
		((char)datatype == 'm') ||
		((char)datatype == 's') ||
		((char)datatype == 'd') ||
		((char)datatype == 'v'))
		{
		if (output == 'h')
			if (buffer_byte[0] < 16) 
				wsprintf(str1,"0%x",buffer_byte[0]);
			else 
				wsprintf(str1,"%2x",buffer_byte[0]);
		
		if (output == 'd')
			wsprintf(str1,"%3d",buffer_byte[0]);

		if (output == 'b')
			{
			j = 0x01;
			while (j!=0x100)
				{
				if (j & buffer_byte[0]) 
					strcat(str1,"1");
				else 
					strcat(str1,"0");
				j=j<<1;
				}
			}

		}

	if (datatype == 't')
		{
#ifdef S7_200
		//as200 timer value -> 2 bytes long, but 5 bytes are received!!
		value = (unsigned long)buffer_byte[4] << 0  |
				(unsigned long)buffer_byte[3] << 8;
#endif
#ifdef S7_300
		// timer value -> 2 bytes long !!
		value = (unsigned long)buffer_int[0];
#endif

		if (output == 'h')
			{
			wsprintf(str, "0000");
			wsprintf(str1,"%x",value);
			str[4-(strlen(str1))] = 0;
			}

		if (output == 'd')
			wsprintf(str,"%5d",value);

		if (output == 'b')
			{
			j = 0x0001;
			str1[0] = 0;
			while (j!=0x00010000)
				{
				if (j & value) 
					strcat(str1,"1");
				else 
					strcat(str1,"0");
				j=j<<1;
				}
			}

		strcat(str,str1); 			
		} // end of datatype 't'

   
	if (datatype == 'z')
		{
#ifdef S7_200
		//as200 counter value -> 2 bytes long, but 3 bytes are received !!
		value = (unsigned long)buffer_byte[2] << 0 |
				(unsigned long)buffer_byte[1] << 8;
#endif
#ifdef S7_300
		//as300 counter value -> 2 bytes long !!
		value = (unsigned long)buffer_byte[1] << 0 |
				(unsigned long)buffer_byte[0] << 8;
#endif
		if (output == 'h')
			{
			wsprintf(str, "0000");
			wsprintf(str1,"%x",value);
			str[4-(strlen(str1))] = 0;
			}

		if (output == 'd')
			wsprintf(str1,"%5d",value);

		if (output == 'b')
			{
			str1[0] = 0;
			j = 0x00000001;
			while (j!=0x00010000)
				{
				if (j & value) 
					strcat(str1,"1");
				else 
					strcat(str1,"0");
				j=j<<1;
				}
			}

		strcat(str,str1); 			
		}

	SetDlgItemText(hDlg, IDC_EDIT_VALUE,str); 
	UpdateWindow(hDlg);                         
	}   

return (TRUE);

}  /* end of WRITEBOX */
/*************************************************************************/
BOOL WINAPI DBBUCHBOX(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
HWND listbox;

switch (message) {
    case WM_INITDIALOG:
		i = 0;
		CheckDlgButton(hDlg, IDC_VERB1, 1);
		return(TRUE);

    case WM_COMMAND:
		if (wParam == IDOK)
			{
			listbox = GetDlgItem(hDlg,IDC_LIST_DB);

			// set connection no
			if (IsDlgButtonChecked(hDlg, IDC_VERB1)) i=0;
			if (IsDlgButtonChecked(hDlg, IDC_VERB2)) i=1;
			if (IsDlgButtonChecked(hDlg, IDC_VERB3)) i=2;
			if (IsDlgButtonChecked(hDlg, IDC_VERB4)) i=3;
			res = new_ss((char)(i+1));

			if (res == 0)
				{
	
#ifdef S7_300
				res = db_buch(buffer);
#endif
#ifdef S7_200
				res = 0x8104;
#endif
				}

			if (res != 0) 
				{
				DialogBox(hInst,MAKEINTRESOURCE(IDD_ERROR),hDlg,ERRORBOX);
				EndDialog(hDlg, TRUE);
				return (TRUE);
				}
	     
         
			SendMessage(listbox,LB_RESETCONTENT,0,0);
			for (i=0;i<256;i++)
       			if (buffer_int[i] != 0) 
					{
			 		wsprintf(str,"DB %4d  ",i);
  					if ((buffer_int[i] & BST_IN_RAM) == BST_IN_RAM) strcat(str,"RAM"); 			
  					if ((buffer_int[i] & BST_IN_EPROM) == BST_IN_EPROM) strcat(str,"EPROM"); 			

		         	SendMessage(listbox,LB_ADDSTRING,0,(LPARAM)str);
				 	}
			 return (TRUE);
			}


		if (wParam == IDCANCEL)
			{
			EndDialog(hDlg, TRUE);
			return (TRUE);
			}

	} // end of case


return (FALSE);
}  /* end of DBBUCHBOX */
/*************************************************************************/
BOOL WINAPI MIXBOX(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
BOOL result;
HWND combobox[MAX_MIX];

#ifdef S7_200
char * DataType[] = {"e","a","m","t","z","v"};
HWND BoxHdl;
#endif
#ifdef S7_300
char * DataType[] = {"e","a","m","t","z","d"};
#endif

char output;
int i,j,k;
int show = 0;
unsigned long value;


switch (message) {
	case WM_INITDIALOG:
#ifdef S7_200
		BoxHdl = GetDlgItem(hDlg,IDC_BLKNO1);
		SendMessage(BoxHdl,EM_SETREADONLY,TRUE,0);
		BoxHdl = GetDlgItem(hDlg,IDC_BLKNO2);
		SendMessage(BoxHdl,EM_SETREADONLY,TRUE,0);
		BoxHdl = GetDlgItem(hDlg,IDC_BLKNO3);
		SendMessage(BoxHdl,EM_SETREADONLY,TRUE,0);
		BoxHdl = GetDlgItem(hDlg,IDC_BLKNO4);
		SendMessage(BoxHdl,EM_SETREADONLY,TRUE,0);
#endif
		i = 0;
		CheckDlgButton(hDlg, IDC_VERB1, 1);

		output = 'h';
		CheckDlgButton(hDlg, IDC_HEX, 1);

		// get handles of combobox
		combobox[0] = GetDlgItem(hDlg,IDC_COMBO1);
		combobox[1] = GetDlgItem(hDlg,IDC_COMBO2);
		combobox[2] = GetDlgItem(hDlg,IDC_COMBO3);
		combobox[3] = GetDlgItem(hDlg,IDC_COMBO4);

		//CB_INITSTORAGE		
		//wParam = (WPARAM) (int) cItems;  // number of items to add 
		//lParam = (LPARAM) (DWORD) cb;    // amount of memory to allocate, in bytes 
		SendMessage(combobox[0],CB_INITSTORAGE,6,36);
		SendMessage(combobox[1],CB_INITSTORAGE,6,36);
		SendMessage(combobox[2],CB_INITSTORAGE,6,36);
		SendMessage(combobox[3],CB_INITSTORAGE,6,36);

		// fill comboxes with datatype identifier
		for (i=0;i<MAX_MIX;i++)
			{
			SendMessage(combobox[i],CB_ADDSTRING,0,(LPARAM)DataType[0]);
			SendMessage(combobox[i],CB_ADDSTRING,0,(LPARAM)DataType[1]);
			SendMessage(combobox[i],CB_ADDSTRING,0,(LPARAM)DataType[2]);
			SendMessage(combobox[i],CB_ADDSTRING,0,(LPARAM)DataType[3]);
			SendMessage(combobox[i],CB_ADDSTRING,0,(LPARAM)DataType[4]);
			SendMessage(combobox[i],CB_ADDSTRING,0,(LPARAM)DataType[5]);
			}

		//select datatypes in combobox
		str[1]=0;
		for (k=0;k<MAX_MIX;k++)
			{
			str[0] = mix_tab[k].type;
			if ((i=SendMessage(combobox[k],CB_FINDSTRING,-1,(LPARAM)str))!=CB_ERR)
				SendMessage(combobox[k],CB_SETCURSEL,i,0);
			else
				SendMessage(combobox[k],CB_SETCURSEL,0,0);
			}

		show = 1;


#ifdef S7_300
		SetDlgItemInt(hDlg, IDC_BLKNO1,mix_tab[0].dbno,0); 
		SetDlgItemInt(hDlg, IDC_BLKNO2,mix_tab[1].dbno,0); 
		SetDlgItemInt(hDlg, IDC_BLKNO3,mix_tab[2].dbno,0); 
		SetDlgItemInt(hDlg, IDC_BLKNO4,mix_tab[3].dbno,0); 
#endif

		SetDlgItemInt(hDlg, IDC_NO1,mix_tab[0].no,0); 
		SetDlgItemInt(hDlg, IDC_NO2,mix_tab[1].no,0); 
		SetDlgItemInt(hDlg, IDC_NO3,mix_tab[2].no,0); 
		SetDlgItemInt(hDlg, IDC_NO4,mix_tab[3].no,0); 

		memset( buffer,0,sizeof(buffer));
		break;


    case WM_TIMER:
    case WM_COMMAND:
		if ((wParam == IDC_VERB1) ||
			(wParam == IDC_VERB2) ||
			(wParam == IDC_VERB3) ||
			(wParam == IDC_VERB4))
			{
			show = 1;
			break;
			}

		if ((wParam == IDC_HEX) ||
			(wParam == IDC_DECIMAL) ||
			(wParam == IDC_BINARY))
			{
			show = 1;
			break;
			}

		if ((wParam == ID_READ) || (wParam == ID_WRITE) ||
			((message == WM_TIMER) && (wParam == MYTIMER)))
			{

			if ((wParam == ID_READ) || (wParam == ID_WRITE)) KillTimer(hDlg,MYTIMER);


#ifdef S7_300
			mix_tab[0].dbno = GetDlgItemInt(hDlg, IDC_BLKNO1, &result,0);
			mix_tab[1].dbno = GetDlgItemInt(hDlg, IDC_BLKNO2, &result,0);
			mix_tab[2].dbno = GetDlgItemInt(hDlg, IDC_BLKNO3, &result,0);
			mix_tab[3].dbno = GetDlgItemInt(hDlg, IDC_BLKNO4, &result,0);
#endif			
			mix_tab[0].no = GetDlgItemInt(hDlg, IDC_NO1, &result,0);
			mix_tab[1].no = GetDlgItemInt(hDlg, IDC_NO2, &result,0);
			mix_tab[2].no = GetDlgItemInt(hDlg, IDC_NO3, &result,0);
			mix_tab[3].no = GetDlgItemInt(hDlg, IDC_NO4, &result,0);

#ifdef S7_300
			SetDlgItemInt(hDlg, IDC_BLKNO1,mix_tab[0].dbno,0); 
			SetDlgItemInt(hDlg, IDC_BLKNO2,mix_tab[1].dbno,0); 
			SetDlgItemInt(hDlg, IDC_BLKNO3,mix_tab[2].dbno,0); 
			SetDlgItemInt(hDlg, IDC_BLKNO4,mix_tab[3].dbno,0); 
#endif
			SetDlgItemInt(hDlg, IDC_NO1,mix_tab[0].no ,0); 
			SetDlgItemInt(hDlg, IDC_NO2,mix_tab[1].no ,0); 
			SetDlgItemInt(hDlg, IDC_NO3,mix_tab[2].no ,0); 
			SetDlgItemInt(hDlg, IDC_NO4,mix_tab[3].no ,0); 


			combobox[0] = GetDlgItem(hDlg,IDC_COMBO1);
			combobox[1] = GetDlgItem(hDlg,IDC_COMBO2);
			combobox[2] = GetDlgItem(hDlg,IDC_COMBO3);
			combobox[3] = GetDlgItem(hDlg,IDC_COMBO4);

			for (k=0;k<MAX_MIX;k++)
			if ((i = SendMessage(combobox[k],CB_GETCURSEL,0,0)) != CB_ERR)
				{
				mix_tab[k].type = DataType[i][0];
				if ((mix_tab[k].type == 'e') ||
					(mix_tab[k].type == 'a') ||
					(mix_tab[k].type == 'm') ||
					(mix_tab[k].type == 'd') ||
					(mix_tab[k].type == 'v') ||
					(mix_tab[k].type == 's'))
					mix_tab[k].size = 'B';
				else
					mix_tab[k].size = 'W';
				}
			else {
				mix_tab[k].type = 'e';
				mix_tab[k].size = 'B';
				SendMessage(combobox[k],CB_SETCURSEL,0,0);
				}
			

			// read and convert values if ID_WRITE
			if (wParam == ID_WRITE)
				{
				if (IsDlgButtonChecked(hDlg, IDC_HEX))    output='h';
				if (IsDlgButtonChecked(hDlg, IDC_DECIMAL)) output='d';
				if (IsDlgButtonChecked(hDlg, IDC_BINARY))  output='b';
				
				i = 0;
				for (k=0;k<MAX_MIX;k++)
					{
				
					if (k == 0) GetDlgItemText(hDlg, IDC_VALUE1, str,sizeof(str));
					if (k == 1) GetDlgItemText(hDlg, IDC_VALUE2, str,sizeof(str));
					if (k == 2) GetDlgItemText(hDlg, IDC_VALUE3, str,sizeof(str));
					if (k == 3) GetDlgItemText(hDlg, IDC_VALUE4, str,sizeof(str));
	
					if (output == 'h') value = hexstr2value(str);
					if (output == 'd') value = decstr2value(str);
					if (output == 'b') value = binstr2value(str);

					if (((char)datatype == 'e') || 
						((char)datatype == 'a') ||
						((char)datatype == 'm') ||
						((char)datatype == 's') ||
						((char)datatype == 'd') ||
						((char)datatype == 'v'))
						{	
						memcpy(&buffer[i],&value,1);
						i++;
						}

					if ((char)datatype == 't') 
						{
#ifdef S7_200
						buffer_byte[i+0] = 0;
						buffer_byte[i+1] = 0;
						buffer_byte[i+2] = 0;
						buffer_byte[i+3] = (unsigned char)((value >> 8) & 0xff);
						buffer_byte[i+4] = (unsigned char)(value & 0xff);
						i= i+5;
#endif

⌨️ 快捷键说明

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