Ejemplo n.º 1
0
// Free returned memory!
uint8_t *memGetBytes(uint32_t address, uint8_t length) {
	// We could use the High-Speed Mode of the FM24V10 here, but we don't, right now...
	uint8_t addA, addB, memAddress = MEMTWIADDRESS, i, *ret;
	if (address >= 65536) {
		// Address needs more than 16 bits, we have to set the PAGE bit in i2c address
		memAddress |= 2;
	}
	addA = (address & 0xFF00) >> 8;
	addB = address & 0xFF;
	ret = (uint8_t *)malloc(length); // Allocate memory for values read
	if (ret == NULL) {
		serialWriteString(getString(24));
		return NULL;
	}

	if (i2c_start(memAddress | I2C_WRITE) == 0) {
		i2c_write(addA);
		i2c_write(addB);
		i2c_rep_start(memAddress | I2C_READ);
		for (i = 0; i < (length - 1); i++) {
			ret[i] = i2c_readAck();
		}
		ret[length - 1] = i2c_readNak();
		i2c_stop();
		return ret;
	} else {
		return NULL;
	}
}
Ejemplo n.º 2
0
void loop()
{
  switch (rs485State)
  {
    case Rs485Loopback:
      serialReadString();
      serialWriteString();
      break;

    case Rs485RxTest:
      if (TIMER_COUNT_1S < timerCounter)
      {
        Serial.write("~!EloHello$75~");
        timerCounter = 0;
      }
      break;

    case Rs485TxTest:
      serialReadString();
      break;

    default:
      break;
  }

  // Create a client connection
  EthernetClient client = server.available();
  if (client)
  {
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
         }

         //if HTTP request has ended
         if (c == '\n')
         {
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button485Loopback") >0)        changeRs485Mode(Rs485Loopback);
           else if (readString.indexOf("?button485RxTest") >0)     changeRs485Mode(Rs485RxTest);
           else if (readString.indexOf("?button485TxTest") >0)     changeRs485Mode(Rs485TxTest);

           //clearing string for next read
           readString="";

           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
           client.println();
           client.println("<HTML>");
           client.println("<HEAD>");
           refreshSection(client);
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>RS-485 - RET tester</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>RS-485 - RET tester</H1>");
           client.println("<hr />");
           client.println("<br />");
           client.println("<H2>RS-485 mode</H2>");
           client.println("<a href=\"/?button485Loopback\"\">1. RS-485 Loopback</a>");
           client.println("<a href=\"/?button485RxTest\"\">2. RS-485 RX Test</a>");
           client.println("<a href=\"/?button485TxTest\"\">3. RS-485 TX Test</a>");
           client.println("<br />");
           client.println("<H2>RET Status</H2>");
           // DC Voltage section
           // RS485 section
           client.print("<a style=\"background-color:#4DB257\">RS-485 mode: ");  //
           client.print(rs485State);
           client.println("</a> <br />");
           client.println("<br />");
           // Last 10 received messages section
           client.print("<H2>Last ");
           client.print(MSG_BUFFER_SIZE);
           client.println(" received messages</H2>");

           char counter = msgBuffer.counter;
           for(char i = 0; i < MSG_BUFFER_SIZE; ++i)
           {
             client.print(msgBuffer.readString[counter]);
             client.println("<br />");
             if (--counter < 0) counter = MSG_BUFFER_SIZE-1;
           }

           client.println("<br />");
           client.println("</BODY>");
           client.println("</HTML>");

           delay(1);
           //stopping client
           client.stop();
         }
       }
    }
  }
}
Ejemplo n.º 3
0
//thread function: process commands as they're added to the queue
void* processCommands(void *vhand) {
	BHand *hand = (BHand*)vhand;
	char *command;
	char resp[MAX_FEEDBACK_LEN];
	int bytesread,len;
	CommandPacket *currentCP; 
	char *respt;

	while (1) {
		// check if we have any pending commands to send out
		if (queueSize(hand)>0) {

			// flush the serial port input
			tcflush(hand->port.ifd,TCIFLUSH);

			currentCP = &hand->commandQueue[hand->queueHead];
			
			// send out command
			command = currentCP->command;
			serialWriteString(&(hand->port),command);
			free(command);

			//if in realtime mode
			if(currentCP->realtime){
				int err;
				
				//get the acknowledgement char (*)
				err = get_ack_star(hand);
				int responselen = currentCP->responselen;	
				if(err){
					printf("error in getting ack!\n");
					resp[0] = 0;
				}
				else{
					//get a feedback block of length responselen
					//printf("command %d responselen:%d\n", currentCP->index, responselen);
					err = read_serial_block(hand, resp, responselen);
					if(err == -1){
						printf("error in read_serial_block!\n");
					}
				}
				//put it in hand->lastResponse
				setResponse(hand, resp, currentCP->index, responselen);
			}
			else{
				//not in realtime mode, get the command echo/response
				bytesread = 0;
				len = 0;
				while (1) {
					serialRead(&(hand->port),resp+len,MAX_FEEDBACK_LEN,&bytesread);
					len += bytesread;
					if (len>=2) {
						char *startResp;
						if (startResp=strstr(resp,"=>")) {
							len = startResp-resp-2;
							break;
						}
					} else if (len>=MAX_FEEDBACK_LEN) {
						printf("bhand_processCommands: ERROR: command overflow\n");
						break;
					}
				}
				resp[len] = 0;
				respt = memchr(resp,'\n',len);
				if(DEBUG)
					printf("resp %d: %s\n", currentCP->index, resp);
				if(respt){
					respt = respt+2;
					if(DEBUG)
						printf("respt: %s\n", respt);
					setResponse(hand, respt, currentCP->index, -1);
				}
				else{
					setResponse(hand, resp, currentCP->index, -1);
				}
			}
			
			// next
			pthread_mutex_lock(&(hand->mutex));
			hand->queueHead = (hand->queueHead+1) % COMMAND_QUEUE_SIZE;
			pthread_mutex_unlock(&(hand->mutex));
		}
		usleep(10000);
	}
}