Home > OS >  Socket can contract after receiving obstruction
Socket can contract after receiving obstruction

Time:11-04

Need to use a socket can communication, in the main thread: create a socket, set up receiving threads, sending packets,
Now problems: before sending packets, receiving threads can receive packets; But the main thread to send packets, the child thread is in the select local blocking,
Excuse me what's the problem?
The code is as follows:

#include
#include
#include
#include
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include



# define uint8_t unsigned char
# define int8_t signed char
# define uint16_t unsigned short int
# define int16_t signed short int
# define uint32_t unsigned int
# define int32_t signed int
# define uint64_t unsigned long long int
# define int64_t signed long long int
# define DWORD unsigned int
# define UINT unsigned int
# define USHORT unsigned short int
# define CHAR signed CHAR
# define UCHAR unsigned char
# define BYTE unsigned char
# define ULONG unsigned int
# define INT signed INT
# define PVOID void *
# define LPVOID void *
# define HANDLE void *
# define BOOL BYTE


# define msleep (ms) usleep (* 1000) (ms)
# define min (a, b) (((a) & lt; (b))? (a) : (b))


# # ifndef AF_CAN
# define AF_CAN 29
# endif
# # ifndef PF_CAN
# define PF_CAN AF_CAN
# endif


# define errout (_s) fprintf (stderr, "the error class: % s \ n", (_s))
# define errcode (_d) fprintf (stderr, "the error code: 02 x % \ n", (_d))


The static void handle_err_frame (const struct can_frame * fr)
{
If (fr - & gt; Can_id & amp; CAN_ERR_TX_TIMEOUT) {
CAN_ERR_TX_TIMEOUT errout (" ");
}
If (fr - & gt; Can_id & amp; CAN_ERR_LOSTARB) {
CAN_ERR_LOSTARB errout (" ");
Errcode (fr - & gt; Data [0]);
}
If (fr - & gt; Can_id & amp; CAN_ERR_CRTL) {
CAN_ERR_CRTL errout (" ");
Errcode (fr - & gt; Data [1]);
}
If (fr - & gt; Can_id & amp; CAN_ERR_PROT) {
CAN_ERR_PROT errout (" ");
Errcode (fr - & gt; Data [2]);
Errcode (fr - & gt; Data [3]);
}
If (fr - & gt; Can_id & amp; CAN_ERR_TRX) {
CAN_ERR_TRX errout (" ");
Errcode (fr - & gt; Data [4]);
}
If (fr - & gt; Can_id & amp; CAN_ERR_ACK) {
CAN_ERR_ACK errout (" ");
}
If (fr - & gt; Can_id & amp; CAN_ERR_BUSOFF) {
CAN_ERR_BUSOFF errout (" ");
}
If (fr - & gt; Can_id & amp; CAN_ERR_BUSERROR) {
CAN_ERR_BUSERROR errout (" ");
}
If (fr - & gt; Can_id & amp; CAN_ERR_RESTARTED) {
CAN_ERR_RESTARTED errout (" ");
}
}
# define myerr (STR) fprintf (stderr, "% s, % s, % d: % s \ n", a __FILE__, __func__, __LINE__, STR)

The static void print_frame (struct can_frame * fr)
{
int i;
Printf (" % 08 x \ n ", fr - & gt; Can_id);
Printf (" % 08 x \ n ", CAN_EFF_MASK);
//if (fr - & gt; Data [0]!=0 x01)
//return;
Printf (" % 08 x \ n ", fr - & gt; Can_id & amp; CAN_EFF_MASK);
//printf (" % 08 x \ n ", fr - & gt; Can_id);
Printf (" DLC=% d \ n ", fr - & gt; Can_dlc);
Printf (" data="HTTP://https://bbs.csdn.net/topics/);
for (i=0; i Printf (" % 02 x ", fr - & gt; Data [I]);
printf("\n");
}
The static void generate_frame (struct can_frame * fr)
{
Printf (" generate_frame. ");
//printf (" mask: 8 x %. \ n ", CAN_EFF_MASK);
Fr - & gt; Can_id=0 x00000555;//| CAN_EFF_MASK;

Fr - & gt; Can_dlc=8;

Fr - & gt; Data [0]=0 x01;
Fr - & gt; Data [1]=0 x01;
Fr - & gt; Data [2]=0 x00;
Fr - & gt; Data [3]=0 x00;
Fr - & gt; Data [4]=0 x00;
Fr - & gt; Data [5]=0 x04;
Fr - & gt; Data [6]=0 x05;
Fr - & gt; Data [7]=0 x00;
}

Void * rx_thread (void * data)
{
Int ret=0;
Struct can_frame frdup;
Struct timeval TV;
Fd_set rset.
FD_ZERO (& amp; Rset);

Printf (" RX thread. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
Int s=* (int *) data;
FD_SET (s, & amp; Rset);

While (1)
{
/* the parse the can data. */
TV. Tv_sec=1;
TV. Tv_usec=0;
Ret=select (s + 1, & amp; Rset, NULL, NULL, NULL);
If (ret==0) {
Myerr (select "time out");
//return - 1;
}

Ret=read (s, & amp; Frdup, sizeof (frdup));
If (ret & lt; Sizeof (frdup)) {
Myerr (read "failed");
//return - 1;
continue;
}
Printf (" recv can frame, can_id: % d. -- -- -- -- -- -- -- \ n ", frdup. Can_id);
If (frdup can_id & amp; CAN_ERR_FLAG) {/* 鍑洪敊璁惧阌栾 */
Handle_err_frame (& amp; Frdup);
Myerr (" CAN device error ");
continue;
}
Print_frame (& amp; Frdup);

}

Myerr (" level RX thread. -- -- -- -- -- -- -- -- -- -- -- -- -- ");
Close (s);
Pthread_exit (0);

}


Static int test_can_rw (int fd, int master)
{
Int ret, I;
Struct can_frame fr, frdup;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related