⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pc23c.c

📁 激光加工控制系统 可以读入plt文件
💻 C
字号:
/* *********************************************************************
		     PC23 RESET, READ, WRITE DRIVERS

   The following routines will compile under Borland or Microsoft C.
   They are intended to demonstrate basic communication routines in C.
********************************************************************** */

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define	FAIL  	   0X20
#define BIT2MASK   0X04
#define READY      0X16
#define	CB	   0X60
#define IDB_M	   0X10
#define CHAR_READY 0X70
#define ODB	   0X8
#define ACK	   0XE0
#define ALDONE     0X02
#define BADADDR    0XFF

#define HALT          (CB | BIT2MASK)
#define RESTART       0x40          /* byte to restart the pc21 */

char *pc23_readanswer ( char *s, unsigned int address );
char pc23_readch( unsigned int rw_addr);
void initialize (void);
void writech (char);
char readch (void);
void writecmd (char *s);
void readanswer (char *s);
trans_i_to_s(int x,char *buf);
get_a_line(FILE *fpe);
float count_angle(float x0, float y0, float x1, float y1);

int axis, address ;		        /* normally 300 hex */
FILE *fpe;
FILE *ff;
char buf[200];
//int speed;

void main()
{
	char  *message,*answer;
	int ret,delay_time,i,flag;
	char filename[20];
	long lines;
	int ch,time_mode=0;
	char ch1;

	answer="";

	initialize();                  /* RESET THE PC23 */

	printf("Please Input The File Name To Send: ");
	scanf("%s",filename);
again:
	clrscr();
//	printf("Please Input The Speed To Run: ");
//	scanf("%d",&speed);
	printf("File Is Sending.\n");
	printf("Space To Pause.\n");
	printf("Press ESC To Abort!\n");
	if (filename[strlen(filename)-3] != '.')
	  strcat(filename,".23");
	fpe = fopen(filename,"rb");
	if (fpe == NULL){
	  printf("File Not Found!\n");
	  return;
	}
	ff = fopen("setup1.fil","rb");
	if (ff == NULL){
	  fclose(fpe);
	  printf("File Setup1.fil Not Found!\n");
	  return;
	}

	get_a_line(ff);
	i = 0;
	while ((buf[0] != 'U')||(buf[1] != 'p')||(buf[2] != 'd')){
	  i ++;
	  get_a_line(ff);
	  if (i > 500){
	    printf("Setup1.fil Error, No UpdateInterval Line!\n");
	    fclose(ff);
	    fclose(fpe);
	    return;
	  }
	}
	get_a_line(ff);
	delay_time = atoi(buf);
	fclose(ff);
/*
	message=" LD3 A1 V1 D2500 G123 1PR ";
	writecmd(message);


	message="A1 V1 D2500 G ";
	writecmd(message);
*/
//	strcpy(buf,"QSG1");
//	writecmd(buf);
	ret = get_a_line(fpe);
	lines = 0;
	while (ret == 0){
	  if (buf[0] != 0){
	    if ((lines % 100L) == 0){
	      printf("Line:  %ld\n",lines);
	    }
//	    if ((lines % 2) == 0)
	    delay(delay_time);
  //	    else if ((lines % 13) == 0)
//	      delay(1);
//	    else if ((lines % 197) == 0)
//	      delay(1);
//	    else if ((lines % 1193) == 0)
//	      delay(1);

//	    delay(4);

	    if ((buf[0] == '1')&&(buf[1] == 'Q')&&(buf[2] == '2')){
	      time_mode = 1;
	      delay(100);
	    }
	    if ((buf[0] == '1')&&(buf[1] == 'Q')&&(buf[2] == '0'))
	      time_mode = 0;
	    if (time_mode == 1){
	      ch1 = inp(769);
	      if ((ch1 & 0x80) == 0){
//		ch1 = inp(769);
//		delay(10);
	      }
	    }
	    writecmd(buf);
	    lines ++;
	  }
	  ret = get_a_line(fpe);
	  if (kbhit() != 0){
	    ch = getch();
	    if (ch == 27){
	      initialize();                  /* RESET THE PC23 */
	      ret = -1;
	    }
	    else if (ch == ' '){
	      printf("Pause, Press Space Bar To Continue!\n");
	      ch = 0;
	      while ((ch != ' ')&&(ch != 27))
		ch = getch();
	      if (ch == 27)
		ret = -1;
	    }
	  }
	}

	fclose(fpe);
	initialize();                  /* RESET THE PC23 */
	printf("If You Want To Continue, Press Enter!\n");
	ch = getch();
	if (ch == 13)
	  goto again;
}
/* *********************************************************************
   INITIALIZE: RESET THE PC23 BOARD.  ASSURING THAT THE BOARD IS
	       READY TO ACCEPT COMMANDS FROM THE USER PROGRAM.
************************************************************************* */
void initialize ()
{
	char i;
	int status_addr=0;
	unsigned char statbyte;
	address = 768;
	if ((address < 768) || ( address > 812))
		{
		printf ("\n\nInvalid address, check PC-23 dipswitches\n\n");
		exit(1);
		}
	status_addr=address+1;
	outp(status_addr,HALT);  /* initialize procedure */
	while (!((statbyte=inp(status_addr)) & FAIL) );
	if(statbyte==BADADDR)
		{
		printf ("\n\nInvalid address, check PC-23 dipswitches\n\n");
		exit(1);
		}
	outp(status_addr,RESTART);
	outp(status_addr,CB);
	while( ( (statbyte=inp(status_addr)) & READY) != READY );
}

