Explorar o código

20200930, 测试snap7通信

huli %!s(int64=4) %!d(string=hai) anos
pai
achega
771d2299a5

+ 9 - 7
dispatch/dispatch_communication.cpp

@@ -52,10 +52,10 @@ Error_manager Dispatch_communication::updata_receive_buf()
 	std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
 	std::unique_lock<std::mutex> t_lock2(m_data_lock);
 
-	memcpy(&m_response_from_plc_to_dispatch, &m_receive_buf_map[0], m_receive_buf_map[0].m_size);
-	memcpy(&m_status_from_plc_to_dispatch, &m_receive_buf_map[1], m_receive_buf_map[1].m_size);
-
+	memcpy(&m_response_from_plc_to_dispatch, m_receive_buf_map[0].mp_buf_obverse, m_receive_buf_map[0].m_size);
+	memcpy(&m_status_from_plc_to_dispatch, m_receive_buf_map[1].mp_buf_obverse, m_receive_buf_map[1].m_size);
 
+	std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_heartbeat = " << (int)m_status_from_plc_to_dispatch.m_heartbeat << std::endl;
 	std::cout << " huli test :::: " << " m_response_from_plc_to_dispatch.m_command_key = " << m_response_from_plc_to_dispatch.m_command_key << std::endl;
 	std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_elevator_coordinates = " << m_status_from_plc_to_dispatch.m_elevator_coordinates << std::endl;
 	std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_carrier_coordinates = " << m_status_from_plc_to_dispatch.m_carrier_coordinates << std::endl;
@@ -67,12 +67,14 @@ Error_manager Dispatch_communication::updata_send_buf()
 	std::unique_lock<std::mutex> t_lock1(m_send_buf_lock);
 	std::unique_lock<std::mutex> t_lock2(m_data_lock);
 
-	memcpy(&m_request_from_dispatch_to_plc.m_command_key, "from_dispatch_to_plc_123", 24);
-	m_status_from_dispatch_to_plc.m_year = 2020;
+	memcpy(m_request_from_dispatch_to_plc.m_command_key, "from_dispatch_to_plc_123", 20);
+	m_status_from_dispatch_to_plc.m_year = (unsigned short)20;
+	m_status_from_dispatch_to_plc.m_day = 20;
+	m_status_from_dispatch_to_plc.m_heartbeat = 20;
 
-	memcpy(&m_send_buf_map[0], &m_request_from_dispatch_to_plc, m_send_buf_map[0].m_size);
+	memcpy(m_send_buf_map[0].mp_buf_obverse, &m_request_from_dispatch_to_plc, m_send_buf_map[0].m_size);
 	m_send_buf_map[0].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION);
-	memcpy(&m_send_buf_map[1], &m_status_from_dispatch_to_plc, m_send_buf_map[1].m_size);
+	memcpy(m_send_buf_map[1].mp_buf_obverse, &m_status_from_dispatch_to_plc, m_send_buf_map[1].m_size);
 	m_send_buf_map[1].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION);
 
 

+ 6 - 0
main.cpp

@@ -63,6 +63,12 @@ int main(int argc,char* argv[])
 	
 	Error_manager t_error;
 
+	std::cout << " huli test :::: " << " sizeof(Dispatch_communication::Request_from_dispatch_to_plc) = " << sizeof(Dispatch_communication::Request_from_dispatch_to_plc) << std::endl;
+	std::cout << " huli test :::: " << " sizeof(Dispatch_communication::Response_from_plc_to_dispatch) = " << sizeof(Dispatch_communication::Response_from_plc_to_dispatch) << std::endl;
+	std::cout << " huli test :::: " << " sizeof(Dispatch_communication::Status_from_dispatch_to_plc) = " << sizeof(Dispatch_communication::Status_from_dispatch_to_plc) << std::endl;
+	std::cout << " huli test :::: " << " sizeof(Dispatch_communication::Status_from_plc_to_dispatch) = " << sizeof(Dispatch_communication::Status_from_plc_to_dispatch) << std::endl;
+
+
 	t_error = Dispatch_communication::get_instance_references().communication_init();
 	std::cout << " huli test :::: " << " t_error = " << t_error << std::endl;
 	std::cout << "Dispatch_communication = " << Dispatch_communication::get_instance_references().get_status() << std::endl;

