Это часть огромного кода, где я общаюсь с роботом и s7 plc, работая параллельно. Проблема в том, что, прежде чем я, хотя проблема была частью коммуникационного взаимодействия, теперь я сократил весь свой код до клиент-серверного приложения, которое также терпит неудачу. Код в основном работает следующим образом; каждые 50 мс клиент отправляет на сервер сообщение «привет». Когда отправляется 20 сообщений, запускается таймер 7 секунд. Когда таймер запускается, запускается второй поток, и он меняет переменную f = 1, и она завершается. В то же время основная программа будет печатать привет, но когда эта переменная f изменяется на 1, она должна печатать только «aaaaa», но это никогда не произойдет.Проблема связи с tcp при запуске нового потока таймером
С ПЛК возникли проблемы с дефектами сегментации. Моя идея заключается в том, что TCP-связь выходит из строя (почему?), И ПЛК блокирует доступ. Я пробовал одну и ту же программу, прежде чем просто двигать руку робота и анализировать с помощью wirehark, вы можете видеть, как TCP все время отправляется, но опять же, робот останавливается. И, наконец, с помощью этого клиентского приложения можно увидеть, как они не работают так, как ожидалось.
Я надеюсь, что кто-то из вас может мне помочь.
Большое спасибо;)
КЛИЕНТ
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h> // Primitive System Data Types
#include <errno.h> // Errors */
#include <sys/wait.h> // Wait for Process Termination
#include "plc_interface.h"
#include <netinet/in.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/socket.h>
\t timer_t firstTimerID;
\t timer_t secondTimerID;
struct itimerspec it;
int t_block3=5;
\t pthread_t thread_robot;
\t pthread_attr_t thread_attr;
\t int res, z;
\t int f=0;
//THREAD FUNCTION ONCE THE REQUIRED BLOCK IS DETECTED
void *detection_robot(){
\t printf("First\n");
\t //f==1 to send the message aaaaaa
\t f=1;
\t pthread_exit(NULL);
}
int setTimer(timer_t * timerID, int time) {
struct itimerspec its;
//Interval for starting again
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
//Timer time
its.it_value.tv_sec = time;
its.it_value.tv_nsec = 0;
//Arm/disarmer a per process time
timer_settime (*timerID, 0, &its, NULL);
return 0;
}
//TIMER INTERRUPTION
static void timerHandler (int sig, siginfo_t * si, void *uc_) {
\t \t timer_t *tidp;
\t \t int iret;
\t \t tidp = si->si_value.sival_ptr;
\t \t //Initializes the attributes for the thread
\t \t res= pthread_attr_init(&thread_attr);
\t \t res=pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED);
\t \t if (*tidp == firstTimerID) {
\t \t \t printf ("First timer\n");
\t \t \t //New thread to detect the second detection sensor
\t \t \t iret = pthread_create(&thread_robot, &thread_attr, detection_robot, NULL);
\t \t \t if(iret)
\t \t \t \t {
\t \t \t \t fprintf(stderr,"Error - pthread_create() return code: %d\n",iret);
\t \t \t \t exit(EXIT_FAILURE);
\t \t \t \t }
\t \t } else if (*tidp == secondTimerID) {
\t \t \t printf ("Second timer\n");
\t \t }
\t \t z--;
}
//ARM THE TIMERS
static int makeTimer (timer_t * timerID) {
\t \t struct sigevent te;
\t \t struct sigaction sa;
\t \t int sigNo = SIGRTMIN;
\t \t // Set up signal handler.
\t \t sa.sa_flags = SA_SIGINFO;
\t \t sa.sa_sigaction = timerHandler; //Action when singal is triggered
\t \t sigemptyset (&sa.sa_mask);
\t \t if (sigaction (sigNo, &sa, NULL) == -1) {
\t \t \t perror ("sigaction");
\t \t \t \t }
\t \t // Set and enable alarm
\t \t te.sigev_notify = SIGEV_SIGNAL; //Gnerate alarm upon expiration
\t \t te.sigev_signo = sigNo; //SIGALRM
\t \t te.sigev_value.sival_ptr = timerID; //Timer ID
\t \t //Create a per_process timer using the timer ID
\t \t timer_create (CLOCK_REALTIME, &te, timerID);
\t \t return 0;
}
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[6]={'h','e','l','l','o','\0'};
char message2[6]={'a','a','a','a','a','\0'};
char server_reply[2000];
\t //Initialize the timers
\t makeTimer(&firstTimerID);
\t makeTimer(&secondTimerID);
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(8888);
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
//keep communicating with server
\t int i=0;
\t while(1){
//Send hello every 50ms
if(send(sock , message , 6 , 0) < 0)
{
puts("Send failed");
return 1;
}
\t \t \t i++;
\t \t \t if (i==20){
\t \t \t \t \t \t setTimer (&firstTimerID, t_block3);
\t \t \t \t \t \t i=0;
\t \t \t \t \t }
\t \t \t \t \t
\t \t \t //f-> 1, send aaaaaa when timer is triggered, but here is when it get stacked
\t \t \t if (f==1) {
\t \t \t \t f=0;
\t \t \t \t if(send(sock , message2 , 6 , 0) < 0)
\t \t \t \t {
\t \t \t \t \t puts("Send failed");
\t \t \t \t \t return 1;
\t \t \t \t }}
\t \t \t usleep(500000); //500ms
\t \t }
}
SERVER
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
//Bind
if(bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
while(1){
//Receive a message from client
recv(client_sock , client_message , 2000 , 0);
printf("%s\n", client_message);
\t }
}
что выход? можете ли вы запустить его с помощью отладчика и сказать, где он разбился? –