/* **********************************************************************
   WRITECH: WRITES A SINGLE CHARACTER TO THE PC23. PC23 COMMANDS ARE
	    GENERATED BY SENDING MULTIPLE CHARACTERS TO IT.
************************************************************************* */
void writech ( char alpha )
{
	while (!(inp(address+1) & IDB_M));
	outp (address,alpha);
	outp (address+1, CHAR_READY);
	while (inp(address+1) & IDB_M);
	outp(address+1,CB);
	while (!(inp(address+1) & IDB_M));
	return;
}
/* ***********************************************************************
   READCH: READS ONE CHARACTER OF A PC23 RESPONSE TO A STATUS REQUEST.
	   RETURNS THE CHARACTER RESPONSE.
************************************************************************ */
char readch()
{
	char alpha=0;
	while (!(inp(address+1) & ODB));
	alpha = inp(address);
	outp (address+1,ACK);
	while ((inp(address+1) & ODB));
	outp (address+1,CB);
	return(alpha);
}
/* ***********************************************************************
   WRITECMD: WRITES A COMMAND STRING TO THE PC23.
************************************************************************* */
void writecmd(char *s)
{
	while (*s)
	     writech (*s++);
	return;
}
/* ***********************************************************************
   READANSWER: READS A COMPLETE PC23 STATUS REQUEST RESPONSE STRING.
************************************************************************* */
void readanswer (char *s)
{
	while ((*s++ = readch()) != 13);
	*s = '\0';
	return;
}

