Esempio n. 1
0
int CUartThread::Initial(char *strDev, int nBaud, int nByte, int nStop, char cCheck)
{
    int ret = 0;

    m_nFD = openport(strDev);

    if(m_nFD > 0)
    {
        ret = setport(m_nFD, nBaud, nByte, nStop, cCheck);  //设置端口参数,波特率、数据位、停止位、校验位
        if(ret < 0)
        {
            return 0;
        }

        // start thread recv data
        //this->Start();

    }
    else
    {
        return -1;
    }

    return m_nFD;
}
Esempio n. 2
0
int front_panel_open()
{
	fd = openport(fp_tty, B115200);
	if (fd <= 0) {
		trace_err("failed to open port!");
		return -1;
	}
	wu_swait_init(&fp_swait);
	trace_info("success open front panel port");

	return 0;
}
Esempio n. 3
0
File: xbee.cpp Progetto: jwatte/xbee
int main(int argc, char * const *argv) {

    int opt = 0;

    //  getopt treats argv interstingly
    while ((opt = getopt(argc, argv, "b:p:")) != -1) {
        switch (opt) {
        case 'b':
            baud = optarg;
            break;
        case 'p':
            port = optarg;
            break;
        default:
            usage();
            break;
        }
    }

    int baudi = atoi(baud);
    int bval = lookup_baud(baudi);
    if (bval <= 0) {
        fprintf(stderr, "Not a recognized baud rate: %s\n", baud);
        exit(EXIT_FAILURE);
    }

    if (optind >= argc) {
        usage();
    }
    command = argv[optind++];
    if (optind < argc) {
        filename = argv[optind++];
    }
    if (optind < argc) {
        usage();
    }

    openport(bval);

    if (!strcmp(command, "dump")) {
        do_dump();
    }
    else if (!strcmp(command, "load")) {
        do_load();
    }
    else {
        fprintf(stderr, "unknown command '%s'\n", command);
        usage();
    }

    return EXIT_SUCCESS;
}
Esempio n. 4
0
int WriteThread::Initial(char *strDev)
{
    wFD = openport(strDev);

    if(wFD < 0)
    {
         return -1;
    }
    else
    {
        return wFD;
    }
}
Esempio n. 5
0
int main(int argc, char** argv)
{
	unsigned long data;
	struct termios term;
	char data2;
	int hasdat;
	int fd;
	
	if (argc != 1)
	{
usage:
		fprintf(stderr,
			"%s: a program to watch for stuff coming out over the console over jtag.\n", argv[0]);
		exit(1);
	}
	
	openport();
	
	test_reset();		// returns us in reset state
	clockin(0, 0);		// this leaves us in run-test/idle
	look_for_pxa();		// returns us in run-test/idle state
	
	set_instr(DBGTX);	
	/* We do not do any of the high-level stuff here. Just wait for RR and read. */
	while(1)
	{
		int lcount = 0;
		hasdat = 0;
		while (!hasdat)
		{
			hasdat = gettxword(&data);
			if (!hasdat)
				lcount++;
			if (lcount > 10)
				set_instr(DBGTX);
		}
 		data2 = data & 0xFF;
 		write(1, &data2, 1);
	}
	
	closeport();
	return 0;
}
Esempio n. 6
0
static Chan*
eiaopen(Chan *c, int mode)
{
	int port = NETID(c->qid.path);

	c = devopen(c, mode, eiadir, ndir, devgen);

	switch(NETTYPE(c->qid.path)) {
	case Nctlqid:
	case Ndataqid:
	case Nstatqid:
		if(incref(&eia[port].r) != 1)
			break;
		if(waserror()) {
			decref(&eia[port].r);
			nexterror();
		}
		openport(port);
		poperror();
		break;
	}
	return c;
}
int main(int argc, char *argv[]) {
  unsigned char  addr, fc, data[256], crc[2];
  int datalen;
  char inport[100], outport[100];


  int i, j, len, err;
  int port;
  struct modbus_pdu *pdu;

  strcpy(inport,"/dev/ttyS0");
  strcpy(outport,"/dev/ttyS1");
  
  for (i=1;i<argc;i++) {
    if (strcmp(argv[i], "-inport") == 0) {
      len = strlen(argv[i+1]);
      if (len > 100) {
         printf("Error: Port name must be less then 100 characters long.\n");
         return(-1);
      };
      strncpy(inport, argv[i+1],len); 
      i++;
      printf("INFO: Input port = %s\n", inport);
    } else if (strcmp(argv[i], "-outport") == 0) {
      len = strlen(argv[i+1]);
      if (len > 100) {
         printf("Error: Port name must be less then 100 characters long.\n");
         return(-1);
      };
      strncpy(outport, argv[i+1],len); 
      i++;
    } else {
      printf("ERROR: Illegal argument (%s)\n", argv[i]);
      printf("Usage: %s -inport /dev/ttyXX -outport /dev/ttyYY\n", argv[0]);
      return (-1);
    }
  }



//turn water tank to auto
//Address = 0x7, FC = 0x10, data length = 9
//Data = 
//0x8d75038: 0b
//0x8d75039: e9
//0x8d7503a: 0a
//0x8d7503b: 14
//0x8d7503c: 02
//0x8d7503d: 0a
//0x8d7503e: 0a
//0x8d7503f: 5a
//0x8d75040: 5a
//CRC = 
//0x8d750e0: b9
//0x8d750e1: 8d
//
//
//turn water tank off
//Address = 0x7, FC = 0x10, data length = 8
//Data = 
//0x917a050: 0b
//0x917a051: e9
//0x917a052: 0a
//0x917a053: 14
//0x917a054: 0a
//0x917a055: 0a
//0x917a056: 5a
//0x917a057: 5a
//CRC = 
//0x917a530: c0
//0x917a531: 35



 port = openport(outport);
  if (port < 0) { 
   return (port);
  }

  printf("open %s ok port = %d\n", outport, port);

 while (1) {  
    addr = (unsigned char) 0x7;
    fc = (unsigned char) 0x3;
    datalen = 21;
    data[0] = 0x14;
    data[1] = 0x10;
    data[2] = 0x00;
    data[3] = 0x0e;
    data[4] = 0x00;
    data[5] = 0x0b;
    data[6] = 0xf2;
    data[7] = 0x00;
    data[8] = 0x01;
    data[9] = 0x00;
    data[10] = 0x00;
    data[11] = 0x00;
    data[12] = 0x00;
    data[13] = 0x00;
    data[14] = 0x00;
    data[15] = 0xbd;
    data[16] = 0xcc;
    data[17] = 0xcc;
    data[18] = 0xcd;
    data[19] = 0x00;
    data[20] = 0x00;
    crc[0] = 0x3a;
    crc[1] = 0xf4;

 pdu = mkpdu(addr, fc, data, datalen, crc);
// pdu->crc[0] = 0x30;
// pdu->crc[1] = 0x46;
 printmodbuspdu(pdu);
// calc_crc(pdu);
// printmodbuspdu(pdu);

 sendmodbus_RTU(port, pdu);    
 freepdu(pdu);
 sleep(1); // change to random time in the future.

 /*   addr = (unsigned char) 0x7;
    fc = (unsigned char) 0x10;
    datalen = 4;
    data[0] = 0x0b;
    data[1] = 0xe9;
    data[2] = 0x00;
    data[3] = 0x0a;
    //data[4] = 0x0a;
    //data[5] = 0x0a;
   // data[6] = 0x5a;
   // data[7] = 0x5a;
   crc[0] = 0x93;
   crc[1] = 0xb8;

 pdu = mkpdu(addr, fc, data, datalen, crc);
 //pdu->crc[0] = 0xc0;
//pdu->crc[1] = 0x35;
 printmodbuspdu(pdu);

 sendmodbus_RTU(port, pdu);    
 freepdu(pdu);
 //sleep(drand48()*5); // change to random time in the future.

*/
/* addr = (unsigned char) 0x7;
    fc = (unsigned char) 0x3;
    datalen = 21;
    data[0] = 0x14;
    data[1] = 0x10;
    data[2] = 0x10;
    data[3] = 0x0e;
    data[4] = 0x10;
    data[5] = 0x17;
    data[6] = 0xe7;
    data[7] = 0x00;
    data[8] = 0x01;
    data[9] = 0x00;
    data[10] = 0x00;
    data[11] = 0x00;
    data[12] = 0x00;
    data[13] = 0x00;
    data[14] = 0x00;
    data[15] = 0x42;
    data[16] = 0x91;
    data[17] = 0x5d;
    data[18] = 0xdf;
    data[19] = 0x00;
    data[20] = 0x00;
    crc[0] = 0x5b;
    crc[1] = 0x0f;
 pdu = mkpdu(addr, fc, data, datalen, crc);
// pdu->crc[0] = 0x30;
//  pdu->crc[1] = 0x46;
 printmodbuspdu(pdu);
//  calc_crc(pdu);
//   printmodbuspdu(pdu);

  sendmodbus_RTU(port, pdu);
   freepdu(pdu);
   sleep(1);



 addr = (unsigned char) 0x7;
    fc = (unsigned char) 0x10;
    datalen = 4;
    data[0] = 0x0b;
    data[1] = 0xe9;
    data[2] = 0x00;
    data[3] = 0x0a;
    //data[4] = 0x0a;
    //data[5] = 0x0a;
    //data[6] = 0x5a;
    //data[7] = 0x5a;
    crc[0] = 0x93;
    crc[1] = 0xb8;
   
  pdu = mkpdu(addr, fc, data, datalen, crc);
   printmodbuspdu(pdu);

 sendmodbus_RTU(port, pdu);
 freepdu(pdu);
 sleep(1);
*/


}
}
int main(int argc, char *argv[]){

  unsigned char  addr, fc, data[256], crc[2];
  int datalen;
  char inport[100], outport[100];
  int i, j, len, err;
  int port;
  struct modbus_pdu *pdu;
  int cnt1, cnt2;

  strcpy(inport,"/dev/ttyUSB2");
  strcpy(outport,"/dev/ttyUSB0");

   for (i=1;i<argc;i++) {
    if (strcmp(argv[i], "-inport") == 0) {
      len = strlen(argv[i+1]);
      if (len > 100) {
         printf("Error: Port name must be less then 100 characters long.\n");
         return(-1);
      };
      strncpy(inport, argv[i+1],len);
      i++;
      printf("INFO: Input port = %s\n", inport);
    } else if (strcmp(argv[i], "-outport") == 0) {
      len = strlen(argv[i+1]);
      if (len > 100) {
         printf("Error: Port name must be less then 100 characters long.\n");
         return(-1);
      }
      strncpy(outport, argv[i+1],len);
      i++;
    } else {
      printf("ERROR: Illegal argument (%s)\n", argv[i]);
      printf("Usage: %s -inport /dev/ttyXX -outport /dev/ttyYY\n", argv[0]);
      return (-1);
    }
  }

 port = openport(outport);
  if (port < 0) {
   return (port);
  }
  printf("open %s ok port = %d\n", outport, port);
  
  char *str;
  unsigned char lrc, top, bottom;

  addr = (unsigned char) 0x04;
  fc = (unsigned char) 0x03;
  int cnt = 1;
  while(1){
  for(cnt1=0x48; cnt1<0xff; cnt1++){
   for(cnt2=0xbf; cnt2<0xff; cnt2++){
    datalen = 19;
    data[0] = 0x12;
    data[1] = 0x10;
    data[2] = 0x00;
    data[3] = 0x0e;
    data[4] = 0x00;
    data[5] = 0x0c;
    data[6] = 0x4c;
    data[7] = 0x00;
    data[8] = 0x00;
    data[9] = 0x00;
    data[10] = 0x00;
    data[11] = 0x00;
    data[12] = 0x00;
    data[13] = 0x00;
    data[14] = 0x00;
    data[15] = cnt2;

    data[16] = cnt1;
    data[17] = 0x00;
    data[18] = 0x00; 
 
    str = malloc(datalen + 2);
    str[0] = addr;
    str[1] = fc;
    memcpy(str+2, data, datalen);   
    lrc = LRC(str,datalen+2);
    free(str);    
    byte2ascii(lrc, &top, &bottom);
    //crc[0] = 0x34;
    // crc[1] = 0x39;
    crc[0] = top;
    crc[1] = bottom;
    pdu = mkpdu(addr, fc, data, datalen, crc);
    sendmodbus_ascii(port, pdu);
    freepdu(pdu);
    printf("number is %d\n", cnt++);
    sleep(1);

   }
  }
  }
}
Esempio n. 9
0
//===========================================
int main (int argc, char * argv[])
{
    //------------------
    //debugging option
    char dump,showlastonly;

    //-------------------
    int i,j,k,len,cl;
    unsigned int c;
    unsigned char data[1024];
    char buffer[1024];
    char * pb;

	//evaluate cmdline line argument
	if(argc < 4 ){
        printf("usage %s <device> <baudrate> <ussdcommand>\n",argv[0]);
	    return 1;
	}

	//dump verbose option
	if     (!memcmp("-vvv",argv[1],4)){cl=1;showlastonly=1;dump=1;verbose=1;}
	else if(!memcmp("-vv" ,argv[1],3)){cl=1;showlastonly=1;dump=1;verbose=0;}
	else if(!memcmp("-v"  ,argv[1],2)){cl=1;showlastonly=1;dump=0;verbose=0;}
    else {cl=0;showlastonly=0,dump=0,verbose=0;}

    //setting up baudrate
    serial.baudrate=atoi(argv[cl+2]);
    if(getbaudrate(serial.baudrate)==1){
        fprintf(stderr,"Fatal Error, cannot set baudrate: %d\n",serial.baudrate);
        return 1;
    }

	serial.port=strdup(argv[cl+1]);
    //opening the port
    if (openport()){
        fprintf(stderr,"Fatal Error, cannot open device: %s\n",serial.port);
        return 1;
    }

    ///*
    if(dump)printf("AT\n");
    serial_port_write("AT\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }
    //if(len)printhex(data,len);

    if(dump)printf("ATE0\n");
    serial_port_write("ATE0\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }   
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }
    //if(len)printhex(data,len);

    if(dump)printf("AT^CURC=0\n");
    serial_port_write("AT^CURC=0\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }
    //if(len)printhex(data,len);

    if(dump)printf("AT^USSDMODE=0\n");
    serial_port_write("AT^USSDMODE=0\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }
    //if(len)printhex(data,len);

    //sending the ussd sequence start at argv[3];
    for (i=cl+3;i<argc;i++){
        memset(buffer,0,sizeof(buffer));
        strcpy(buffer,"AT+CUSD=1,\"");
        strcat(buffer,argv[i]);
        strcat(buffer,"\",15\r\n");
        //strcat(buffer,"\"\r\n");

        if(dump){printf(buffer);}
        serial_port_write(buffer);
        len=getser(data,timeoutussd);

        //see if this is not ascii
        //skip respon daOKda+CUSSD: 1,", start at 18
        //end ,68"da at len-6

        if(len>22){
            //data received nicely, dump to stdout if only last ussd sequence
            if( showlastonly || i == (argc-1)) {
                //check wheter ascii transparent or encoded 7bit ascii
                //if(dump)fwrite(&data[18],1,len-18-6,stdout);
                k=0;
                for (j=18;j<22;j++){
                    if (((data[j] >= '0') && (data[j] <='9')) ||
                        ((data[j] >= 'A') && (data[j] <='F')) ||
                        ((data[j] >= 'a') && (data[j] <='f'))
                        ) {k++;}
                    else {k=0;break;}
                }
                if(k){
                    //data not ascii encoded 7bit ascii integer (2byte char)
                    //printf("not ascii\n");
                    //printf("length  %d bagi 4 %d sisa %d\n",(len-18-6),((len-18-6)/4),(len-18-6) % 4 );
                    //printf("\n");
                    for(k=18;k<(len-6);k+=4){
                        c=0;
                        for(j=0;j<4;j++){
                            c=c<<4;
                            if      ((data[k+j] >= '0') && (data[k+j] <='9'))c+=data[k+j]-'0';
                            else if ((data[k+j] >= 'A') && (data[k+j] <='F'))c+=data[k+j]-'A'+0x0a;
                            else if ((data[k+j] >= 'a') && (data[k+j] <='f'))c+=data[k+j]-'a'+0x0a;
                            else {perror ("failed 7bit decode\n");return 1;}
                        }
                        putchar(c);//putchar(0x20);
                    }
                    printf("\n");
                }
                //data is ascii transparent, continue
                else {fwrite(&data[18],1,len-18-6,stdout);printf("\n");}
            }//if(i==argc-1)
        }//if(len>22)
        else{
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            break;
            //goto endsession;
            //return 1;
        }
    }//for (i=3;i<argc;i++)
    
    endsession:
    //end ussd session
    if(dump)printf("AT+CUSD=2\r\n");
    serial_port_write("AT+CUSD=2\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }

    closeport();
    return 0;
}
Esempio n. 10
0
int main (int argc, char const * argv [])

{
	static char const * optv [] =
	{
		"B:c:D:F:m:p:P:q:S:uvw",
		"",
		"Atheros Serial Line Device Settings",
		"B n\tbaud rate is (n) [" LITERAL (UART_BAUDRATE) "]",
		"c s\tsend custom serial line command (s)",
		"D n\tuse (n) data bits [" LITERAL (UART_DATABITS) "]",
		"F n\tflow control is (n) ["LITERAL (UART_FLOWCTRL) "]",
		"m n\tcommand mode is (n)",
		"p f\tserial port is (f) [" DEVICE "]",
		"P n\tuse (n) parity bits [" LITERAL (UART_PARITY) "]",
		"q\tquiet mode",
		"S n\tuse (n) stop bits [" LITERAL (UART_STOPBITS) "]",
		"u\tforce default host port settings [115200 8N1]",
		"v\tverbose mode",
		"w\twake device [+++]",
		(char const *) (0)
	};
	struct uart uart =
	{
		{
			0,
			DEVICE
		},
		(char *)(0),
		UART_MODE,
		UART_BAUDRATE,
		UART_DATABITS,
		UART_PARITY,
		UART_STOPBITS,
		UART_FLOWCTRL,
		0
	};
	signed c;
	if (getenv (UART_PORT))
	{
		uart.port.name = strdup (getenv (UART_PORT));
	}
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch (c)
		{
		case 'B':
			_setbits (uart.flags, UART_ATBR);
			uart.baudrate = (uint64_t)(uintspec (optarg, 1, ULONG_MAX));
			break;
		case 'c':
			_setbits (uart.flags, UART_COMMAND);
			uart.string = optarg;
			break;
		case 'D':
			_setbits (uart.flags, UART_ATBR);
			uart.databits = (byte)(uintspec (optarg, 7, 8));
			break;
		case 'F':
			_setbits (uart.flags, UART_ATBR);
			uart.flowctrl = (byte)(uintspec (synonym (optarg, flowctrls, FLOWCTRLS), 0, UCHAR_MAX));
			break;
		case 'm':
			_setbits (uart.flags, UART_ATBR);
			uart.mode = (byte)(uintspec (synonym (optarg, modes, MODES), 0, UCHAR_MAX));
		case 'P':
			_setbits (uart.flags, UART_ATBR);
			uart.parity = (byte)(uintspec (synonym (optarg, paritybits, PARITYBITS), 0, UCHAR_MAX));
			break;
		case 'q':
			_setbits (uart.flags, UART_SILENCE);
			break;
		case 'p':
			uart.port.name = optarg;
			break;
		case 'S':
			_setbits (uart.flags, UART_ATBR);
			uart.stopbits = (unsigned)(uintspec (optarg, 1, 2));
			break;
		case 'u':
			_setbits (uart.flags, UART_DEFAULT);
			break;
		case 'v':
			_setbits (uart.flags, UART_VERBOSE);
			break;
		case 'w':
			_setbits (uart.flags, UART_WAKE);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc)
	{
		error (1, ENOTSUP, ERROR_TOOMANY);
	}
	openport (&uart.port, uart.flags);
	manager (&uart);
	closeport (&uart.port);
	return (0);
}
Esempio n. 11
0
int main(int argc, char *argv[])
{
 if(!init_cfg())
 {
  fprintf(stderr, "Unable to open config file or invalid content in config file.\n");
  exit(1);
 }
 
 parseopts(argc, argv, conf);

 if(conf->msg)
 {
  if(!open_producer())
  {
   printf("Unable to contact Bluemote server!\n");
   exit(1);
  }
  if(ipc_write(conf->device, strlen(conf->device))<=0)
  {
   printf("Unable to send message to Bluemote server!\n");
   close_producer();
   exit(1);
  }
  close_producer();
  exit(0);
 }
 else
 {
  if(!get_lock())
  {
   printf("Another instance of Bluemote already running!\n");
   exit(1);
  }

  if(!open_consumer())
  {
   printf("Unable to listen for messages!\n");
   exit(1);
  }
 }

 strcpy(logfile, getenv("HOME"));
 strcat(logfile, BLUEMOTEDIR);
 strcat(logfile, LOGFILE);

 printf("Setting up signal handlers.\n");
 logger("INIT", "Setting up signal handlers");
 if(!init_signals())
 {
  perror("init_signals()");
  exit(1);
 }

 if(conf->daemon)
 {
  printf("Entering daemon mode.\n");
  logger("INIT", "Entering daemon mode.\n");
  if(daemon(TRUE, FALSE)==-1)
  {
   printf("Unable to enter daemon mode. Exiting.\n");
   logger("ERROR", "Unable to enter daemon mode. Exiting.\n");
   exit(1);
  }
 }

 while(TRUE)
 {
  closeport();

  printf("Connecting to phone...\n");
  logger("INIT", "Connecting to phone...");
  while(openport(conf->device) == -1)
  {
   sprintf(buf,"Unable to connect to phone. Will retry after %d secs.",conf->retrysecs);
   logger("INIT", buf);
   sleep(conf->retrysecs);
   logger("INIT", "Retrying...");
  }
  printf("Connected to phone.\n");
  logger("INIT", "Connected to phone.");
  
  printf("Initialising the connection.\n");
  logger("INIT", "Initialising the connection.\n");
  if(!initport()) exit(1);

  printf("Waiting for commands from phone\n");
  logger("INIT", "Waiting for commands from phone\n");

  /* Not looping to take care of timeout because timeout is very unlikely and
   * this function is called again inside remote(). In the worst case, the I/O
   * commands for Connect event won't have any effect. */
  if(init_mainmenu()==-1) continue;

  exec_event("Connect");
  if(!manual)
  {
   exec_event("MoveIn");
  }
  manual = FALSE;
  remote();
  if(!manual)
  {
   exec_event("MoveOut");
  }
  exec_event("Disconnect");
 }
 return(0);
}
int main(int argc, char *argv[]){

  unsigned char addr, fc, data[256], crc[2];
  int datalen;
  char inport[100], outport[100];
  int i, j, len, err;
  int port;
  struct modbus_pdu *pdu;
  int cnt1 = 0, cnt2 = 0x0, cnt;
  
  strcpy(inport, "/dev/ttyUSB2");
  strcpy(outport, "/dev/ttyUSB0");
  
  for(i=1; i<argc; i++){
    if(strcmp(argv[i], "-inport") == 0){
      len = strlen(argv[i+1]);
      if(len > 100){
        printf("Error: Port name must be less than 100 characters long.\n");
        return(-1);
      }
      strncpy(inport, argv[i+1], len);
      i++;
      printf("INFO: Input port = %s\n", inport);
    } else if(strcmp(argv[i], "-outport") == 0){
      len = strlen(argv[i+1]);
      if(len > 100){
        printf("Error: Port name must be less than 100 characters long.\n");
        return(-1);
      }
      strncpy(outport, argv[i+1],len);
      i++;      
    }else{
      printf("ERROR: Illegal arguments (%s)\n", argv[i]);
      printf("Usage: %s -inport /dev/ttyXX -outport /dev/ttyYY\n", argv[0]);
      return (-1);
    }
  }
   
  port = openport(outport);
  if(port < 0)
    return (port);
  printf("open %s ok port = %d\n", outport, port);
  
  char *str;
  unsigned char lrc, top, bottom;
  addr = (unsigned char) 0x4; 

  //fc =0x01 read coil,
  while(1){

   cnt1 = 0;
  for(cnt1=0; cnt1<100; cnt1++){
  fc = (unsigned char) 0x01;
  datalen = 19;
  data[0] = 0x00;
  data[1] = 0x00;
  data[2] = 0x00;
  data[3] = 0x00;
  data[4] = 0x00;
  data[5] = 0x00;
  data[6] = 0x00;
  data[7] = 0x00;
  data[8] = 0x00;
  data[9] = 0x00;
  data[10] = 0x00;
  data[11] = 0x00;
  data[12] = 0x00;
  data[13] = 0x00;
  data[14] = 0x00;
  data[15] = 0x00;
  data[16] = 0x00;
  data[17] = 0x00;
  data[18] = 0x00;


  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);     
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;
  
  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("number is %d\n", cnt1);
  sleep(1);
  }

   for(cnt1=0; cnt1<100; cnt1++){
  fc = (unsigned char) 0x01;
  datalen = 19;
  data[0] = 0xff;
  data[1] = 0xff;
  data[2] = 0xff;
  data[3] = 0xff;
  data[4] = 0xff;
  data[5] = 0xff;
  data[6] = 0xff;
  data[7] = 0xff;
  data[8] = 0xff;
  data[9] = 0xff;
  data[10] = 0xff;
  data[11] = 0xff;
  data[12] = 0xff;
  data[13] = 0xff;
  data[14] = 0xff;
  data[15] = 0xff;
  data[16] = 0xff;
  data[17] = 0xff;
  data[18] = 0xff;
   str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;

  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("number is %d\n", cnt1);
  sleep(1);
  }
 
  //fc = 0x02 Read Discrete Inputs, not implement excode = 01
  fc = (unsigned char)0x02;
  for(cnt1=0; cnt1<100; cnt1++){
   datalen = 19;
  data[0] = 0x00;
  data[1] = 0x00;
  data[2] = 0x00;
  data[3] = 0x00;
  data[4] = 0x00;
  data[5] = 0x00;
  data[6] = 0x00;
  data[7] = 0x00;
  data[8] = 0x00;
  data[9] = 0x00;
  data[10] = 0x00;
  data[11] = 0x00;
  data[12] = 0x00;
  data[13] = 0x00;
  data[14] = 0x00;
  data[15] = 0x00;
  data[16] = 0x00;
  data[17] = 0x00;
  data[18] = 0x00;
 
  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;
  
  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("number is %d\n", cnt);
  sleep(1);
  }

  for(cnt1=0; cnt1<100; cnt1++){
  fc = (unsigned char) 0x02;
  datalen = 19;
  data[0] = 0xff;
  data[1] = 0xff;
  data[2] = 0xff;
  data[3] = 0xff;
  data[4] = 0xff;
  data[5] = 0xff;
  data[6] = 0xff;
  data[7] = 0xff;
  data[8] = 0xff;
  data[9] = 0xff;
  data[10] = 0xff;
  data[11] = 0xff;
  data[12] = 0xff;
  data[13] = 0xff;
  data[14] = 0xff;
  data[15] = 0xff;
  data[16] = 0xff;
  data[17] = 0xff;
  data[18] = 0xff;
  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;

  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("number is %d\n", cnt1);
  sleep(1);
  }

  //fc = 0x03 Read Holding Registers, not implement excode = 01
  fc = 0x03;
  for(cnt1=0; cnt1<100; cnt1++){
  datalen = 19;
  data[0] = 0x00;
  data[1] = 0x00;
  data[2] = 0x00;
  data[3] = 0x00;
  data[4] = 0x00;
  data[5] = 0x00;
  data[6] = 0x00;
  data[7] = 0x00;
  data[8] = 0x00;
  data[9] = 0x00;
  data[10] = 0x00;
  data[11] = 0x00;
  data[12] = 0x00;
  data[13] = 0x00;
  data[14] = 0x00;
  data[15] = 0x00;
  data[16] = 0x00;
  data[17] = 0x00;
  data[18] = 0x00;

  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;
  
  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("numb is %d\n", cnt1);
  sleep(1);
  }

  for(cnt1=0; cnt1<100; cnt1++){
  fc = (unsigned char) 0x03;
  datalen = 19;
  data[0] = 0xff;
  data[1] = 0xff;
  data[2] = 0xff;
  data[3] = 0xff;
  data[4] = 0xff;
  data[5] = 0xff;
  data[6] = 0xff;
  data[7] = 0xff;
  data[8] = 0xff;
  data[9] = 0xff;
  data[10] = 0xff;
  data[11] = 0xff;
  data[12] = 0xff;
  data[13] = 0xff;
  data[14] = 0xff;
  data[15] = 0xff;
  data[16] = 0xff;
  data[17] = 0xff;
  data[18] = 0xff;
  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;

  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("number is %d\n", cnt1);
  sleep(1);
  }

  //fc = 0x04 Read Input Registers, not implement excode = 01
  fc = 0x04;
  for(cnt1=0; cnt1<100; cnt1++){
  len = 19;
  data[0] = 0x00;
  data[1] = 0x00;
  data[2] = 0x00;
  data[3] = 0x00;
  data[4] = 0x00;
  data[5] = 0x00;
  data[6] = 0x00;
  data[7] = 0x00;
  data[8] = 0x00;
  data[9] = 0x00;
  data[10] = 0x00;
  data[11] = 0x00;
  data[12] = 0x00;
  data[13] = 0x00;
  data[14] = 0x00;
  data[15] = 0x00;
  data[16] = 0x00;
  data[17] = 0x00;
  data[18] = 0x00;

  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;

  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);

  freepdu(pdu);
  printf("number is %d\n", cnt1);
  sleep(1);
  }
  
   for(cnt1=0; cnt1<100; cnt1++){
  fc = (unsigned char) 0x04;
  datalen = 19;
  data[0] = 0xff;
  data[1] = 0xff;
  data[2] = 0xff;
  data[3] = 0xff;
  data[4] = 0xff;
  data[5] = 0xff;
  data[6] = 0xff;
  data[7] = 0xff;
  data[8] = 0xff;
  data[9] = 0xff;
  data[10] = 0xff;
  data[11] = 0xff;
  data[12] = 0xff;
  data[13] = 0xff;
  data[14] = 0xff;
  data[15] = 0xff;
  data[16] = 0xff;
  data[17] = 0xff;
  data[18] = 0xff;
  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;

  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);

  freepdu(pdu);
  printf("number is %d\n", cnt1);
  sleep(1);
  }

  //fc = 0x18, read FIFO
  fc = 0x18;
  for(cnt1=0; cnt1<100; cnt1++){
  len = 19;
  data[0] = 0x00;
  data[1] = 0x00;
  data[2] = 0x00;
  data[3] = 0x00;
  data[4] = 0x00;
  data[5] = 0x00;
  data[6] = 0x00;
  data[7] = 0x00;
  data[8] = 0x00;
  data[9] = 0x00;
  data[10] = 0x00;
  data[11] = 0x00;
  data[12] = 0x00;
  data[13] = 0x00;
  data[14] = 0x00;
  data[15] = 0x00;
  data[16] = 0x00;
  data[17] = 0x00;
  data[18] = 0x00;

  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;

  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("number is %d\n", cnt1++);
  sleep(1);
  } 

   for(cnt1=0; cnt1<100; cnt1++){
  fc = (unsigned char) 0x18;
  datalen = 19;
  data[0] = 0xff;
  data[1] = 0xff;
  data[2] = 0xff;
  data[3] = 0xff;
  data[4] = 0xff;
  data[5] = 0xff;
  data[6] = 0xff;
  data[7] = 0xff;
  data[8] = 0xff;
  data[9] = 0xff;
  data[10] = 0xff;
  data[11] = 0xff;
  data[12] = 0xff;
  data[13] = 0xff;
  data[14] = 0xff;
  data[15] = 0xff;
  data[16] = 0xff;
  data[17] = 0xff;
  data[18] = 0xff;

  str = malloc(datalen+2);
  str[0] = addr;
  str[1] = fc;
  memcpy(str+2, data, datalen);
  lrc = LRC(str, datalen+2);
  free(str);
  byte2ascii(lrc, &top, &bottom);
  crc[0] = top;
  crc[1] = bottom;

  pdu = mkpdu(addr, fc, data, datalen, crc);
  sendmodbus_ascii(port, pdu);
  freepdu(pdu);
  printf("number is %d\n", cnt1++);
  sleep(1);
  }
 }
}
Esempio n. 13
0
File: rip.c Progetto: aahud/harvey
void
main(int argc, char *argv[])
{
	int dobroadcast, i, n;
	long diff;
	char *p;
	char buf[2*1024];
	uint8_t raddr[Pasize];
	Bnet *bn, **l;
	Udphdr *up;
	Rip *r;
	Ripmsg *m;
	Route route;
	static long btime;

	setnetmtpt(netdir, sizeof(netdir), nil);
	dobroadcast = 0;
	ARGBEGIN{
	case 'b':
		dobroadcast++;
		break;
	case 'd':
		debug++;
		break;
	case 'n':
		readonly++;
		break;
	case 'x':
		p = ARGF();
		if(p == nil)
			usage();
		setnetmtpt(netdir, sizeof(netdir), p);
		break;
	default:
		usage();
	}ARGEND

	/* specific broadcast nets */
	l = &bnets;
	while(argc > 0){
		bn = (Bnet*)malloc(sizeof(Bnet));
		if(bn == 0)
			fatal(1, "out of mem");
		v4parseip(bn->addr, *argv);
		*l = bn;
		l = &bn->next;
		argc--;
		argv++;
		dobroadcast++;
	}

	/* command returns */
	if(!debug)
		switch(rfork(RFNOTEG|RFPROC|RFFDG|RFNOWAIT)) {
		case -1:
			fatal(1, "fork");
		case 0:
			break;
		default:
			exits(0);
		}


	fmtinstall('E', eipfmt);
	fmtinstall('V', eipfmt);

	snprint(routefile, sizeof(routefile), "%s/iproute", netdir);
	snprint(buf, sizeof(buf), "%s/iproute", netdir);

	now = time(0);
	readifcs();
	readroutes();

	notify(ding);

	ripfd = openport();
	for(;;) {
		diff = btime - time(0);
		if(diff <= 0){
			if(dobroadcast)
				broadcast();
			timeoutroutes();

			btime = time(0) + 2*60;
			diff = 2*60;
		}
		alarm(diff*1000);
		n = read(ripfd, buf, sizeof(buf));
		alarm(0);
		if(n <= 0)
			continue;

		n = (n - Udphdrsize - 4) / sizeof(Rip);
		if(n <= 0)
			continue;

		up = (Udphdr*)buf;
		m = (Ripmsg*)(buf+Udphdrsize);
		if(m->type != Response || m->vers != Version)
			continue;
		v6tov4(raddr, up->raddr);

		/* ignore our own messages */
		for(i = 0; i < ialloc.nifc; i++)
			if(equivip(ialloc.ifc[i].addr, raddr))
				continue;

		now = time(0);
		for(r = m->rip; r < &m->rip[n]; r++){
			memmove(route.gate, raddr, Pasize);
			memmove(route.mask, getmask(r->addr), Pasize);
			v4maskip(r->addr, route.mask, route.dest);
			route.metric = nhgetl(r->metric) + 1;
			if(route.metric < 1)
				continue;
			considerroute(&route);
		}
	}
	/* not reached */
}
int main(int argc, char *argv[]){

  unsigned char  addr, fc, data[256], crc[2];
  int datalen;
  char inport[100], outport[100];
  int i, j, len, err;
  int port;
  struct modbus_pdu *pdu;
  int cnt = 0x0;
  int id = 0x0;

  strcpy(inport,"/dev/ttyUSB0");
  strcpy(outport,"/dev/ttyUSB2");

   for (i=1;i<argc;i++) {
    if (strcmp(argv[i], "-inport") == 0) {
      len = strlen(argv[i+1]);
      if (len > 100) {
         printf("Error: Port name must be less then 100 characters long.\n");
         return(-1);
      };
      strncpy(inport, argv[i+1],len);
      i++;
      printf("INFO: Input port = %s\n", inport);
    } else if (strcmp(argv[i], "-outport") == 0) {
      len = strlen(argv[i+1]);
      if (len > 100) {
         printf("Error: Port name must be less then 100 characters long.\n");
         return(-1);
      }
      strncpy(outport, argv[i+1],len);
      i++;
    } else {
      printf("ERROR: Illegal argument (%s)\n", argv[i]);
      printf("Usage: %s -inport /dev/ttyXX -outport /dev/ttyYY\n", argv[0]);
      return (-1);
    }
  }

 port = openport(outport);
  if (port < 0) {
   return (port);
  }
  printf("open %s ok port = %d\n", outport, port);
  
  char *str;
  unsigned char lrc, top, bottom;
  while(1){
  for(cnt=0x01; cnt<=0x04; cnt++){
   for(id=0x00; id<=0xff; id++){
    addr = (unsigned char) 0x07;
    fc = (unsigned char) 0x2b;
    datalen = 3;
    data[0] = 0x0e;  //MEI type
    data[1] = cnt;   // ID code
    data[2] = id;  //object ID
  
    str = malloc(datalen + 2);
    str[0] = addr;
    str[1] = fc;
    memcpy(str+2, data, datalen);   
    lrc = LRC(str,datalen+2);
    free(str);    
    byte2ascii(lrc, &top, &bottom);
    crc[0] = top;
    crc[1] = bottom;
  
    pdu = mkpdu(addr, fc, data, datalen, crc);
    sendmodbus_ascii(port, pdu);
    freepdu(pdu);
    printf("number is %d\n", cnt);
    sleep(4);
   }
  }
 }
  
}
Esempio n. 15
0
//===========================================
int main (int argc, char * argv[])
{
    //------------------
    //debugging option
    char dump,showlastonly;

    //-------------------
    int i,j,k,len,cl;
    unsigned int c;
    unsigned char data[1024];
    char buffer[1024];
    char * pb;

	//evaluate cmdline line argument
	if(argc < 3 ){
        printf("usage %s <device> <baudrate>\n",argv[0]);
	    return 1;
	}

	//dump verbose option
	if     (!memcmp("-vvv",argv[1],4)){cl=1;showlastonly=1;dump=1;verbose=1;}
	else if(!memcmp("-vv" ,argv[1],3)){cl=1;showlastonly=1;dump=1;verbose=0;}
	else if(!memcmp("-v"  ,argv[1],2)){cl=1;showlastonly=1;dump=0;verbose=0;}
    else {cl=0;showlastonly=0,dump=0,verbose=0;}

    //setting up baudrate
    serial.baudrate=atoi(argv[cl+2]);
    if(getbaudrate(serial.baudrate)==1){
        fprintf(stderr,"Fatal Error, cannot set baudrate: %d\n",serial.baudrate);
        return 1;
    }

	serial.port=strdup(argv[cl+1]);
    //opening the port
    if (openport()){
        fprintf(stderr,"Fatal Error, cannot open device: %s\n",serial.port);
        return 1;
    }

    ///*
    if(dump)printf("AT\n");
    serial_port_write("AT\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }
    //if(len)printhex(data,len);

    if(dump)printf("ATE0\n");
    serial_port_write("ATE0\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }   
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }
    //if(len)printhex(data,len);

    if(dump)printf("AT^CURC=0\n");
    serial_port_write("AT^CURC=0\r\n");
    len=getser(data,timeout);
    if(len){
        if(dump)fwrite(data,1,len,stdout);
        pb=(char *) buffer;
        pb=strstr((char *)data,"OK");
        if(memcmp("OK",pb,2)){
            perror("ERROR data!\n");
            fwrite(data,1,len,stdout);
            return 1;
        }
    }else{
        perror("ERROR timeout!\n");
        return 1;
    }
    //if(len)printhex(data,len);

    closeport();
    return 0;
}
int main(int argc, char *argv[]) {
  unsigned char  addr, fc, data[256], crc[2];
  int datalen;
  char inport[100], outport[100];


  int i, j, len, err;
  int port;
  struct modbus_pdu *pdu;
  
  int atk_cnt;
  strcpy(inport,"/dev/ttyS0");
  strcpy(outport,"/dev/ttyS1");
  
  for (i=1;i<argc;i++) {
   if (strcmp(argv[i], "-outport") == 0) {
      len = strlen(argv[i+1]);
      if (len > 100) {
         printf("Error: Port name must be less then 100 characters long.\n");
         return(-1);
      };
      strncpy(outport, argv[i+1],len+1); 
      i++;
    } else {
      printf("ERROR: Illegal argument (%s)\n", argv[i]);
      printf("Usage: %s -inport /dev/ttyXX -outport /dev/ttyYY\n", argv[0]);
      return (-1);
    }
  }


 port = openport(outport);
  if (port < 0) { 
   return (port);
  }

  printf("open %s ok port = %d\n", outport, port);

 while (1) {  
  for(atk_cnt = 0; ; atk_cnt++){
    addr = (unsigned char) 0x7;
    fc = (unsigned char) 0x3;
    datalen = 21;
    data[0] = 0x14;
    data[1] = 0x10;
    data[2] = 0x00;
    data[3] = 0x0e;
    data[4] = 0x00;
    data[5] = 0x11;
    data[6] = 0x20;
    data[7] = 0x00;
    data[8] = 0x01;
    data[9] = 0x00;
    data[10] = 0x00;
    data[11] = 0x00;
    data[12] = 0x00;
    data[13] = 0x00;
    data[14] = 0x00;
    data[15] = 0x41;
    data[16] = 0xba;
    data[17] = 0xec;
    data[18] = 0xa5;
    data[19] = 0x00;
    data[20] = 0x00;
    crc[0] = 0xb4;
    crc[1] = 0x29;

 pdu = mkpdu(addr, fc, data, datalen, crc);
 printmodbuspdu(pdu);
 sendmodbus_RTU(port, pdu);    //Send read response
 freepdu(pdu);
 sleep(1); // change to random time in the future.

}
}
}