Skip to content

Commit 525089f

Browse files
committed
Update README.md
1 parent 2429f8f commit 525089f

File tree

1 file changed

+163
-159
lines changed

1 file changed

+163
-159
lines changed

README.md

Lines changed: 163 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010
#### 头文件
1111

1212
```c
13-
#include "ab_cip.h" //协议提供方法接口
14-
#include "typedef.h" //部分类型宏定义
13+
#include "ab_cip.h" //协议提供方法接口
14+
#include "typedef.h" //部分类型宏定义
1515
```
1616

1717
## 西门子PLC地址说明
18-
#### 连接属性
18+
19+
#### 连接属性
20+
1921
- port 端口号,通常为44818
2022
- plc_type plc 型号,适用1756 ControlLogix, 1756 GuardLogix, 1769 CompactLogix, 1769 Compact GuardLogix, 1789SoftLogix, 5069 CompactLogix, 5069 Compact GuardLogix, Studio 5000 Logix Emulate等型号
23+
2124
#### PLC地址分类
22-
支持使用标签的形式进行读写操作
2325

26+
支持使用标签的形式进行读写操作
2427

2528
## 实现方法
2629

@@ -35,6 +38,7 @@ bool ab_cip_disconnect(int fd);
3538
```
3639
3740
#### 2.读取数据
41+
3842
```c
3943
cip_error_code_e ab_cip_read_bool(int fd, const char* address, bool* val);
4044
cip_error_code_e ab_cip_read_short(int fd, const char* address, short* val);
@@ -64,9 +68,10 @@ cip_error_code_e ab_cip_write_string(int fd, const char* address, int length, co
6468
```
6569
6670
## 使用样例
71+
6772
完整样例参见代码中**main.c**文件,如下提供主要代码和使用方法:
6873
69-
*读取地址,格式为"**F**","**D**"
74+
读取地址,格式为"**F**","**D**"
7075
7176
```c
7277
#ifdef _WIN32
@@ -83,178 +88,177 @@ cip_error_code_e ab_cip_write_string(int fd, const char* address, int length, co
8388
int main(int argc, char** argv)
8489
{
8590
#ifdef _WIN32
86-
WSADATA wsa;
87-
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
88-
{
89-
return -1;
90-
}
91+
WSADATA wsa;
92+
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
93+
{
94+
return -1;
95+
}
9196
#endif
9297
93-
char* plc_ip = "192.168.123.26";
94-
int plc_port = 44818;
95-
if (argc > 1)
96-
{
97-
plc_ip = argv[1];
98-
plc_port = atoi(argv[2]);
99-
}
100-
101-
int fd = -1;
102-
int slot = 0;
103-
bool ret = ab_cip_connect(plc_ip, plc_port, 0, &fd);
104-
if (ret && fd > 0)
105-
{
106-
cip_error_code_e ret = CIP_ERROR_CODE_FAILED;
107-
108-
const int TEST_COUNT = 5000;
109-
const int TEST_SLEEP_TIME = 1000;
110-
int faild_count = 0;
111-
char address[50] = { 0 };
112-
int i = 0;
113-
114-
for (i = 0; i < TEST_COUNT; i++)
115-
{
116-
printf("==============Test count: %d==============\n", i + 1);
117-
bool all_success = false;
118-
//////////////////////////////////////////////////////////////////////////
119-
bool val = true;
120-
strcpy(address, "E");
121-
ret = ab_cip_write_bool(fd, address, val);
122-
printf("Write\t %s \tbool:\t %d, \tret: %d\n", address, val, ret);
123-
GET_RESULT(ret);
124-
125-
val = false;
126-
ret = ab_cip_read_bool(fd, address, &val);
127-
printf("Read\t %s \tbool:\t %d\n", address, val);
128-
GET_RESULT(ret);
129-
130-
//////////////////////////////////////////////////////////////////////////
131-
short w_s_val = 23;
132-
strcpy(address, "A");
133-
ret = ab_cip_write_short(fd, address, w_s_val);
134-
printf("Write\t %s \tshort:\t %d, \tret: %d\n", address, w_s_val, ret);
135-
GET_RESULT(ret);
136-
137-
short s_val = 0;
138-
ret = ab_cip_read_short(fd, address, &s_val);
139-
printf("Read\t %s \tshort:\t %d\n", address, s_val);
140-
GET_RESULT(ret);
141-
142-
//////////////////////////////////////////////////////////////////////////
143-
ushort w_us_val = 255;
144-
strcpy(address, "A");
145-
ret = ab_cip_write_ushort(fd, address, w_us_val);
146-
printf("Write\t %s \tushort:\t %d, \tret: %d\n", address, w_us_val, ret);
147-
GET_RESULT(ret);
148-
149-
ushort us_val = 0;
150-
ret = ab_cip_read_ushort(fd, address, &us_val);
151-
printf("Read\t %s \tushort:\t %d\n", address, us_val);
152-
GET_RESULT(ret);
153-
154-
//////////////////////////////////////////////////////////////////////////
155-
int32 w_i_val = 12345;
156-
strcpy(address, "B");
157-
ret = ab_cip_write_int32(fd, address, w_i_val);
158-
printf("Write\t %s \tint32:\t %d, \tret: %d\n", address, w_i_val, ret);
159-
GET_RESULT(ret);
160-
161-
int i_val = 0;
162-
ret = ab_cip_read_int32(fd, address, &i_val);
163-
printf("Read\t %s \tint32:\t %d\n", address, i_val);
164-
GET_RESULT(ret);
165-
166-
//////////////////////////////////////////////////////////////////////////
167-
uint32 w_ui_val = 22345;
168-
ret = ab_cip_write_uint32(fd, address, w_ui_val);
169-
printf("Write\t %s \tuint32:\t %d, \tret: %d\n", address, w_ui_val, ret);
170-
GET_RESULT(ret);
171-
172-
uint32 ui_val = 0;
173-
ret = ab_cip_read_uint32(fd, address, &ui_val);
174-
printf("Read\t %s \tuint32:\t %d\n", address, ui_val);
175-
GET_RESULT(ret);
176-
177-
//////////////////////////////////////////////////////////////////////////
98+
char* plc_ip = "192.168.123.26";
99+
int plc_port = 44818;
100+
if (argc > 1)
101+
{
102+
plc_ip = argv[1];
103+
plc_port = atoi(argv[2]);
104+
}
105+
106+
int fd = -1;
107+
int slot = 0;
108+
bool ret = ab_cip_connect(plc_ip, plc_port, 0, &fd);
109+
if (ret && fd > 0)
110+
{
111+
cip_error_code_e ret = CIP_ERROR_CODE_FAILED;
112+
113+
const int TEST_COUNT = 5000;
114+
const int TEST_SLEEP_TIME = 1000;
115+
int faild_count = 0;
116+
char address[50] = { 0 };
117+
int i = 0;
118+
119+
for (i = 0; i < TEST_COUNT; i++)
120+
{
121+
printf("==============Test count: %d==============\n", i + 1);
122+
bool all_success = false;
123+
//////////////////////////////////////////////////////////////////////////
124+
bool val = true;
125+
strcpy(address, "E");
126+
ret = ab_cip_write_bool(fd, address, val);
127+
printf("Write\t %s \tbool:\t %d, \tret: %d\n", address, val, ret);
128+
GET_RESULT(ret);
129+
130+
val = false;
131+
ret = ab_cip_read_bool(fd, address, &val);
132+
printf("Read\t %s \tbool:\t %d\n", address, val);
133+
GET_RESULT(ret);
134+
135+
//////////////////////////////////////////////////////////////////////////
136+
short w_s_val = 23;
137+
strcpy(address, "A");
138+
ret = ab_cip_write_short(fd, address, w_s_val);
139+
printf("Write\t %s \tshort:\t %d, \tret: %d\n", address, w_s_val, ret);
140+
GET_RESULT(ret);
141+
142+
short s_val = 0;
143+
ret = ab_cip_read_short(fd, address, &s_val);
144+
printf("Read\t %s \tshort:\t %d\n", address, s_val);
145+
GET_RESULT(ret);
146+
147+
//////////////////////////////////////////////////////////////////////////
148+
ushort w_us_val = 255;
149+
strcpy(address, "A");
150+
ret = ab_cip_write_ushort(fd, address, w_us_val);
151+
printf("Write\t %s \tushort:\t %d, \tret: %d\n", address, w_us_val, ret);
152+
GET_RESULT(ret);
153+
154+
ushort us_val = 0;
155+
ret = ab_cip_read_ushort(fd, address, &us_val);
156+
printf("Read\t %s \tushort:\t %d\n", address, us_val);
157+
GET_RESULT(ret);
158+
159+
//////////////////////////////////////////////////////////////////////////
160+
int32 w_i_val = 12345;
161+
strcpy(address, "B");
162+
ret = ab_cip_write_int32(fd, address, w_i_val);
163+
printf("Write\t %s \tint32:\t %d, \tret: %d\n", address, w_i_val, ret);
164+
GET_RESULT(ret);
165+
166+
int i_val = 0;
167+
ret = ab_cip_read_int32(fd, address, &i_val);
168+
printf("Read\t %s \tint32:\t %d\n", address, i_val);
169+
GET_RESULT(ret);
170+
171+
//////////////////////////////////////////////////////////////////////////
172+
uint32 w_ui_val = 22345;
173+
ret = ab_cip_write_uint32(fd, address, w_ui_val);
174+
printf("Write\t %s \tuint32:\t %d, \tret: %d\n", address, w_ui_val, ret);
175+
GET_RESULT(ret);
176+
177+
uint32 ui_val = 0;
178+
ret = ab_cip_read_uint32(fd, address, &ui_val);
179+
printf("Read\t %s \tuint32:\t %d\n", address, ui_val);
180+
GET_RESULT(ret);
181+
182+
//////////////////////////////////////////////////////////////////////////
178183
#if false
179-
int64 w_i64_val = 333334554;
180-
strcpy(address, "N");
181-
ret = ab_cip_write_int64(fd, address, w_i64_val);
182-
printf("Write\t %s \tuint64:\t %lld, \tret: %d\n", address, w_i64_val, ret);
183-
GET_RESULT(ret);
184+
int64 w_i64_val = 333334554;
185+
strcpy(address, "N");
186+
ret = ab_cip_write_int64(fd, address, w_i64_val);
187+
printf("Write\t %s \tuint64:\t %lld, \tret: %d\n", address, w_i64_val, ret);
188+
GET_RESULT(ret);
184189
185-
int64 i64_val = 0;
186-
ret = ab_cip_read_int64(fd, address, &i64_val);
187-
printf("Read\t %s \tint64:\t %lld\n", address, i64_val);
188-
GET_RESULT(ret);
190+
int64 i64_val = 0;
191+
ret = ab_cip_read_int64(fd, address, &i64_val);
192+
printf("Read\t %s \tint64:\t %lld\n", address, i64_val);
193+
GET_RESULT(ret);
189194
190195
#endif
191-
//////////////////////////////////////////////////////////////////////////
192-
uint64 w_ui64_val = 4333334554;
193-
strcpy(address, "N");
194-
ret = ab_cip_write_uint64(fd, address, w_ui64_val);
195-
printf("Write\t %s \tuint64:\t %lld, \tret: %d\n", address, w_ui64_val, ret);
196-
GET_RESULT(ret);
197-
198-
int64 ui64_val = 0;
199-
ret = ab_cip_read_uint64(fd, address, &ui64_val);
200-
printf("Read\t %s \tuint64:\t %lld\n", address, ui64_val);
201-
GET_RESULT(ret);
202-
203-
//////////////////////////////////////////////////////////////////////////
204-
float w_f_val = 32.454f;
205-
strcpy(address, "C");
206-
ret = ab_cip_write_float(fd, address, w_f_val);
207-
printf("Write\t %s \tfloat:\t %f, \tret: %d\n", address, w_f_val, ret);
208-
GET_RESULT(ret);
209-
210-
float f_val = 0;
211-
ret = ab_cip_read_float(fd, address, &f_val);
212-
printf("Read\t %s \tfloat:\t %f\n", address, f_val);
213-
GET_RESULT(ret);
214-
215-
//////////////////////////////////////////////////////////////////////////
196+
//////////////////////////////////////////////////////////////////////////
197+
uint64 w_ui64_val = 4333334554;
198+
strcpy(address, "N");
199+
ret = ab_cip_write_uint64(fd, address, w_ui64_val);
200+
printf("Write\t %s \tuint64:\t %lld, \tret: %d\n", address, w_ui64_val, ret);
201+
GET_RESULT(ret);
202+
203+
int64 ui64_val = 0;
204+
ret = ab_cip_read_uint64(fd, address, &ui64_val);
205+
printf("Read\t %s \tuint64:\t %lld\n", address, ui64_val);
206+
GET_RESULT(ret);
207+
208+
//////////////////////////////////////////////////////////////////////////
209+
float w_f_val = 32.454f;
210+
strcpy(address, "C");
211+
ret = ab_cip_write_float(fd, address, w_f_val);
212+
printf("Write\t %s \tfloat:\t %f, \tret: %d\n", address, w_f_val, ret);
213+
GET_RESULT(ret);
214+
215+
float f_val = 0;
216+
ret = ab_cip_read_float(fd, address, &f_val);
217+
printf("Read\t %s \tfloat:\t %f\n", address, f_val);
218+
GET_RESULT(ret);
219+
220+
//////////////////////////////////////////////////////////////////////////
216221
#if true
217-
double w_d_val = 12345.6789;
218-
ret = ab_cip_write_double(fd, address, w_d_val);
219-
printf("Write\t %s \tdouble:\t %lf, \tret: %d\n", address, w_d_val, ret);
220-
GET_RESULT(ret);
222+
double w_d_val = 12345.6789;
223+
ret = ab_cip_write_double(fd, address, w_d_val);
224+
printf("Write\t %s \tdouble:\t %lf, \tret: %d\n", address, w_d_val, ret);
225+
GET_RESULT(ret);
221226
222-
double d_val = 0;
223-
ret = ab_cip_read_double(fd, address, &d_val);
224-
printf("Read\t %s \tdouble:\t %lf\n", address, d_val);
225-
GET_RESULT(ret);
227+
double d_val = 0;
228+
ret = ab_cip_read_double(fd, address, &d_val);
229+
printf("Read\t %s \tdouble:\t %lf\n", address, d_val);
230+
GET_RESULT(ret);
226231
227232
#endif
228-
//////////////////////////////////////////////////////////////////////////
229-
const char sz_write[] = "[email protected]";
230-
int length = strlen(sz_write);
231-
strcpy(address, "F");
232-
ret = ab_cip_write_string(fd, address, length, sz_write);
233-
printf("Write\t %s \tstring:\t %s, \tret: %d\n", address, sz_write, ret);
234-
GET_RESULT(ret);
235-
236-
char* str_val = NULL;
237-
ret = ab_cip_read_string(fd, address, &length, &str_val);
238-
printf("Read\t %s \tstring:\t %s\n", address, str_val);
239-
free(str_val);
240-
GET_RESULT(ret);
233+
//////////////////////////////////////////////////////////////////////////
234+
const char sz_write[] = "[email protected]";
235+
int length = strlen(sz_write);
236+
strcpy(address, "F");
237+
ret = ab_cip_write_string(fd, address, length, sz_write);
238+
printf("Write\t %s \tstring:\t %s, \tret: %d\n", address, sz_write, ret);
239+
GET_RESULT(ret);
240+
241+
char* str_val = NULL;
242+
ret = ab_cip_read_string(fd, address, &length, &str_val);
243+
printf("Read\t %s \tstring:\t %s\n", address, str_val);
244+
free(str_val);
245+
GET_RESULT(ret);
241246
242247
#ifdef _WIN32
243-
Sleep(TEST_SLEEP_TIME);
248+
Sleep(TEST_SLEEP_TIME);
244249
#else
245-
usleep(TEST_SLEEP_TIME * 1000);
250+
usleep(TEST_SLEEP_TIME * 1000);
246251
#endif
247-
}
252+
}
248253
249-
printf("All Failed count: %d\n", faild_count);
254+
printf("All Failed count: %d\n", faild_count);
250255
251-
ab_cip_disconnect(fd);
252-
system("pause");
253-
}
256+
ab_cip_disconnect(fd);
257+
system("pause");
258+
}
254259
255260
#ifdef _WIN32
256-
WSACleanup();
261+
WSACleanup();
257262
#endif
258263
}
259264
```
260-

0 commit comments

Comments
 (0)