get_a_line(FILE *fpe)
{
  int i,ch,x,y;
  float ff;
  char buf1[8],buf2[8];

  ch = fgetc(fpe);
  if (ch == EOF)
    return -1;
  i = 0;
  while ((ch != 13)&&(ch != 10)&&(ch != EOF)){
    buf[i++] = ch;
    ch = fgetc(fpe);
  }
  buf[i] = ' ';
  buf[i+1] = 0;
  if (ch == EOF)
    return -1;
  fgetc(fpe);
  if (buf[0] == '\\')
    buf[0] = 0;
  else if (buf[0] == '='){
    for (i=0;i<strlen(buf);i++)
      buf[i] = buf[i+1];
  }
  else if (buf[0] == 'P'){
    x = atoi(&(buf[1]));
    i = 0;
    while (buf[i] != ' ')
      i ++;
    y = atoi(&(buf[i+1]));
    x = -x;
    y = -y;
    trans_i_to_s(x,buf1);
    trans_i_to_s(y,buf2);
    strcpy(buf,"SD");
    strcat(buf,buf1);
    strcat(buf,buf2);
    strcat(buf," ");
  }
  else if ((buf[0] == 'E')||(buf[0] == 'I')){
    buf[0] = 0;
  }
  else if (buf[0] == 'O'){
    y = atoi(&(buf[2]));
    x = atoi(&(buf[5]));
    buf[1] = x / 32 + '0';
    x = x % 32;
    buf[2] = x / 16 + '0';
    x = x % 16;
    buf[3] = x / 8 + '0';
    x = x % 8;
    buf[4] = x / 4 + '0';
    x = x % 4;
    buf[5] = x / 2 + '0';
    buf[6] = x % 2 + '0';
    buf[7] = ' ';
    buf[8] = 0;
    writecmd(buf);
    delay(100);
    x = y;
    if (x != 0){
    buf[0] = 'O';
    buf[1] = x / 32 + '0';
    x = x % 32;
    buf[2] = x / 16 + '0';
    x = x % 16;
    buf[3] = x / 8 + '0';
    x = x % 8;
    buf[4] = x / 4 + '0';
    x = x % 4;
    buf[5] = x / 2 + '0';
    buf[6] = x % 2 + '0';
    x = x % 32;
    buf[7] = ' ';
    buf[8] = 0;
    }
  }
  else if (buf[0] == 'W'){
    ff = atof(&(buf[1]));
    delay(ff);
    buf[0] = 0;
  }
  if ((buf[0] == 'M')&&(buf[1] == '1')&&((buf[2] == 0)||(buf[2] == ' '))){
    printf("Pause, Press Space Bar To Start!\n");
    getch();
  }
/*
  else if (buf[0] == 'V'){
    switch (speed){
      case 1:
	buf[1] = '1';
	buf[2] = 0;
	break;
      case 2:
	buf[1] = '2';
	buf[2] = 0;
	break;
      case 3:
	buf[1] = '3';
	buf[2] = 0;
	break;
      case 4:
	buf[1] = '4';
	buf[2] = 0;
	break;
      case 5:
	buf[1] = '5';
	buf[2] = 0;
	break;
    }
  }
*/
  return 0;
}

trans_i_to_s(int x,char *buf)
{
  int a[4];
  char b[4];
  int px,i;

  if (x < 0)
    px = -x;
  else
    px = x;
  a[0] = px / (16*16*16);
  px = px % (16*16*16);
  a[1] = px / (16*16);
  px = px % (16*16);
  a[2] = px / 16;
  a[3] = px % 16;
  if (x < 0)
    a[0] += 8;
  for (i=0;i<4;i++){
    if (a[i] < 10)
      b[i] = a[i] + '0';
    else
      b[i] = 'A' + a[i] - 10;
    buf[i] = b[i];
  }
  buf[4] = 0;
}

/* ***********************************************************************
 * pc23_readanswer: Reads a complete PC23 response string.  This routine
 *                  assumes the string will be terminated by a carriage
 *                  routine.  It will hang the program if no CR is
 *                  forthcoming.
 */
char *
pc23_readanswer ( char *s, unsigned int address )
{
	while ((*s++ = pc23_readch( address )) != 13) //CR
		;
	*s = '\0';
	return s;
} /* END PC23_READANSWER */

/* ***********************************************************************
 * pc23_readch: Reads one character of a PC23 response to an information
 *              request.  Returns the character of the response.  This
 *              routine has no error checking, improving performance
 *              but leaving the potential for the PC23 to hang the
 *              computer.
 */
char
pc23_readch( unsigned int rw_addr)
{
	int esc_seq = 0;
	int status_addr = rw_addr + 1;     /* point to status port */
	char alpha=0;                      /* character returned */
	while (!(1)) {  //ODB_READY            /* wait for char to be ready */
	   if (kbhit()) {
	      esc_seq=getch();
	      if (esc_seq==27)
		  exit(0);
	      else
		  ungetch(esc_seq);
	   }
	}
	alpha = (char) inp( rw_addr );     /* get the char */
	outp( status_addr, ACK );          /* tell the pc23 you've got it */
	while ((1)); //ODB_READY               /* wait for the pc23's acknowledge */
	outp( status_addr, CB );           /* reset the control byte to normal */
	return ( alpha );                  /* return the character read */
} /* END PC23_READCH */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -