View Single Post

   
  #8 (permalink)  
Old 01-16-2008, 06:28 PM
linda
 
Posts: n/a
Default Re: RS232 programming, helps needed...

Hi All,

here's my test code...

#define ENQ 5
#define ACK 6
#define NAK 15
#define STX 2
#define ETX 3

#define QUERY "XX" /* XX, something to represent an query, say 2
bytes */

int main()
{
int fd;
int io_status;
int write_status;
int close_status;
struct termio t;
char *device = "/dev/devicefile";
char query[10];
char recvbuf[10];

sprintf(query, "%c%s%c\n", STX, QUERY, ETX);
/* need to encapsulate QUERY between the STX and ETX, to be recognize
by the robot */

if((fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY))==-1)
fprintf(stderr, "Open RS232 Fail%d!\n", fd);

if((io_status = ioctl(fd, TCGETA, &t))==-1)
fprintf(stderr, "IOCTL TCGETA Fail!\n");

t.c_cflag=B9600|CS8|HUPCL|CREAD;

if((io_status = ioctl(fd, TCSETA, &t))==-1)
fprintf(stderr, "IOCTL TCSETA Fail!%d\n", io_status);

if((write_status = write(fd, &query, 5))==-1)
fprintf(stderr, "Write RS232 Fail!%d\n", write_status);

/* let say the query returns 2 bytes, YY */
/* actual data return from robot and received will be "2YY3" but ascii
character 2 and 3 is not printable(is this the problem?) therefore it
will be YY, as STX, "YY", ETX */

if((read_status = read(fd, &recvbuf, 5))==-1)
fprintf(stderr, "read RS232 Fail!%d\n", read_status);

printf("%s", recvbuf); /*returns nothing */

/* read_status received but is 0*/

close(fd);

}
/* End of code */

Is there any settings that are capable of filter away the STX and ETX
during read? or it that needed?

thanks
linda
Reply With Quote