RS485 Automatic Direction switching mode

Source: Internet
Author: User

RS485 communication, in the re-application, can be set to 485 for automatic direction switching mode. Prevent due to our manual switch pin, the existence of time problems, because we manually switch, each time the serial port, the PIN will be pulled high or low to switch to write mode, after the completion of the function of the serial port, it is necessary to delay a period of time, to ensure that the data in the underlying hardware layer transmission is complete, Then pull the pin high or pull down in the switch to read state, so that due to the time you calculate the error in your pull high-pull low pin time error will cause 485 of communication instability. Therefore, if we adopt 485 automatic direction switching mode, will reduce this communication error, when each time the serial port, he will switch to write state, after writing, and automatically switch to read state.

The 485 auto-switching application operations on ARM Linux are explained as follows:


Open the serial port method, and set the properties of the serial port, more normal we use the serial port in the application, the same way to open the serial port can be.

After opening the serial port, we have to proceed to the next side, set a RS485 automatic direction mode switch to use the structure and a few macros, the structure will be filled in the future, with the IOCTL function written to the open serial file descriptor can be.


#define TIOCGRS485 0x542e

#define TIOCSRS485 0x542f

struct my_serial_rs485//about 485 automatic direction switching mode The structure of the content to be set.

{

unsigned longflags; /* RS485 Feature flags * *

#define SER_RS485_ENABLED(1 << 0)/* If ENABLED */

#define Ser_rs485_rts_on_send(1 << 1)/* Logical level for

RTS PIN when

Sending * *

#define Ser_rs485_rts_after_send(1 << 2)/* Logical level for

RTS pin after sent*/

#define SER_RS485_RX_DURING_TX(1 << 4)

unsigned longdelay_rts_before_send; /* Delay before Send (milliseconds) */

unsigned longdelay_rts_after_send; /* Delay after send (milliseconds) */

unsigned longpadding[5]; /* Memory is cheap, new structs

is a royal PITA. */

};


int main (void)

{

struct my_serial_rs485 rs485conf;

BOOL flag = FALSE;

Flag = g_com1.open_com (serial_com_10, O_RDWR |    O_noctty); Open the serial port, of course, this serial port class is to you to package the

if (!flag)

{

printf ("Open com is failed.\n");

_exit (-1);

}


/**************458config Filling ***********************************************/

/* Enable RS485 mode: */

Rs485conf.flags |= ser_rs485_enabled; Set Enable to 485


/* Set logical level for RTS pin equal to 1 when sending: */

Rs485conf.flags |= Ser_rs485_rts_on_send; Set the RTS, when the logic level is high, for sending

Rs485conf.flags |= Ser_rs485_rts_after_send;


/* Set logical level for RTS pin equal to 0 after sending: */

Rs485conf.flags &= ~ (ser_rs485_rts_after_send); Set RTS When the logic level is low after sending

Rs485conf.flags &= ~ (ser_rs485_rts_on_send);

/****************************************************************************/


Use of 485

Pthread_mutex_lock (&MUTEX_NET1);

Len = Cclinet1.    Recv_from_net (Recv_buffer, Max_buf_len); Read data from the network, Len indicates the length of the read

Pthread_mutex_unlock (&MUTEX_NET1);


Rs_485_wait_time = ten * Len/baudrate_list[g_info.baud] * 1000; MS//calculates how long it takes to output bytes of this len length according to Len's length and baud rate

/********************************** Auto Switch 485******************/

Pthread_mutex_lock (&MUTEX_COM1);

Rs485conf.delay_rts_after_send = Rs_485_wait_time; Fills the calculated time of the Len length byte into the members of the struct, indicating that the serial data is sent out after a delay of some time

if (IOCTL (G_COM1.M_FD, TIOCSRS485, &rs485conf) < 0)//writes the populated structure to the file descriptor

{

/* Error handling.*/

printf ("IOCTL TIOCSRS485 error.\n");

}

Pthread_mutex_unlock (&MUTEX_COM1);


After/******************** can be like Operation 232 serial port like to operate 485 of the serial port. Because each time to write the data, will automatically switch to write mode, and delay more accurate time to automatically switch to read mode.


return 0;


}


Since the program is part of the project development, to do 485 records, not complete, but about 485 of the use of automatic switching mode, but this is basically the case.



This article from "Whylinux" blog, declined reprint!

RS485 Automatic Direction switching mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.