www.pudn.com > MOXA--DOS.rar > TC-EX1.C


/* 
 *      TC-EX1.C - written by Turbo C V2.0 
 *      Syntex example for MOXA API-232 
 */ 
 
#include        "head-c.h" 
 
int     port_no[100]; 
char    buffer[1000]; 
int     buf_ptr,p; 
 
void    main() 
{ 
        int             i, c, port, n; 
        int             total_port; 
        char            data[100]; 
 
        clrscr(); 
		printf("Reset the serial ports....\n"); 
 
		if ( sio_reset() <= 0 ) { 
				printf("No Driver Found !\n"); 
				printf("Please run driver first.\n"); 
				exit(0); 
		} 
		total_port = sio_getports(port_no,99); 
        printf("Which port to test: 1(1st port) 2(2nd port) 3(3rd port)...? "); 
        scanf("%d", &port); port -= 1; 
        p = port_no[port]; 
 
/* Before using, each port should be OPENed first */ 
        printf("Setting all ports 9600, N, 8, 1....\n"); 
        for (port = 0; port < total_port; port++) { 
                n = port_no[port]; 
                if (sio_ioctl(n,B9600,BIT_8| P_NONE| STOP_1) != 0){ 
                        printf("Port #%d IOCTL error !\n", n); 
                        exit(0); 
                } 
                sio_lctrl(n, 3);        /* set DTR RTS on      */ 
                sio_flowctrl(n,0);      /* set NO flow control */ 
                printf("Open Port #%d\n\r", port); 
                sio_open(n); 
        } 
 
        printf("sio_getch() test in progress...Any key to next\n"); 
        while ( kbhit() == 0) { 
                if( (i = sio_getch(p)) >= 0 ) 
                printf("%c", i ); 
        } 
        getch(); 
        printf("\nline data input in progress...Any key to next\n"); 
        while ( kbhit() == 0) { 
                i = sio_linput(p, data, sizeof(data),0x0d); 
                if ( i > 0 ) { 
                        data[i] = 0x00; 
                        printf("Line data read : %s\n", data); 
                } 
        } 
        getch(); 
 
        sio_timeout(18 * 5);    /* set timeout value = 5 sec */ 
        printf("read data within 5 sec.\n"); 
        i = sio_linput_t(p, data, 10, 0x0d) ; 
        switch( i ) { 
                case -1 : printf( "bad port !\n"); 
                          break; 
                case -2 : printf( "time out !\n"); 
                          break; 
                case -3 : printf( "bad function call !\n"); 
                          break; 
                default : if (i == 0) 
                            printf( "read 0 byte !\n"); 
                          else { 
                            data[i] = 0x00; 
                            printf("%d bytes read in 5 sec: %s\n",i,data); 
                          } 
                          break; 
        } 
        printf("sio_putch A...Any key to next.\n"); 
        while( kbhit() == 0 ) { 
                sio_putch(p, 'A'); 
        } 
        getch(); 
 
        printf("sio_putb ABCDEFGHIJ...\n"); 
        for( i = 0; i < 100; i++ ) { 
                sio_putb(p, "ABCDEFGHIJ", 10 ); 
        } 
 
        printf("Read data from all ports, any key to next demo....\n"); 
        while (kbhit() == 0) 
                for (port = 0; port < total_port; port++) { 
                        n = port_no[port]; 
                        if ((i = sio_read(n, data, 100)) < 0) { 
                                printf("Port #%2d READ error !\n", port); 
                                exit(0); 
                        } 
                        else if (i) { 
                                data[i] = 0; 
                                printf("Port #%2d  data[%s]\n",port,data); 
                        } 
                } 
        getch(); 
 
        clrscr(); 
        printf("Read queue status from ports, any key to next demo....\n"); 
        while (kbhit() == 0) { 
                gotoxy(1, 3); 
                for (port = 0; port < total_port; port++) { 
                   n = port_no[port]; 
                   printf("Port #%2d  input queue:%04d     output queue:%04d\n", 
                                port, sio_iqueue(n), sio_oqueue(n)); 
                } 
        } 
        if ( getch() == 0x1b ) exit(0); 
 
        clrscr(); 
        printf("Read line status from ports, any key to next demo....\n"); 
        while (kbhit() == 0) { 
                gotoxy(1, 3); 
                for (port = 0; port < total_port; port++) { 
                     n = port_no[port]; 
                     printf("Port #%2d  line status:%04x\n",port,sio_lstatus(n)); 
                } 
        } 
        if ( getch() == 0x1b ) exit(0); 
 
        printf("\n\nFlush all ports input/output queue....\n"); 
        for (port = 0; port < total_port; port++) { 
                n = port_no[port]; 
                sio_flush(n, 2); 
                sio_close(n); 
        } 
        sio_reset(); 
}