Click to See Complete Forum and Search --> : Rs232 programming


madhivadhana
11-03-2007, 02:13 AM
Hi,
I have to do Linux rs232 programming to connect a device. This device communicates through Hex. It takes commands as 8 bytes of Hex and sends 11 bytes of Hex as output.
the device settings,
9600, 7 bit, 1 start bit, 1 stop bit, Even parity.

I have written the below code for commnication. But there is no response.
Please, .. someone help me. I am very much struck in this.
*************************************************
struct termios newtio;
struct timeval tv;
int retval;

char buf[255];



FD_ZERO(&read_set);

tv.tv_sec = 5;
tv.tv_usec = 0;
fd=open("/dev/ttyAM1",O_RDWR | O_NOCTTY |O_NDELAY);
if(fd < 0)
{
fprintf(stderr, "Unable to open /dev/ttyAM1");

exit(1);
}
fcntl(fd,F_SETFL,FNDELAY);




cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
newtio.c_cflag |= CREAD;

newtio.c_cflag |= CLOCAL;
newtio.c_cflag |= PARENB;

newtio.c_cflag &= ~PARODD;
newtio.c_cflag |= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS7;

newtio.c_cflag &= ~CRTSCTS;
newtio.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR);
newtio.c_lflag &= ~(ICANON | ECHO | ISIG );
newtio.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONLRET);



tcsetattr(fd,TCSANOW,&newtio);


FD_SET(fd,&read_set);
write(fd,&cmd,sizeof(cmd));


retval = select(fd+1,&read_set,NULL,NULL,&tv);
if(retval)
{
printf("Data is available");
res=read(fd,buf,255);
printf("\n");
printf("%d\n",res);
for(i=0;i<res;i++)
{
printf("%x",buf[i]);
}
}
else
printf("No data within five seconds");


printf("retval=%d",retval);
close(fd);
************************************************** ********
Regards,
Madhi.

trilarian
11-14-2007, 12:05 PM
It's been some time since I've done programming for a COM port, but what I do remember is that you have to send a configuration byte(s) to initialize communication. Usually this consist of settings for speed, parity, etc. Have you done this? Sorry, don't have the time to read through your code while at work, but here is an example I followed the last time I needed to use a serial port. Code is in reduced C for microcontrollers.


/*
* Serial port driver for 16Cxx chips
* using software delays.
*
* Copyright (C)1996 HI-TECH Software.
* Freely distubutable.
*/

/*
* Tunable parameters
*/

#include <pic.h>
#include <conio.h>

/* Transmit and Receive port bits */

//static bit TxData @ (unsigned)&PORTA*8+3; /* bit3 in port A */
//static bit RxData @ (unsigned)&PORTA*8+2; /* bit2 in port A */
static bit TxData @ (unsigned)&PORTB*8+1; /* bit1 in port B */
static bit RxData @ (unsigned)&PORTB*8+0; /* bit0 in port B */


//#define INIT_PORT TRISA = 7 /* set up I/O direction */
#define INIT_PORT TRISB &= 0xFB /* set up I/O direction */

/* Xtal frequency */

//#define XTAL 4000000
#define XTAL 10000000

/* Baud rate */

//#define BRATE 9600
#define BRATE 19200

/* Don't change anything else */

#define DLY 3 /* cycles per null loop */
#define TX_OHEAD 13 /* overhead cycles per loop */
#define RX_OHEAD 12 /* receiver overhead per loop */

#define DELAY(ohead) (((XTAL/4/BRATE)-(ohead))/DLY)

void
putch(char c)
{
unsigned char dly, bitno;

bitno = 11;

INIT_PORT;
TxData = 0; /* start bit */
bitno = 12;
do {
dly = DELAY(TX_OHEAD); /* wait one bit time */
do
/* nix */ ;
while(--dly);
if(c & 1)
TxData = 1;
if(!(c & 1))
TxData = 0;
c = (c >> 1) | 0x80;
} while(--bitno);
TxData=1; //--- Ensure Pin Is HIGH when Finished!!!
}

char
getch(void)
{
unsigned char c, bitno, dly;

for(;;) {
while(RxData)
continue; /* wait for start bit */
dly = DELAY(3)/2;
do
/* nix */;
while(--dly);
if(RxData)
continue; /* twas just noise */
bitno = 8;
c = 0;
do {
dly = DELAY(RX_OHEAD);
do
/* nix */;
while(--dly);
c = (c >> 1) | (RxData << 7);
} while(--bitno);
return c;
}
}

//--------- Added by Mike pearce ---------------
void puts(const char *str)
{
while(*str !=0)
{
putch(*str);
str++;
}
}