/* ============================================================================ Name : emctest.c Author : jopka1@gmail.com Version : Copyright : Just test =) Description : Testing emcrsh software and see if emcrsh server lock up or not, as i describe here http://www.linuxcnc.org/component/option,com_kunena/Itemid,20/func,view/catid,38/id,13940/lang,english/ ============================================================================ */ #include #include #include #include #include #include //#include #include #define remote_ip "1.1.1.73" #define remote_port 5007 FILE *f; char s[10240],s1[10240],s2[10240],ss[10240]; //byte outbuf[2048]; //byte inbuf[2048]; char outtext[2048]; char intext[2048]; unsigned short device_port; char *device_ipaddr; int sock; struct sockaddr_in device_sock; int connected; int i,ii,iii; int yyyyy; void send_rcv_text (void) { // assume same size packet always send/rcv // char s[1024]; int rcvd; int qty; void rcv_ (void) { rcvd = 0; if ((rcvd = recv(sock, intext, sizeof(intext), 0)) <= 0) printf("recv() failed or connection closed prematurely\n"); intext[rcvd]='\0'; } if (connected) { // emcrsh telnet mode: // send: \n string end not work, \r\n or \r - work. // rcv: \r\n at the end. qty=strlen(outtext); printf("%d byte(s) send: *%s*\n", qty, outtext); outtext[qty]='\r'; qty=qty+1; outtext[qty]='\0'; if (send(sock, outtext, qty, 0) != qty) printf("send() sent a different number of bytes than expected\n"); // answer can be one or two lines (packets). // if first line not has '\n\ at end, then it is echo and // we need to read one (we hope only one) line. rcv_(); // printf("%d byte(s) receive: *%s*\n",rcvd, intext); if ((rcvd>1)&&(intext[rcvd-1]!='\n')) { if (rcvd>1) { intext[rcvd-1]='\0'; } // strip \r at the end printf("%d byte(s) receive 1st line: *%s*\n",rcvd, intext); if (yyyyy==0) { // ugly workaround rcv_(); if (rcvd>2) { intext[rcvd-2]='\0'; } // strip \r\n at the end printf("%d byte(s) receive 2nd line: #%s#\n",rcvd, intext); } } else { if (rcvd>2) { intext[rcvd-2]='\0'; } // strip \r\n at the end printf("%d byte(s) receive: *%s*\n",rcvd, intext); } } } void emc_bye(void) { if (connected) { printf("Emcrsh quit.\n"); strcpy(outtext,"set machine off"); send_rcv_text(); strcpy(outtext,"set estop on"); send_rcv_text(); printf("Emcrsh quit done.\n"); } } void send_gcode(char* s) { if (connected) { sprintf(outtext,"set mdi %s", s); send_rcv_text(); } } void emc_testgcode(void) { if (connected) { printf("Test G-code:\n"); // strcpy(outtext,"set update auto"); // send_rcv_text(); strcpy(outtext,"set mode mdi"); send_rcv_text(); strcpy(s, "G90 G40 G17"); send_gcode(s); strcpy(s, "S500 M3"); send_gcode(s); strcpy(s, "G0 X2.54 Y26.15"); send_gcode(s); strcpy(s, "Z1.0"); send_gcode(s); strcpy(s, "G1 Z-10.0 F200"); send_gcode(s); strcpy(s, "X5.19 Y 2.0 A720"); send_gcode(s); strcpy(s, "G0 X0 Y0 Z0 A0"); send_gcode(s); // strcpy(outtext,"set update none"); // send_rcv_text(); } } void connect_emcrsh(void) { printf("Connect\n"); connected=0; //ethernet init // timer_set(200000); // set timeout for socket operations, // because it is dependent and interfere with SIGALRM subsystem. // timer_empty(); //socket operations no need timer callback device_ipaddr = remote_ip; device_port = remote_port; if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) printf("socket() failed\n"); memset(&device_sock, 0, sizeof(device_sock)); /* Zero out structure */ device_sock.sin_family = AF_INET; /* Internet address family */ device_sock.sin_addr.s_addr = inet_addr(device_ipaddr); /* Server IP address */ device_sock.sin_port = htons(device_port); /* Server port */ if (connect(sock, (struct sockaddr *) &device_sock, sizeof(device_sock)) < 0) printf("connect() failed\n"); else connected=1; } void hello_emcrsh(void) { if (connected) { strcpy(outtext,"hello EMC x 1.0"); send_rcv_text(); strcpy(outtext,"set verbose on"); send_rcv_text(); strcpy(outtext,"set enable EMCTOO"); send_rcv_text(); strcpy(outtext,"set set_wait none"); send_rcv_text(); } } void disconnect_emcrsh(void) { if (connected) { printf("Disconnect\n"); connected=0; close(sock); } } void emcrsh_init(void) { yyyyy=0; connect_emcrsh(); if (connected) { printf("Emcrsh init.\n"); hello_emcrsh(); // not disable echo, because some commands then never send answer at all. // strcpy(outtext,"set echo off"); // send_rcv_text(); // strcpy(outtext,"set update none"); // send_rcv_text(); //strcpy(outtext,"set update auto"); //send_rcv_text(); /* yyyyy=1; // ugly workaround against bug: lock-up on "set machine on" :-( strcpy(outtext,"set machine on"); send_rcv_text(); disconnect_emcrsh(); usleep(10000); yyyyy=0; connect_emcrsh(); hello_emcrsh(); */ strcpy(outtext,"set estop off"); send_rcv_text(); usleep(200000); strcpy(outtext,"set machine on"); // it is NOT work before 'set estop off' due to unknown reason. send_rcv_text(); usleep(200000); // strcpy(outtext,"set mode manual"); // send_rcv_text(); // strcpy(outtext,"set update auto"); // send_rcv_text(); /* yyyyy=1; // ugly workaround against bug: lock-up on "set machine on" :-( strcpy(outtext,"set mode manual"); send_rcv_text(); disconnect_emcrsh(); usleep(100000); yyyyy=0; connect_emcrsh(); hello_emcrsh(); */ strcpy(outtext,"set home 0"); send_rcv_text(); usleep(200000); strcpy(outtext,"set home 1"); send_rcv_text(); usleep(200000); strcpy(outtext,"set home 2"); send_rcv_text(); usleep(200000); // 4th 'set home' not work, regardless of axis order, if we remove four 'usleep's strcpy(outtext,"set home 3"); send_rcv_text(); usleep(200000); /* yyyyy=1; // ugly workaround against bug: lock-up on "set machine on" :-( strcpy(outtext,"set mode manual"); send_rcv_text(); disconnect_emcrsh(); usleep(100000); yyyyy=0; connect_emcrsh(); hello_emcrsh(); */ strcpy(outtext,"set mode mdi"); send_rcv_text(); printf("Emcrsh init done.\n"); } else { printf("Not connected.\n"); } } int main( int argc, char *argv[] ) { printf("Hi!\n"); yyyyy=0; connected=0; printf("Emc2 Emcrsh test %s:%d\n", remote_ip, remote_port); emcrsh_init(); emc_testgcode(); // emc_bye(); // disconnect_emcrsh(); printf("Bye!\n"); return 0; }