P
Hi,
I have written a code in C by which i should be able to Communicate with PLC(Modicon)
But the Reply(message) from the PLC is not correct. Kindly Help me to slove this Problem.
Protocol: MODBUS
Development Env. : TC++
Comm. Mode : ASCII
Pankaj.
[email protected]
===============================================
Code
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#define PORT1 0x3F8
#define DATA_BUFFER 25
typedef unsigned int int16;
typedef unsigned short u16;
typedef unsigned char byte_t;
void main(void)
{
int loop=0;
int c;
int index=0;
int ch;
byte_t outbuf[20];
outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */
/* PORT 1 - Communication Settings */
outportb(PORT1 + 3 , 0x80); /* SET DLAB ON */
outportb(PORT1 + 0 , 0x0c); /* 9600 Divisor Latch Low Byte */
outportb(PORT1 + 1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */
outportb(PORT1 + 3 , 0x1b); /* 0x1b 8 Bits, No Parity, 1 Stop Bit */
outportb(PORT1 + 2 , 0xC7); /* FIFO Control Register */
outportb(PORT1 + 4 , 0x08); /* Turn on DTR, RTS, and OUT2 */
clrscr();
printf("\nSample PLC Comm's Program. Press ESC to quit \n");
/* Read A Word From PLC Port */
index=0;
outbuf[index++]=0x3a;
outbuf[index++]=0x30;
outbuf[index++]=0x34;
outbuf[index++]=0x30;
outbuf[index++]=0x38;
outbuf[index++]=0x30;
outbuf[index++]=0x30;
outbuf[index++]=0x30;
outbuf[index++]=0x36;
outbuf[index++]=0x31;
outbuf[index++]=0x36;
outbuf[index++]=0x32;
outbuf[index++]=0x33;
outbuf[index++]=0x34;
outbuf[index++]=0x0d;
outbuf[index++]=0x0a;
for(loop=0;loop<index;loop++) {
delay(50);
printf("%x ",outbuf[loop]);
outportb(PORT1,outbuf[loop]);}
printf(" \n");
do { c = inportb(PORT1 + 5); /* Check to see if char has been */
/* received. */
if (c & 1) {ch = inportb(PORT1); /* If so, then get Char */
printf(" %x",ch);} /* Print Char to Screen */
delay(20);
if (kbhit()){ch = getch(); /* If key pressed, get Char */
outportb(PORT1, ch);} /* Send Char to Serial Port */
} while (ch !=27); /* Quit when ESC (ASC 27) is pressed */
}
===============================================
OutPut::::
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
it goes on .......
I have written a code in C by which i should be able to Communicate with PLC(Modicon)
But the Reply(message) from the PLC is not correct. Kindly Help me to slove this Problem.
Protocol: MODBUS
Development Env. : TC++
Comm. Mode : ASCII
Pankaj.
[email protected]
===============================================
Code
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#define PORT1 0x3F8
#define DATA_BUFFER 25
typedef unsigned int int16;
typedef unsigned short u16;
typedef unsigned char byte_t;
void main(void)
{
int loop=0;
int c;
int index=0;
int ch;
byte_t outbuf[20];
outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */
/* PORT 1 - Communication Settings */
outportb(PORT1 + 3 , 0x80); /* SET DLAB ON */
outportb(PORT1 + 0 , 0x0c); /* 9600 Divisor Latch Low Byte */
outportb(PORT1 + 1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */
outportb(PORT1 + 3 , 0x1b); /* 0x1b 8 Bits, No Parity, 1 Stop Bit */
outportb(PORT1 + 2 , 0xC7); /* FIFO Control Register */
outportb(PORT1 + 4 , 0x08); /* Turn on DTR, RTS, and OUT2 */
clrscr();
printf("\nSample PLC Comm's Program. Press ESC to quit \n");
/* Read A Word From PLC Port */
index=0;
outbuf[index++]=0x3a;
outbuf[index++]=0x30;
outbuf[index++]=0x34;
outbuf[index++]=0x30;
outbuf[index++]=0x38;
outbuf[index++]=0x30;
outbuf[index++]=0x30;
outbuf[index++]=0x30;
outbuf[index++]=0x36;
outbuf[index++]=0x31;
outbuf[index++]=0x36;
outbuf[index++]=0x32;
outbuf[index++]=0x33;
outbuf[index++]=0x34;
outbuf[index++]=0x0d;
outbuf[index++]=0x0a;
for(loop=0;loop<index;loop++) {
delay(50);
printf("%x ",outbuf[loop]);
outportb(PORT1,outbuf[loop]);}
printf(" \n");
do { c = inportb(PORT1 + 5); /* Check to see if char has been */
/* received. */
if (c & 1) {ch = inportb(PORT1); /* If so, then get Char */
printf(" %x",ch);} /* Print Char to Screen */
delay(20);
if (kbhit()){ch = getch(); /* If key pressed, get Char */
outportb(PORT1, ch);} /* Send Char to Serial Port */
} while (ch !=27); /* Quit when ESC (ASC 27) is pressed */
}
===============================================
OutPut::::
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
10 5 1 10 5 2 10 5 3 10 5 4 10 5 5 10 5 1 10 5 2 10 5 3 10 5 4 10 5 5
it goes on .......