+ 4 - 1
setting/snap7_communication.prototxt

@@ -1,7 +1,10 @@
 
 snap7_communication_parameters
 {
-    ip_string:"192.168.0.1"
+  #  ip_string:"192.168.0.1"
  #   ip_string:"192.168.2.134"
+
+ ip_string:"192.168.0.6"
+
 }
 

+ 2 - 0
snap7_communication/snap7_buf.cpp

@@ -68,7 +68,9 @@ Snap7_buf::Snap7_buf(int id, int start_index, int size, Communication_mode commu
 	if ( size > 0)
 	{
 		mp_buf_obverse = (void*)malloc(size);
+		memset(mp_buf_obverse, 0, size);
 		mp_buf_reverse = (void*)malloc(size);
+		memset(mp_buf_reverse, 0, size);
 		m_size = size;
 	}
 }

+ 28 - 2
snap7_communication/snap7_communication_base.cpp

@@ -239,14 +239,34 @@ Error_manager Snap7_communication_base::read_data_buf(Snap7_buf& snap7_buf)
 	if ( snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION)
 	{
 		std::unique_lock<std::mutex> lck(m_communication_lock);
-		int result = m_snap7_client.AsDBRead(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size, snap7_buf.mp_buf_reverse);
+//		int result = m_snap7_client.AsDBRead(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size, snap7_buf.mp_buf_reverse);
+
+		char t_buf[20] ;
+		memset(t_buf, 0, 20);
+		int result = m_snap7_client.AsDBRead(123, 10, 30, t_buf);
+		unsigned char ccccc = *((unsigned char*)t_buf);
+		std::cout << " huli test :::: " << " ccccc = " << (unsigned int)ccccc << std::endl;
+
+
+		std::cout << " huli test :::: " << " AsDBRead result = " <<  result << std::endl;
 		if ( snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION )
 		{
 			snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
 		}
+		std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
 		if (result==0)
 		{
 			t_error = reverse_byte(snap7_buf.mp_buf_reverse, snap7_buf.mp_buf_obverse, snap7_buf.m_size);
+
+//			unsigned int ccccc = *((unsigned int*)snap7_buf.mp_buf_reverse);
+//
+//			std::cout << " huli test :::: " << " snap7_buf.m_id = " << snap7_buf.m_id << std::endl;
+//			std::cout << " huli test :::: " << " snap7_buf.m_start_index = " << snap7_buf.m_start_index << std::endl;
+//			std::cout << " huli test :::: " << " snap7_buf.m_size = " << snap7_buf.m_size << std::endl;
+//			std::cout << " huli test :::: " << " ccccc = " << (unsigned int)ccccc << std::endl;
+
+
+
 			if ( t_error != Error_code::SUCCESS )
 			{
 				return t_error;
@@ -278,10 +298,12 @@ Error_manager Snap7_communication_base::write_data_buf(Snap7_buf& snap7_buf)
 		std::unique_lock<std::mutex> lck(m_communication_lock);
 		int result = m_snap7_client.AsDBWrite(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,
 											  snap7_buf.mp_buf_reverse);
+		std::cout << " huli test :::: " << " AsDBWrite result = " <<  result << std::endl;
 		if ( snap7_buf.m_communication_mode != Snap7_buf::ONCE_COMMUNICATION )
 		{
 			snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
 		}
+		std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
 		if (result == 0)
 		{
 			std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
@@ -306,9 +328,13 @@ Error_manager Snap7_communication_base::reverse_byte(void* p_buf_in, void* p_buf
 	}
 	char* tp_in=(char*)p_buf_in;
 	char* tp_out=(char*)p_buf_out;
+//	for(int i=0;i<size;++i)
+//	{
+//		tp_out[i]=tp_in[size-i-1];
+//	}
 	for(int i=0;i<size;++i)
 	{
-		tp_out[i]=tp_in[size-i-1];
+		tp_out[i]=tp_in[i];
 	}
 	return Error_code::SUCCESS;
 }