@@ -368,22 +368,36 @@ cip_error_code_e ab_cip_write_double(int fd, const char* address, double val)
368368
369369cip_error_code_e ab_cip_write_string (int fd , const char * address , int length , const char * val )
370370{
371+ // this functio use Type Code: CIP_Type_Byte
372+ // NOT SUPPORT Type Code 0xDA
371373 cip_error_code_e ret = CIP_ERROR_CODE_FAILED ;
372374 if (fd > 0 && address != NULL && val != NULL )
373375 {
374- byte write_len = length + 1 ;
376+ // 1. write length is even
377+ byte write_len = length ;
378+ if (write_len % 2 == 1 )
379+ write_len += 1 ;
380+
375381 byte_array_info write_data = { 0 };
376382 write_data .data = (byte * )malloc (write_len );
377383 if (write_data .data != NULL )
378384 {
379385 memset (write_data .data , 0 , write_len );
380- memcpy (write_data .data , & write_len , 1 );
381- memcpy (write_data .data + 1 , val , length );
386+ memcpy (write_data .data , val , length );
382387 write_data .length = write_len ;
383388
389+ // 2. write length
384390 char temp_addr [100 ] = { 0 };
385391 sprintf (temp_addr , "%s.LEN" , address );
386- ret = write_value (fd , temp_addr , 1 , CIP_Type_Byte , write_data );
392+ ret = ab_cip_write_int32 (fd , temp_addr , length );
393+
394+ // 3. write data
395+ if (ret == CIP_ERROR_CODE_OK )
396+ {
397+ memset (temp_addr , 0 , 100 );
398+ sprintf (temp_addr , "%s.DATA[0]" , address );
399+ ret = write_value (fd , temp_addr , 1 , CIP_Type_Byte , write_data );
400+ }
387401 }
388402 else
389403 {
0 commit comments