Sign in to follow this  
Followers 0
newnet82

FX2N and linux box

15 posts in this topic

Hi, I need to use a linux box with a perl programm (or a C programm) in order to communicate with a FX2N. When I send the "windows meldde welcome message" (ie, in hexa, AA, B0, AA, B0, AA, B0, 05, 05, 05) the FX2N never answer the response it done with the "widows meldde" (ie, in hexa, 43, 35, 45, 03, 46, 39). I suppose I have a mistake in the serial port configuration. I tried to do this with : baud / bits / parity / stop 9600 / 7 / O / 1 / 9600 / 7 / E / 1 / 9600 / 7 / O / 2 / 9600 / 7 / E / 2 / 9600 / 8 / O / 1 / 9600 / 8 / E / 1 / 9600 / 8 / O / 2 / 9600 / 8 / E / 2 / Thx for your help

Share this post


Link to post
Share on other sites
Hi, 9600/ e/ 7/ 1 or 19200/ e/ 7/ 1 should work. You mentioned MelDDE but you are using linux. Is this supported? (DDE on linux???) As far as I can remember, the FX2N comms start with an enq and the plc sends a ack. Then you can send the message you want. Example (all in hex) Read request PC: 05 PLC: 06 PC: 02 30 30 30 30 3A 30 32 03 36 36 PLC: 02 XX XX XX XX 03 ZZ ZZ XX = data in hex ZZ = LRC in hex Anyway, hope this helps you onto the right track. Edited by waynes

Share this post


