harbour comms via I2C

Issues and Discussions related to Harbour

Moderator: Rathinagiri

Post Reply
Carlos Britos
Posts: 245
Joined: Sat Aug 02, 2008 5:03 pm

harbour comms via I2C

Post by Carlos Britos »

Hi
I´m trying to build a system, consist in one HMG app running in a PC as a master and 10 devices as a slaves.
The communication is via USB - I2C (two wires) each device has a numeric ID.
The devices have its own library (wire.h)
I cant found the way how hbcomm lib set the numeric ID (address) to each device to exchange messages.
Is this possible with hbcomm lib or another lib ?
Somebody has experience with this topic to give me a clue ?
Regards/Saludos, Carlos (bcd12a)
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: harbour comms via I2C

Post by danielmaximiliano »

Hola Carlos:
HBcomm es una libreia que maneja el puerto de comunicaciones COMx y es bastante lento, los modulos que se compraron para ser esclavos no vienen con software propietario, seguramente viene unas DLL para ustilizar con HMG.
si ese software o dispositivo USB crea una interfaz virtual COMx puede ser utilizado por HMG con la libreria HBcomm.

http://robots-argentina.com.ar/Comunicacion_busI2C.htm

http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Carlos Britos
Posts: 245
Joined: Sat Aug 02, 2008 5:03 pm

Re: harbour comms via I2C

Post by Carlos Britos »

Hola Daniel
Los modulos esclavos son arduino o sea que el codigo fuente lo tengo que hacer yo. No hay dlls.
Los arduinos tienen una libreria serial muy simple y no tiene problemas con comunicaciones entre si.
No se si se puede compilar esa libreria (es codigo C y cpp) en harbour de 32 bits.
Lo que me decis de la interfase virtual COMx no tengo idea de como es, sabes de algun ejemplo ?

con respecto a la velocidad yo necesito hacer una lectura y escritura cada 10 segundos en cada slave. supongo que no habra problemas con esa velocidad. no?

ejemplo de master writer

Code: Select all

// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop() {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting

  x++;
  delay(500);
}
ejemplo de master reader

Code: Select all

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}
ejemplo de slaver receiver

Code: Select all

#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}
Regards/Saludos, Carlos (bcd12a)
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: harbour comms via I2C

Post by danielmaximiliano »

¿ Arduino que modelo ? ya que asumo que el Master Arduino es I2C en salida pero la conexion al PC se hace a traves del puerto usb. en caso que no venga nada con el dispositivo es Plug&Play e instala drivers genericos.

asumimos que se pueda utilizar esta libreria, se tendria que exportar las funciones incluidas en rxtxSerial.dll y llamarlas desde tu aplicacion HMG

http://playground.arduino.cc/Interfacing/Java

http://rxtx.qbang.org/wiki/index.php/Download
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Carlos Britos
Posts: 245
Joined: Sat Aug 02, 2008 5:03 pm

Re: harbour comms via I2C

Post by Carlos Britos »

El modelo es arduino UNO
Si la conexion se hace atraves del usb
Hasta donde se las placas ya vienen con un un usb a i2c

Estoy peleando con la instalacion de la dll y los archivos que dice http://playground.arduino.cc/Interfacing/Java me falta el serialtest
Regards/Saludos, Carlos (bcd12a)
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: harbour comms via I2C

Post by danielmaximiliano »

Hola Carlos : esto encontre
Communication
The Uno has a number of facilities for communicating with a computer, another Uno board, or other microcontrollers. The ATmega328 provides UART TTL (5V) serial communication, which is available on digital pins 0 (RX) and 1 (TX). An ATmega16U2 on the board channels this serial communication over USB and appears as a virtual com port to software on the computer. The 16U2 firmware uses the standard USB COM drivers, and no external driver is needed. However, on Windows, a .inf file is required. The Arduino Software (IDE) includes a serial monitor which allows simple textual data to be sent to and from the board. The RX and TX LEDs on the board will flash when data is being transmitted via the USB-to-serial chip and USB connection to the computer (but not for serial communication on pins 0 and 1).
el punto .inf esta aca : https://www.arduino.cc/en/Guide/Windows#toc4
A SoftwareSerial library allows serial communication on any of the Uno's digital pins.
The ATmega328 also supports I2C (TWI) and SPI communication. The Arduino Software (IDE) includes a Wire library to simplify use of the I2C bus; see the documentation for details. For SPI communication, use the SPI library.
A SoftwareSerial library : https://www.arduino.cc/en/Reference/SoftwareSerial
The ATmega328 also supports I2C (TWI) and SPI communication. The Arduino Software (IDE) includes a Wire library to simplify use of the I2C bus; see the documentation for details. For SPI communication, use the SPI library.
SPI library : https://www.arduino.cc/en/Reference/SPI
the documentation : https://www.arduino.cc/en/Reference/Wire

