From VF61 , using c program to send data using modbus protocol to Outside device( tested with device and pc simulator software Docklite) and read the response from device. It happens less than 500ms.
please find the below Sample code
* Driver-specific ioctls: */
#define TIOCSRS485 0x542F
#define MOD_FN_R 0x03
#define timecnt 3
#define bytecount 9
unsigned char writebuf[8];
int fid;
int uart_init_485(void)
{
struct termios options;
struct serial_rs485 rs485conf;
fid = open("/dev/ttyLP2" , O_RDWR| O_NONBLOCK | O_NDELAY ); //open AXIO card port for VH-demand
if ( fid < 0 )
{
perror("/dev/ttyLP2");
return -1;
}
else
{
printf("port opened /dev/ttyLP2 - 485\n");
}
rs485conf.flags |= SER_RS485_ENABLED; //Enable RS485 mode
rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND); //set logical level for RTS pin equal to 0 when sending
rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; // Set logical level for RTS pin equal to 1 after sending
rs485conf.delay_rts_before_send = 1000; // Set rts delay before send, if needed
rs485conf.delay_rts_after_send = 1000; //Set rts delay after send, if needed
if (ioctl (fid, TIOCSRS485, &rs485conf) < 0)//Read ttyLP2 by ioctl
{
perror("/dev/ttyLP2 as 485");
}
fcntl(fid, F_SETFL, FNDELAY); // Configure port reading
tcgetattr(fid, &options); // Get the current options for the port
cfsetispeed(&options, B9600); //Set the baud rates to 9600
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD); //Enable the receiver and set local mode
options.c_cflag &= ~PARENB; // Mask the character size to 8 bits, no parity
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; //Select 8 data bits
options.c_cflag &= ~CRTSCTS; // Disable hardware flow control
options.c_lflag &= ~(ICANON | ECHO | ISIG); // Enable data to be processed as raw input
tcsetattr(fid, TCSANOW, &options); // Set the new options for the port
return 0;
}
int portread(int fid,char *data,int size)
{
int i = 0;
memset (data,' ',size);
i = read (fid,data,size);
fflush (stdout);
tcflush (fid,TCIFLUSH);
return i;
}
int uart_write(unsigned char *Uart_Write_buffer,unsigned char *aiovariable, int fid,int write_len)
{
int ret=0;
unsigned char data[20] = {0};
unsigned char readcount = 0,timeoutcount=0;
ret = write(fid,Uart_Write_buffer,write_len);
usleep(10000);
while((readcount < bytecount) && (timeoutcount<timecnt))
{
if((ret = portread(fid, data+readcount, bytecount-readcount) )>0)
{
readcount=readcount+ret;
}
timeoutcount++;
}
memcpy( aiovariable,data+3,data[2]-2);
return(1);
}
int read_reg(unsigned char AI_ID,unsigned char *variable)
{
int retval_srr = 0;
writebuf[0]=AI_ID;
writebuf[1]=MOD_FN_R;
writebuf[2]=0;
writebuf[3]=0;
writebuf[4]=0;
writebuf[5]=2;
writebuf[6]=1;
writebuf[7]=5;
if(fid <=0)
{
if(uart_init_485() < 0)
{
return 0;
}
else
{
printf("port open success->RS485\n");
}
}
retval_srr=uart_write(writebuf,variable,fid,8);
return(retval_srr);
}
void main()
{
unsigned char j,i,k=0;
unsigned char auxio[4]={0},parray[2]={0};
while(1)
{
for(j=0;j<4;j++)
{
for(i=0;i<3;i++)
{
if(read_reg (j+1,parray) ==1 )
{
k++;
}
if(k>=1)
{
auxio[j]=1;k=0;
break;
}
sleep(2);
}
}
}
}