Hello all,

I have a 2 port Moxa Nport 5232 device. I am trying to get this
communicating to a 2 wire RS485 device.
After configuring the Moxa I got 2 ports on my Linux system
(/dev/ttyr00 & /dev/ttyr01).

I wrote a C program to read & write messages to one of the port
(/dev/ttyr00).
I am treating the /dev/ttyr00 as a standard Com port and setting the
parameters according to my device requirement (9600 8-N-1). Here is my
code snip.

Port Initialization:
struct termios tty_state;
if (tcgetattr(fd, &tty_state) < 0)
return 0;
/* set raw mode - no flow control - no echo - 9600 - 8-N-1 */
tty_state.c_lflag &= ~(ICANON | IEXTEN | ISIG | ECHO);
tty_state.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON | BRKINT);
tty_state.c_oflag &= ~OPOST;
tty_state.c_cflag |= CS8;
tty_state.c_cflag &= ~CRTSCTS;
tty_state.c_cflag &= ~(IXON | IXOFF | IXANY);
cfsetispeed(&tty_state, B9600);
cfsetospeed(&tty_state, B9600);
if (tcsetattr(fd, TCSAFLUSH, &tty_state) < 0)
return 0;

Writing to Port:
int writeport(int fd, char *chars,int len)
{
int n = write(fd,chars,len);
if (n < 0)
return n;
}

Reading from Port:
int readport(int fd, char *result)
{
int n;
fcntl(fd, F_SETFL, 0);
n = read(fd, result, BUFF_SZ);
if (n < 0) {
if (errno == EAGAIN) {
printf("\nSERIAL EAGAIN ERROR");
return 0;
}
else {
printf("\nSERIAL read error %d %s", errno,
strerror(errno));
return 0;
}
}
return n;
}

Even though I could communicate to my device using the above code, what
happens is when the device replies often times, my code reads partial
message and then the next I read the port again, it sends the rest of
the message. I am lost at this point & I am seeking help from you all.
My questions are,

1) Am I initializing the port right (9600 - 8N1) (I have confirmed with
Moxa that my configurations are OK on the device).
2) Am I doing anything wrong with the read?
3) Even though Moxa's device does not require any flow control (Auto
Turnaround) do I still need to flip the RTS for my 1/2 duplex RS485
communication?
4) Do I have to use any sort of interrupts to handle this task?

Please let me know if I am doing anything wrong here? Any help is much
appreciated.

Thanks

CM