espero que te sirva
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Carlos Britos
Posts: 245
Joined: Sat Aug 02, 2008 5:03 pm

Re: harbour comms via I2C

Post by Carlos Britos »

Gracias Daniel, estoy tratando de entender todo esto de las comunicaciones seriales. De nuevo gracias y cualquier cosa que se te ocurra es muy bienvenida. Necesito hacer este proyecto si o si.
Regards/Saludos, Carlos (bcd12a)
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: harbour comms via I2C

Post by danielmaximiliano »

Carlos : Mas simple puede usar la libreria HBCOMM de Harbour
dejo aqui tienes un ejemplo practico y puedes convertirlo a HMG y usar otros controles mas para asignar a que dispositivo estas enviando esos datos que salen por el puerto de comunicaciones..

recuerda que estas escribiendo al puerto COMM/USBdonde esta el maestro, si indicas unos parametros secundarios es que estas escribiendo a un exclavo, cuando leas el puerto tienes que saber si es el Maestro o escalvo el que esta enviando datos.

esto que te digo es una suposicion ya que no tengo ningun arduino fisico para probar yver como se comporta..

Code: Select all

/*
 * Harbour Project source code:
 *
 * Copyright 2009 Viktor Szakats (vszakats.net/harbour)
 *    (fixed, adapted to CLI, translated, formatted)
 * Copyright 2006 Marcelo Torres <lichitorres@yahoo.com.ar>
 * www - http://harbour-project.org
 *
 */

#require "hbcomm"

STATIC s_nHandle
STATIC s_lConnected := .F.

PROCEDURE Main()

   LOCAL nOption

   DO WHILE .T.
      ? ""
      ? "Select test:"
      ? "O) Open"
      ? "C) Close"
      ? "S) Send"
      ? "R) Receive"
      ? "Q) Quit"
      ? "> "

      nOption := Inkey( 0 )
      ?? Chr( nOption )

      SWITCH Upper( Chr( nOption ) )
      CASE "O" ; FConnect() ; EXIT
      CASE "C" ; FDisconnect() ; EXIT
      CASE "S" ; FSend() ; EXIT
      CASE "R" ; FReceive() ; EXIT
      CASE "Q" ; RETURN
      ENDSWITCH
   ENDDO

   RETURN

STATIC PROCEDURE FConnect()

   LOCAL cCom       := "COM1"
   LOCAL nBaudeRate := 19200
   LOCAL nDatabits  := 8
   LOCAL nParity    := 0 /* none */
   LOCAL nStopbit   := 1
   LOCAL nBuff      := 8000

   s_nHandle := INIT_PORT( cCom, nBaudeRate, nDatabits, nParity, nStopbit, nBuff )
   IF s_nHandle > 0
      ? "Connecting..."
      s_lConnected := .T.
      OUTBUFCLR( s_nHandle )
   ELSE
      ? "Could not open connection"
      s_lConnected := .F.
   ENDIF

   RETURN

STATIC PROCEDURE FDisconnect()

   s_lConnected := .F.
   UNINT_PORT( s_nHandle )

   RETURN

STATIC PROCEDURE FSend()

   LOCAL cToSend

   ACCEPT "Enter string to send: " TO cToSend

   IF s_lConnected .AND. ! Empty( cToSend ) .AND. ISWORKING( s_nHandle )
      OUTCHR( s_nHandle, cToSend )
   ELSE
      ? "Cannot send data"
   ENDIF

   RETURN

STATIC PROCEDURE FReceive()

   LOCAL cReceive
   LOCAL nSize

   nSize := INBUFSIZE( s_nHandle )

   IF nSize > 0
      cReceive := Space( nSize )
      INCHR( s_nHandle, nSize, @cReceive )
      ? ">>", Left( cReceive, nSize )
   ENDIF

   RETURN
Last edited by danielmaximiliano on Sat Jun 18, 2016 12:17 am, edited 1 time in total.
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: harbour comms via I2C

Post by danielmaximiliano »

Carlos : necesitas hacer Software en tu Master Arduino que lea el puerto USB y el puerto I2C y dependiendo que haya datos estos sean manejados por su propia rutina Rx/Tx
[attachment=0]HMG.png[/attachment]
Attachments
HMG.png
HMG.png (173.98 KiB) Viewed 8448 times
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Post Reply