Home > other >  Small white for help, in the Linux system using rs485 control, found that every time the return valu
Small white for help, in the Linux system using rs485 control, found that every time the return valu

Time:10-27

In Linux system using rs485 control computer end, need through the usb to 485 switch to 485 serial port control instruction, using the function to the serial port to write command, found that every time the return value is 1, the write function is less than the needs of the number of bytes written. Ask bosses should help take a look at how to solve,
Procedure is as follows:


#include
#include
#include

#include //the open () the close ()
#include //read the write () ()

#include //set the baud rate

#include
#include
#include
#include


# define DEVICE_TTYUSB "/dev/ttyUSB0
"# define MY_BAUD_RATE B9600

# define RECEIVE_BUF_WAIT_1S 1
# define RECEIVE_BUF_WAIT_2S 2
# define RECEIVE_BUF_WAIT_3S 3
# define RECEIVE_BUF_WAIT_4S 4
# define RECEIVE_BUF_WAIT_5S 5
Int func_485_transfer (int fd)
{
Int ret.
//ssize_t ret.
Char rcv_buf [1024].
int i;

Char send_buf [14]={0};
Send_buf [0]=0 XFF;
Send_buf [1]=0 x00;
Send_buf [2]=0 x00;
Send_buf [3]=0 x20;
Send_buf [4]=0 x3f;
Send_buf [5]=0 x3f;
Send_buf [6]=0 x9e;
Bzero (rcv_buf, sizeof (rcv_buf));

While (1) {
Ret=write (fd, send_buf, strlen (send_buf));
Printf (" % d \ n ", sizeof (send_buf));
Printf (" % d \ n ", ret);
If (ret==1) {
Printf (" write error device % s \ n ", DEVICE_TTYUSB);
return -1;
}
return -1;
}

return 0;

}


//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the init seriel port -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void init_ttyS (int fd)
{
Struct termios options;

Bzero (& amp; The options, sizeof (options));//the clear options

Cfsetispeed (& amp; The options, MY_BAUD_RATE);//setup baud rate
Cfsetospeed (& amp; The options, MY_BAUD_RATE);

Options. C_cflag |=(CS8 gives | CREAD);//(CS8 gives | CLOCAL | CREAD);
//options. C_iflag=IGNPAR;

Tcflush (fd, TCIFLUSH);

Tcsetattr (fd, TCSANOW, & amp; The options);

}


//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the main -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Int main (void)
{
Int fd.

Printf (" \ n RS485 TRANSFERS DATAS \ n \ n ");

//the open seriel port
Fd=open (DEVICE_TTYUSB O_RDWR);

If (fd==1) {
Printf (" open error device % s \ n ", DEVICE_TTYUSB);
}
The else {
Init_ttyS (fd);//init device
Func_485_transfer (fd);//functions provides 485
Sleep (0.05);

If (close (fd)!=0) printf (" close the error device % s ", DEVICE_TTYUSB);
}

return 0;
}//end main
  • Related