Selaa lähdekoodia

20200930, 调试snap7通信

huli 4 vuotta sitten
vanhempi
commit
d15abb7f75

+ 3 - 0
dispatch/dispatch_communication.cpp

@@ -52,6 +52,9 @@ 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].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);
 

+ 73 - 0
main.cpp

@@ -16,6 +16,7 @@
 #include "./dispatch/dispatch_manager.h"
 #include "./dispatch/dispatch_communication.h"
 
+#include <algorithm>    // std::for_each
 
 
 #define LIVOX_NUMBER	     2
@@ -44,9 +45,81 @@ GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
 
 #include <chrono>
 using namespace std;
+
+void myfunction (int i) {  // function:
+	std::cout << ' ' << i;
+}
+
 int main(int argc,char* argv[])
 {
 
+	TSnap7Client 					t_snap7_client;			//通信的客户端
+
+	int result=t_snap7_client.ConnectTo("192.168.2.110",0,1);
+	std::cout << " huli test :::: " << " result = " << result << std::endl;
+
+//	std::this_thread::sleep_for(std::chrono::seconds(1));
+
+	unsigned char t_read[20] = {0};
+	memset(t_read, 0, 20);
+	result = t_snap7_client.AsDBRead(120, 0, 1, t_read);
+	if ( result == 0 )
+	{
+		t_snap7_client.WaitAsCompletion(100);
+	}
+	std::cout << " huli test :::: " << " t_read = " << (int)t_read[0] << std::endl;
+	std::cout << " huli test :::: " << " AsDBRead result = " <<  result << std::endl;
+
+//	std::this_thread::sleep_for(std::chrono::seconds(1));
+
+	short f = -4;
+	unsigned char * p_asd = (unsigned char *)&f;
+
+//	unsigned char t_write[20] = {0};
+//	memset(t_write, 20, 20);
+//	t_write[0] = p_asd[3];
+//	t_write[1] = p_asd[2];
+//	t_write[2] = p_asd[1];
+//	t_write[3] = p_asd[0];
+
+//	t_write[0] = p_asd[1];
+//	t_write[1] = p_asd[0];
+
+	unsigned char t_write[20] = "鄂A123456";
+//	t_write[19] = 's';
+
+
+
+	result = t_snap7_client.AsDBWrite(120, 72, 20, t_write);
+	if ( result == 0 )
+	{
+		t_snap7_client.WaitAsCompletion(100);
+	}
+	std::cout << " huli test :::: " << " t_read = " << (int)t_write[0] << std::endl;
+	std::cout << " huli test :::: " << " AsDBRead result = " <<  result << std::endl;
+
+	return 0;
+
+
+
+//	char a=1;
+//	typeof(a) b=2;
+//
+//	typeid(a).name();
+//	std::cout << " huli test :::: " << " a = " << a << std::endl;
+//	std::cout << " huli test :::: " << " typeid(a).name() = " << typeid(a).name() << std::endl;
+//	std::cout << " huli test :::: " << " b = " << b << std::endl;
+//	std::cout << " huli test :::: " << " typeid(b).name() = " << typeid(b).name() << std::endl;
+//
+//
+//	std::vector<int> xcv;
+//	xcv.push_back(123);
+//	for_each(xcv.begin(), xcv.end(), myfunction);
+//
+////	foreach
+//
+//	return 0;
+
 
 	const char* logPath = "./";
 	google::InitGoogleLogging("LidarMeasurement");

+ 9 - 1
snap7_communication/snap7_communication_base.cpp

@@ -38,7 +38,7 @@ Error_manager Snap7_communication_base::communication_init_from_protobuf(Snap7_c
 {
 	LOG(INFO) << " ---Communication_socket_base::communication_init() --- "<< this;
 	Error_manager t_error;
-
+	snap7_communication_parameter_all.DebugString();
 	if ( snap7_communication_parameter_all.snap7_communication_parameters().has_ip_string() )
 	{
 		m_ip_string = snap7_communication_parameter_all.snap7_communication_parameters().ip_string();
@@ -244,6 +244,10 @@ Error_manager Snap7_communication_base::read_data_buf(Snap7_buf& snap7_buf)
 		char t_buf[20] ;
 		memset(t_buf, 0, 20);
 		int result = m_snap7_client.AsDBRead(123, 10, 30, t_buf);
+		if ( result == 0 )
+		{
+			m_snap7_client.WaitAsCompletion(100);
+		}
 		unsigned char ccccc = *((unsigned char*)t_buf);
 		std::cout << " huli test :::: " << " ccccc = " << (unsigned int)ccccc << std::endl;
 
@@ -298,6 +302,10 @@ 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);
+		if ( result == 0 )
+		{
+			m_snap7_client.WaitAsCompletion(100);
+		}
 		std::cout << " huli test :::: " << " AsDBWrite result = " <<  result << std::endl;
 		if ( snap7_buf.m_communication_mode != Snap7_buf::ONCE_COMMUNICATION )
 		{