Link to post
Share on other sites
Of course no. Meldde is not running under linux. I just tried your sample by writing the C-programm following : --------------------------- BEGIN OF THE C CODE ------------------------------------------ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <termios.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main (int argv, char *argc[]) { int fd; char header[250]; char *line; int c; struct termios term_o; if ((fd = open(argc[1], O_RDWR | O_NOCTTY | O_NONBLOCK )) > 0) { printf("open serial port %s\n", argc[1]); } else { printf("Cannot open serial Port %s\n", argc[1]); return 0; } fcntl(fd, F_SETFL, FNDELAY); /*get current term_o for the port*/ tcgetattr(fd, &term_o); /*set baud rates to 9600*/ cfsetispeed(&term_o, B9600); cfsetospeed(&term_o, B9600); term_o.c_cflag = CLOCAL | CREAD | PARENB | CS7 ; /* this will set new line as carriage return*/ term_o.c_oflag |=ONLCR; printf("c_iflag = %d\n", term_o.c_iflag); printf("c_oflag = %d\n", term_o.c_oflag); printf("c_cflag = %d\n", term_o.c_cflag); printf("c_lflag = %d\n", term_o.c_lflag); tcsetattr(fd, TCSANOW, &term_o); line = (char *) malloc(255 * sizeof(char)); header[0] = 0x05; header[1] = '\0'; int i = 0; printf("line to write -> "); while (i < strlen(header)) { printf("%x, ", header); i++; } printf("\n"); c = write(fd, header, strlen(header)); sleep(1); read(fd, line, 255); i = 0; printf("line read <- "); while (i < strlen(line)) { printf("%x, ", line); i++; } printf("\n"); //02 30 30 30 30 3A 30 32 03 36 36 header[0] = 0x02; header[1] = 0x30; header[2] = 0x30; header[3] = 0x30; header[4] = 0x30; header[5] = 0x3A; header[6] = 0x30; header[7] = 0x32; header[8] = 0x03; header[9] = 0x36; header[10] = 0x36; header[11] = '\0'; i = 0; printf("line to write -> "); while (i < strlen(header)) { printf("%x, ", header); i++; } printf("\n"); c = write(fd, header, strlen(header)); sleep(1); while (1) { if ((i = read(fd, line, 255)) > 0) { printf("Nb Byte read = %d\n", i); i = 0; printf("<- "); while (i < strlen(line)) { printf("%x, ", line); i++; } printf("\n"); } } close(fd); } --------------------------- END OF THE C CODE ------------------------------------------ all what I have is : open serial port /dev/ttyS1 c_iflag = 0 c_oflag = 4 c_cflag = 2464 c_lflag = 0 line to write -> 5, line read <- 6, line to write -> 2, 30, 30, 30, 30, 3a, 30, 32, 3, 36, 36, Nb Byte read = 1 <- 15, And that's all. Why the FX2N sent a NACK ? Where is the error ?

Share this post


Link to post
Share on other sites
OK, did you get 06 (hex) back after you sent the 05 (hex)? It seems so. This means that the PLC is ready to receive requests. Now, i thing that the LRC bytes are wrong. I will look for some docs on this. LRC generation static unsigned char LRC(auchMsg, usDataLen) unsigned char *auchMsg ; /* message to calculate */ unsigned short usDataLen ; /* LRC upon quantity of */ /* bytes in message */ { unsigned char uchLRC = 0 ; /* LRC char initialized */ while (usDataLen--) /* pass through message */ uchLRC += *auchMsg++ ; /* buffer add buffer byte*/ /* without carry */ return ((unsigned char)(-((char_uchLRC))) ; /* return twos complemen */ }

Share this post


Link to post
Share on other sites
Yes I got a 06 (hex) when I send a 05 (hex). But after ... Tx for your help

Share this post


Link to post
Share on other sites
Check this website to calculate LRC/ CRC messages http://www.automatas.org/modbus/crc7.html Also, here is the MrPLC donwload link http://forums.mrplc.com/index.php?autocom=downloads&showfile=403 for the FX comms. It covers PC to PLC as well. Good luck!! Let us know if you need help after this..... Regards, WayneS Edited by waynes

Share this post


Link to post
Share on other sites
In the "user's manual" they give a request to read the type of a station. In hex : 05, 30, 46, 46, 46, 50, 43, 30, 43, 35. I adapted it for my case (I think ;)). So the request becomes : 05, 30, 30, 30, 30, 50, 43, 30, .., .. Regarding the user's manual the http://www.automatas.org/modbus/crc7.html is wrong. For me, it never gives the good LRC. neverthelass I tried the request with this LRC function and the answer was : 06, 15, 15 I used the following code : char *CRC(char *msg) { int sum; int i; char *result; // I do the sum in decimal sum = 0; i = 0; while(msg) { sum += msg[i++]; } result = (char *)malloc(5 * sizeof(char)); // I translate decimal to hexadecimal sprintf(result, "%x\0", sum); // I need only the 2 last characters while(strlen(result) > 2) { result++; } return(result); } Regarding the "user's manual", I always got the good CRC (the same as their). So I tried the request 05, 30, 30, 30, 30, 50, 43, 30, 38, 33 And the answer is again : 06, 15, 15. Is the error because I use the protocol format 1 ? Any ideas ? BR

Share this post


Link to post
Share on other sites
There is a company Interact in Holland which has created Linux Drivers for all Mitsubishi PLC's. They will probably give you the driver if you ask them for it. If you like I can give you the contact information

Share this post


Link to post
Share on other sites
Of course Gambit. I'll be happy ... My e-mail id blvq@net6d.com

Share this post


Link to post
Share on other sites
newnet, are you communicating directly to the front port on the FX2N? The RS422 port?

Share this post


Link to post
Share on other sites
Waynes, No, I am communicating to the FX2N via a FX2N-232-BD "extension port"

Share this post


Link to post
Share on other sites
Thanks Waynes, your question help me (FX2N-232-BD). after a google search I found a windows serial analyzer (PortmonNt). With it and meldde I can see requests. My linux box can sent request and read answer ! For example, when I want to see the variable D0504 (D0504 = -1) the linux sent : 02, 45, 30, 30, 34, 33, 46, 30, 30, 32, 03, 45, 37 (in hex) the PLC answer's is : 02, 46, 46, 46, 46, 03, 31, 42 (in hex) ie : FFFF => -1 GREAT !!!!!! Now I have an other problem. I don't understand all my query. Can you help me ? In my example the following is what I think understand 02 => STX 45, 30 => station number ???? 30, 34 => PC number ???? 33, 46, 30, 30, 32 => 3F002 ?????????????? D0504 ???????? 03 => ETX 45, 37 => check sum

Share this post


Link to post
Share on other sites
newnet, did you download & read the manual? Check page 7-6. Ok, you are using on demand example here (chapter 8.10) Note: Only for 1:1 situations. Also, not a lot of data can be sent/ received here. So, you cannot read all the data words. Therefore, you will be polling the PLC with different commands to read more data. I have another SCADA program and it uses this type of polling. I am going to investigate the datascope a bit. If you want, download the demo (works for an hour) copy from http://www.adroit.co.za and checkout the datascope utility. I'll be back.

Share this post


Link to post
Share on other sites
Yes I read, again and again, the last version of the manual. In a first time I work in 1:1 situation (it's enough for me for the moment :)) Now, after some tests, I can read each data between D0000 and D7998. My algorithm is : take the data to read (ie D500) extract the integer value (ie 500) use the formule : 16384 + 2 * the extract number (16384 + 2 * 500 = 17384) why 16384 ? For the moment I don't know. But it is (for D values) Convert this number in hex (17384 (dec) = 43E8 (hex)) take the ascii code for each digit (4 => 34; 3 => 33; E => 45; 8 => 38) So the request is : 02 45 30 30 34 33 45 38 30 32 03 45 45 with 02 : STX 45 30 30 : ??? station number + pc number ? It is not the header like they say in the manual page 8.19 30 30 is it the command the a read request ??? 34 33 45 38 is my D500 request 30 32 : ??? 03 : ETX 45 45 => check sum using my checksum algorithm that I give in a previous post And it is Fine I take the contents of the D500 variable. When I want to read the contents of the M500 I need to search again ... But it is not enough. Too much unknown parameters. 16384 ? 45 30 30 ? 30 32 ? .... D8000 and greatest ... If you have answers ... Thx

Share this post


Link to post
Share on other sites
OK, I understand a little more what's happen. First : number greatest than 7999 are special number. In my last post I tried to understand howto create the request for the D500 read. After extracted the 500 I need to convert it in binary 0001 1111 0100 I add a 0 at the end (why ???) 0011 1110 1000 I add a 0100 at the begining (why ???) 0100 0011 1110 1000 I convert it in hex 43E8 and I continue my algorithm. For the 30 32 I sippose it is the size of the data (02 bytes) So, now I can create with a little more understanding request But, it is not enough ... I have again lot of questions (the same as before PLUS new) ...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0