I am working on the TS-7260 for the first time.I am trying out this
program which does read/write operations on the RS-485 port provided.
The problem with this program is that it is able to write properly
onto the port but unable to read from the port.
Please help me out.....
Enclosed is the C code.....
Please reply in case you need more details.
Thanking you.
//Program for read/write operations on RS-485
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <assert.h>
#include <signal.h>
#include <time.h>
#include "ts_sbc.h"
#include <sys/ioctl.h>
//#include "ioctls.h"
#define port485 "/dev/ttyAM1" // RS485 dev name
#define TIOC_SBCC485 _IOW('T',0x70,int) /*TS RTS/485 mode Clear*/
#define TIOC_SBCS485 _IOW('T',0x71,int) /*TS RTS/485 mode Set */
typedef unsigned char uchar;
typedef volatile unsigned char vuchar;
typedef uchar bool;
#define TRUE 1
#define FALSE 0
int com;
struct termios options;
int openPort(char* port);
bool has485();
int init();
//-------------------------------------
bool has485(){
uchar ccc;
vuchar *chip,*ch;
int fd;
fd = open( "/dev/mem", O_RDWR );
chip = (vuchar*)mmap( 0, getpagesize(),
PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0x22c00000 );
ccc = *chip;
printf("mr=%04x\n",ccc);
chip = (vuchar*)mmap( 0, getpagesize(),
PROT_READ|PROT_WRITE,MAP_SHARED, fd,0x22400000 );
ccc = *chip;
printf("mr2 = %04x\n",ccc);
*chip = 0x04;
// ch = (vuchar*)mmap( 0, getpagesize(),
PROT_READ|PROT_WRITE,MAP_SHARED, fd,0x23000000 );
// *ch= 0x01;
close( fd );
return (ccc & 0x02) ? TRUE:FALSE;
}
//------------------------------------------------
int openPort(char* port){
int mcr;
int err;
if((com=open(port, O_RDWR | O_NOCTTY | O_NDELAY))!=-1){
tcgetattr(com, &options);
options.c_cflag=(CS8 | CLOCAL | CREAD);
options.c_iflag=(IGNBRK | IGNPAR );
// options.c_oflag=OPOST;
// options.c_lflag=0;
options.c_lflag = ICANON;
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(com, TCSANOW, &options);
tcflush( com, TCIFLUSH);
fcntl(com, F_SETFL, FNDELAY);
printf("Setting mode \n");
mcr=AUTO485HD;
err=ioctl(com, TIOC_SBCS485, &mcr);
if(err)
printf("err = %d\n", err);
printf("Mode OK\n");
return 0;
}
printf("OPEN FAILED!\n");
return 1;
}
//-------------------------------------
int init(){
if(has485()) return openPort(port485);
else return 1;
}
//-------------------------------------
int main(){
int n, rd;
char buf[512], dbuf[30]="ts7260\n";
init();
//03/05/00/01/ff/00/dc/18 on
//03/05/00/01/00/00/9d/e8 off
buf[0]=0x03; //slave address 3
buf[1]=0x05; //force single coil
buf[2]=0x00; //coil #0001
buf[3]=0x01;
buf[4]=0xff; //turn it ON (ff00)
buf[5]=0x00;
buf[6]=0xdc; //crc hi
buf[7]=0x18; //crc lo
for(n=0; n<4; n++){
printf("Writing %d...\n",n);
write( com, dbuf, 7);
sleep(1);
}
sleep(1);
memset(buf,0,512);
printf ("while...\n");
while(1){
rd = read(com, buf, 10);
// buf[rd]=0;
if(rd>0) printf(":%s",buf);
}
exit(0);
}
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|