Dear Wiff,
You don't say whether you are using full or half duplex. As written, the code looks to me like it for full-duplex.
I suggest you get an RS-232 to RS-485 dongle and use a PC to talk to your device, to make sure you device works the way you think it does, that all the wires are hooked up, communications parameters are correct, etc. You could then get a scope picture of what working communication sequence looks like and then compare with happens when you do the same thing on your ARM system.
Some devices require very limited time between bytes. I'm working with a pump controller that requires there to be almost no dead time between certain bytes it receives. You might for such restrictions with your device.
Finally, you might try running your test program with strace, to see if that sheds any light on what is going on.
Good luck,
Rob
On 6/30/07,
whiffwaffle <> wrote:
I am having a heck of a time getting a simple Modbus command to work
on the 7200.
I can see the receive light flashing on the device, it just doesn't
accept the command. I get no response at all.
The code I'm using is a stripped down version of the readmeter demo
with some other changes I added while experimenting.
I'm using Modbus RTU devices (8-bit binary commands, not ASCII).
The device is set for 9600, N,8,1.
Here is the code I'm using:
#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 "ioctls.h"
#define port485 "/dev/ttyAM1"
#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;
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=%d\n",ccc);
chip = (vuchar*)mmap( 0, getpagesize(), PROT_READ|PROT_WRITE,
MAP_SHARED, fd,
0x22400000 );
ccc = *chip;
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 | IGNCR);
options.c_oflag=OPOST;
options.c_lflag=0;
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(com, TCSANOW, &options);
tcflush( com, TCIFLUSH);
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,i;
uchar buf[512];
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<10; n++){
printf("\nWriting %d...\n",n);
write( com, buf, 8);
sleep(1);
}
sleep(1);
memset(buf,0,512);
read(com, buf, 512);
for(n=0;n<20;n++){
printf("%d\n",buf[n]);
}
exit(0);
}
Any help would be greatly appreciated.
-- Robert Bedichek cell: +1-650-245-0059 http://bedichekconsulting.com/
__._,_.___
SPONSORED LINKS
__,_._,___
|