📄 demo.c
字号:
if (wParam == IDCANCEL)
{
EndDialog(hDlg, TRUE);
show = 1;
break;
}
default:
return(FALSE);
} // end of case
if (show == 1)
{
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;
result = new_ss((char)(i+1));
if (result == 0)
SetDlgItemText(hDlg, IDC_STATUS, "ok");
else
{
error_message(result,error_txt);
SetDlgItemText(hDlg, IDC_STATUS, error_txt);
}
itoa(adr_table[i].adr,str,10);
SetDlgItemText(hDlg, IDC_STATIONADR, str);
itoa(adr_table[i].rackno,str,10);
SetDlgItemText(hDlg, IDC_RACKNO, str);
itoa(adr_table[i].segmentid,str,10);
SetDlgItemText(hDlg, IDC_SEGMENTID, str);
itoa(adr_table[i].slotno,str,10);
SetDlgItemText(hDlg, IDC_SLOTNO, str);
UpdateWindow(hDlg);
}
return (TRUE);
} /* end of STATUSBOX */
/*************************************************************************/
/*************************************************************************/
BOOL WINAPI READBOX(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
BOOL result;
HWND listbox;
char output;
int i,j;
int show = 0;
unsigned long value;
switch (message) {
case WM_INITDIALOG:
#ifdef S7_200
listbox = GetDlgItem(hDlg,IDC_BLOCKNO);
SendMessage(listbox,EM_SETREADONLY,TRUE,0);
#endif
i = 0;
CheckDlgButton(hDlg, IDC_VERB1, 1);
output = 'h';
CheckDlgButton(hDlg, IDC_HEX, 1);
show = 1;
if (datatype == 'e') CheckDlgButton(hDlg, IDC_ETYP, 1);
if (datatype == 'a') CheckDlgButton(hDlg, IDC_ATYP, 1);
if (datatype == 'm') CheckDlgButton(hDlg, IDC_MTYP, 1);
if (datatype == 't') CheckDlgButton(hDlg, IDC_TTYP, 1);
if (datatype == 'z') CheckDlgButton(hDlg, IDC_ZTYP, 1);
if (datatype == 's') CheckDlgButton(hDlg, IDC_SMTYP,1);
if (datatype == 'v') CheckDlgButton(hDlg, IDC_VSTYP,1);
if (datatype == 'd') CheckDlgButton(hDlg, IDC_DTYP,1);
#ifdef S7_300
SetDlgItemInt(hDlg, IDC_BLOCKNO,blockno,0);
#endif
SetDlgItemInt(hDlg, IDC_FROMNO,no,0);
SetDlgItemInt(hDlg, IDC_AMOUNT,amount,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 == IDOK) ||
((message == WM_TIMER) && (wParam == MYTIMER)))
{
if (wParam == IDOK) KillTimer(hDlg,MYTIMER);
if (IsDlgButtonChecked(hDlg, IDC_ETYP)) datatype = 'e';
if (IsDlgButtonChecked(hDlg, IDC_ATYP)) datatype = 'a';
if (IsDlgButtonChecked(hDlg, IDC_MTYP)) datatype = 'm';
if (IsDlgButtonChecked(hDlg, IDC_TTYP)) datatype = 't';
if (IsDlgButtonChecked(hDlg, IDC_ZTYP)) datatype = 'z';
if (IsDlgButtonChecked(hDlg, IDC_SMTYP)) datatype = 's';
if (IsDlgButtonChecked(hDlg, IDC_VSTYP)) datatype = 'v';
if (IsDlgButtonChecked(hDlg, IDC_DTYP)) datatype = 'd';
#ifdef S7_300
blockno = GetDlgItemInt(hDlg, IDC_BLOCKNO, &result,0);
#endif
no = GetDlgItemInt(hDlg, IDC_FROMNO, &result,0);
amount = GetDlgItemInt(hDlg, IDC_AMOUNT, &result,0);
//program savety
if (((char)datatype == 'e') ||
((char)datatype == 'a') ||
((char)datatype == 'm') ||
((char)datatype == 's') ||
((char)datatype == 'd') ||
((char)datatype == 'v'))
if (amount > MAX_BUFFER) amount = MAX_BUFFER;
#ifdef S7_300
if (((char)datatype == 't') ||
((char)datatype == 'z'))
if ((amount*2) > MAX_BUFFER) amount = MAX_BUFFER/2;
#endif
#ifdef S7_200
if ((char)datatype == 't')
if ((amount*5) > MAX_BUFFER) amount = MAX_BUFFER/5;
if ((char)datatype == 'z')
if ((amount*3) > MAX_BUFFER) amount = MAX_BUFFER/3;
#endif
#ifdef S7_300
SetDlgItemInt(hDlg, IDC_BLOCKNO,blockno,0);
#endif
SetDlgItemInt(hDlg, IDC_FROMNO,no,0);
SetDlgItemInt(hDlg, IDC_AMOUNT, amount,0);
// 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_read(no,amount,buffer);
#endif
#ifdef S7_300
res = e_field_read(no,amount,buffer);
#endif
if (datatype == 'a')
#ifdef S7_200
res = as200_a_field_read(no,amount,buffer);
#endif
#ifdef S7_300
res = a_field_read(no,amount,buffer);
#endif
if (datatype == 'm')
#ifdef S7_200
res = as200_m_field_read(no,amount,buffer);
#endif
#ifdef S7_300
res = m_field_read(no,amount,buffer);
#endif
if (datatype == 't')
#ifdef S7_200
res = as200_t_field_read(no,amount,buffer);
#endif
#ifdef S7_300
res = t_field_read(no,amount,buffer);
swab(buffer,buffer,amount*2);
#endif
if (datatype == 'z')
#ifdef S7_200
res = as200_z_field_read(no,amount,buffer);
#endif
#ifdef S7_300
res = z_field_read(no,amount,buffer);
swab(buffer,buffer,amount*2);
#endif
if (datatype == 's')
#ifdef S7_200
res = as200_sm_field_read(no,amount,buffer);
#endif
#ifdef S7_300
res = 0x8104;
#endif
if (datatype == 'v')
#ifdef S7_200
res = as200_vs_field_read(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_read(blockno,no,amount,buffer);
#endif
}
if (res != 0)
{
KillTimer(hDlg,MYTIMER);
DialogBox(hInst,MAKEINTRESOURCE(IDD_ERROR),hDlg,ERRORBOX);
EndDialog(hDlg, TRUE);
return (TRUE);
}
if ((message == WM_TIMER) && (wParam == MYTIMER))
SetTimer(hDlg,MYTIMER,800,NULL);
show = 1;
break;
}
if (wParam == ID_CYCLE)
{
SetTimer(hDlg,MYTIMER,800,NULL);
return (TRUE);
}
if (wParam == IDCANCEL)
{
KillTimer(hDlg,MYTIMER);
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';
listbox = GetDlgItem(hDlg,IDC_LIST_VALUE);
SendMessage(listbox,LB_RESETCONTENT,0,0);
if (((char)datatype == 'e') ||
((char)datatype == 'a') ||
((char)datatype == 'm') ||
((char)datatype == 's') ||
((char)datatype == 'd') ||
((char)datatype == 'v'))
{
for (i=0;i<amount;i++)
{
str1[0] = 0;
wsprintf(str, "%4d. ",i);
if (output == 'h')
if (buffer_byte[i] < 16)
wsprintf(str1,"0%x",buffer_byte[i]);
else
wsprintf(str1,"%2x",buffer_byte[i]);
if (output == 'd')
wsprintf(str1,"%3d",buffer_byte[i]);
if (output == 'b')
{
j = 0x01;
while (j!=0x100)
{
if (j & buffer_byte[i])
strcat(str1,"1");
else
strcat(str1,"0");
j=j<<1;
}
}
strcat(str,str1);
SendMessage(listbox,LB_ADDSTRING,0,(LPARAM)str);
}
}
if (datatype == 't')
{
for (i=0;i<amount;i++)
{
#ifdef S7_200
//as200 timer value -> 2 bytes long, but 5 bytes are received!!
value = (unsigned long)buffer_byte[i*5+4] << 0 |
(unsigned long)buffer_byte[i*5+3] << 8;
#endif
#ifdef S7_300
// timer value -> 2 bytes long !!
value = (unsigned long)buffer_int[i];
#endif
wsprintf(str, "%4d. ",i);
if (output == 'h')
{
wsprintf(str, "%4d. 0000",i);
wsprintf(str1,"%x",value);
str[11-(strlen(str1))] = 0;
}
if (output == 'd')
wsprintf(str1,"%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);
SendMessage(listbox,LB_ADDSTRING,0,(LPARAM)str);
} // end of for
} // end of datatype 't'
if (datatype == 'z')
{
for (i=0;i<amount;i++)
{
#ifdef S7_200
//as200 counter value -> 2 bytes long, but 3 bytes are received !!
value = (unsigned long)buffer_byte[i*3+2] << 0 |
(unsigned long)buffer_byte[i*3+1] << 8;
#endif
#ifdef S7_300
//as300 counter value -> 2 bytes long !!
value = (unsigned long)buffer_int[i];
#endif
wsprintf(str, "%4d. ",i);
if (output == 'h')
{
wsprintf(str, "%4d. 0000",i);
wsprintf(str1,"%x",value);
str[11-(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);
SendMessage(listbox,LB_ADDSTRING,0,(LPARAM)str);
} // end of for
}
UpdateWindow(hDlg);
}
return (TRUE);
} /* end of READBOX */
/*************************************************************************/
unsigned long hexstr2value(char * s)
{
return(strtol(s,NULL,16));
}
unsigned long decstr2value(char * s)
{
return(atol(s));
}
unsigned long binstr2value(char * s)
{
unsigned long v = 0;
unsigned long w = 1;
int i,j;
j = strlen(s);
for (i=0;i<j;i++)
{
if (s[i] == '1') v = v | w;
w = w << 1;
}
return(v);
}
/*************************************************************************/
BOOL WINAPI WRITEBOX(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
#ifdef S7_200
HWND BoxHdl;
#endif
BOOL result;
char output;
int i,j;
int show = 0;
unsigned long value;
switch (message) {
case WM_INITDIALOG:
#ifdef S7_200
BoxHdl = GetDlgItem(hDlg,IDC_BLOCKNO);
SendMessage(BoxHdl,EM_SETREADONLY,TRUE,0);
#endif
i = 0;
CheckDlgButton(hDlg, IDC_VERB1, 1);
output = 'h';
CheckDlgButton(hDlg, IDC_HEX, 1);
show = 1;
datatype = 'a';
if (datatype == 'e') CheckDlgButton(hDlg, IDC_ETYP, 1);
if (datatype == 'a') CheckDlgButton(hDlg, IDC_ATYP, 1);
if (datatype == 'm') CheckDlgButton(hDlg, IDC_MTYP, 1);
if (datatype == 't') CheckDlgButton(hDlg, IDC_TTYP, 1);
if (datatype == 'z') CheckDlgButton(hDlg, IDC_ZTYP, 1);
if (datatype == 's') CheckDlgButton(hDlg, IDC_SMTYP,1);
if (datatype == 'v') CheckDlgButton(hDlg, IDC_VSTYP,1);
if (datatype == 'd') CheckDlgButton(hDlg, IDC_DTYP,1);
#ifdef S7_300
SetDlgItemInt(hDlg, IDC_BLOCKNO,blockno,0);
#endif
SetDlgItemInt(hDlg, IDC_FROMNO,no,0);
// SetDlgItemInt(hDlg, IDC_AMOUNT,amount,0);
memset( buffer,0,sizeof(buffer));
memset( buffer,0,sizeof(buffer));
break;
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_EDIT_VALUE) ||
(wParam == IDC_BINARY))
{
show = 1;
break;
}
if (wParam == IDOK)
{
if (IsDlgButtonChecked(hDlg, IDC_HEX)) output='h';
if (IsDlgButtonChecked(hDlg, IDC_DECIMAL)) output='d';
if (IsDlgButtonChecked(hDlg, IDC_BINARY)) output='b';
if (IsDlgButtonChecked(hDlg, IDC_ETYP)) datatype = 'e';
if (IsDlgButtonChecked(hDlg, IDC_ATYP)) datatype = 'a';
if (IsDlgButtonChecked(hDlg, IDC_MTYP)) datatype = 'm';
if (IsDlgButtonChecked(hDlg, IDC_TTYP)) datatype = 't';
if (IsDlgButtonChecked(hDlg, IDC_ZTYP)) datatype = 'z';
if (IsDlgButtonChecked(hDlg, IDC_SMTYP)) datatype = 's';
if (IsDlgButtonChecked(hDlg, IDC_VSTYP)) datatype = 'v';
if (IsDlgButtonChecked(hDlg, IDC_DTYP)) datatype = 'd';
#ifdef S7_300
blockno = GetDlgItemInt(hDlg, IDC_BLOCKNO, &result,0);
SetDlgItemInt(hDlg, IDC_BLOCKNO,blockno,0);
#endif
no = GetDlgItemInt(hDlg, IDC_FROMNO, &result,0);
SetDlgItemInt(hDlg, IDC_FROMNO,no,0);
amount = 1;
// amount = GetDlgItemInt(hDlg, IDC_AMOUNT, &result,0);
// SetDlgItemInt(hDlg, IDC_AMOUNT, amount,0);
GetDlgItemText(hDlg, IDC_EDIT_VALUE, 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,&value,4);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -