FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Conexi贸n con balanza a trav茅s del puerto RS232 (SOLUCIONADO)
Posts: 479
Joined: Fri Feb 16, 2007 10:29 AM
Conexi贸n con balanza a trav茅s del puerto RS232 (SOLUCIONADO)
Posted: Tue Mar 10, 2020 08:07 PM
Compa帽eros, buenas noches:

Necesito, por favor, de vuestra ayuda. Llevo varias horas intentando conectar con una balanza a trav茅s del puerto RS232 y pero la balanza NO me reporta ning煤n dato.

Mi c贸digo copiado y adaptado de otro hilo de este foro:
Code (fw): Select all Collapse
 
聽......
聽nComm:=OpenComm( nPuerto, 1024, 256)
聽IF nComm >= 0
聽 else
聽 聽msgalert('ERROR al NO encontrar el puerto '+nPuerto,'error...') ; return .f.
聽endif
聽If !BuildCommDcb( nPuerto+":"+alltrim(str(nbaudios))+","+nParidad+","+alltrim(str(nbitdatos))+","+alltrim(str(nbitparad)) , @cDcb )
聽 nError := GetCommError( nComm )
聽 Msgalert( "ERROR al Configurar el puerto "+nPuerto,'Error de conexi贸n con el puerto...') ; return .f.
聽endif
聽if ! SetCommState( nComm, cDcb )
聽 nError = GetCommError( nComm )
聽 Msgalert( "Error al setear Puerto : " + Str( nError ),'Aviso de error...' ) ; return .f.
聽endif 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽&& <-------- HASTA AQU脥 TODO PERFECTO !!! 聽 聽 聽 
聽ncomand="chr(0X44)" 
聽if ( nBytes := WriteComm( nComm, ncomand +Chr( 10 ) )) < 0 聽 聽 聽&& <------- HE PROBADO TAMBIEN INSERTANDO CHR(13) Y CHR(13)+CHR(10). 聽AQU脥 EST脕 EL PROBLEMA
聽 聽nError = GetCommError( nComm )
聽 聽Msgalert( "Error al realizar la petici贸n a la b谩scula : " + Str( nError ),'Aviso de error...' )
聽else
聽 聽DEFINE DIALOG oDlg TITLE "" OF oWPpal FROM 1,1 TO 12,44 STYLE nOr( WS_THICKFRAME, WS_POPUP )
聽 聽 @ 2,2 SAY oText PROMPT "Capturando el peso de la b谩scula... 聽 聽[ Salir: tecla ESC ]" OF oDlg
聽 聽ACTIVATE DIALOG oDlg NOWAIT CENTER
聽 聽Inkey(1)
聽endif
聽cBuffer:=Space(100)
聽nBytes := ReadComm (nComm,@cBuffer)
聽If FlushComm( nComm, 0 ) != 0
聽 聽nError = GetCommError( nComm )
聽 聽Msgalert("Error al vaciar el puerto de la b谩scula : " + Str( nError ),'Aviso de error...' )
聽endif
聽if ! CloseComm( nComm )
聽 聽nError = GetCommError( nComm )
聽 聽Msgalert( "Error al cerrar el puerto de la b谩scula : " + Str( nError ) )
聽endif
聽odlg:end()
? 'lectura balanza: '+cBuffer
聽 cKilos :=Substr(cBuffer,nPosIniEn,nPosFinEn-nPosIniEn)
聽 cGramos:=Substr(cBuffer,nPosIniDe,nposFinDe-nPosIniDe)
聽 nPesoBascula:=Val(Alltrim(cKilos)+"."+Alltrim(cGramos)) 聽; onPesoBascula:Refresh()
聽 SysRefresh()
return


La lectura de la balanza es siempre la misma: NADA, es decir cBuffer=space(0)
NO muestra problemas a la hora de configurar el puerto, conectar ni setear. Creo que el problema est谩 a la hora de hacer la petici贸n a la b谩scula (comando WriteComm)

La balanza es marca GRAM modelo ZFOC y seg煤n su manual instrucciones el protocolo de comunicaci贸n via RS232 es el siguiente:
1. El PC transmite 0X44
2. El PC transmite el package una vez recibida la respuesta de la balanza 0x02
3. PC transmite el comando una vez recibida la respuesta de la balanza 0x02
4. Despues de que el PC reciba los datos de la balanza 0x02 , si se precisa transmitir el comando switch a c
5. PC envia el final del package
6. PC finaliza despues de recibir la respuesta 0x02

Comenzo del package, fixed 6 bytes: 11 00 00 00 00 EF5
Final del package, fixed 6 bytes: 33 00 00 00 00 CD

Por favor, necesito ayuda...

Mil gracias!!
LORENZO.
Posts: 115
Joined: Fri Oct 14, 2005 07:10 PM
Re: Conexi贸n con balanza a trav茅s del puerto RS232
Posted: Tue Mar 10, 2020 09:20 PM
Lorenzo, alguna vez este c贸digo me sirvi贸 para lo que tu quieres.

Code (fw): Select all Collapse
聽STATIC FUNCTION LeePeso(aVars)
*-----------------------------------------
/* Rutina de lectura de Balanza
// Parametros de transmision de datos a Balanza
聽 聽nComPort 聽 聽:= 1
聽 聽nBaudRate 聽 := 9600
聽 聽cParity 聽 聽 := "N"
聽 聽nDataLen 聽 聽:= 8
聽 聽nStopBits 聽 := 2
*/

聽 聽cBuffer 聽 聽 := SPACE(20)
聽 聽cString 聽 聽 := ""
聽 聽nPesoLeido 聽:= 0


聽 聽cRetOpen := CommOpen(aVars, cComPort, cBaudRate) // Abro COM

聽 聽IF .NOT. EMPTY(cRetOpen)
聽 聽 聽 MsgInfo(cRetOpen)
聽 聽 聽 RETURN 0
聽 聽ENDIF

聽 聽IF FlushComm( nComm, 0 ) != 0
聽 聽 聽 nError = GetCommError( nComm )
聽 聽 聽 MsgInfo( "FlushComm Error: " + Str( nError ) )
聽 聽ENDIF

聽 聽nTry=0
聽 聽do while nTry<10 // can remove this nTry if not applicable
聽 聽 聽 if ( nBytes := ReadComm( nComm, @cBuffer) ) > 0
聽 聽 聽 聽 聽 聽MsgWait ("...", , .1)
聽 聽 聽 聽 聽exit
聽 聽 聽 else
聽 聽 聽 聽 聽nTry++
聽 聽 聽 聽 聽MsgWait ("...", ,.1)
聽 聽 聽 endif
聽 聽 聽 if nTry==10
聽 聽 聽 聽 聽exit
聽 聽 聽 endif
聽 聽enddo

// Extraigo 6 carateres anteriores a ultimo CHR(13) o CR o 聽CHR(75) Letra K

聽 聽DO CASE
聽 聽 聽 CASE nTipoBal = 1 聽 聽 聽 聽// CAIR Pesaje y chicas Kretz
聽 聽 聽 聽 聽cString 聽:= SUBSTR( cBuffer,( RAT(CHR(13),cBuffer) - 6), 6) 聽// Enter
聽 聽 聽 CASE nTipoBal = 2 聽 聽 聽 聽// CAIR Amaraillo
聽 聽 聽 聽 聽cString 聽:= SUBSTR( cBuffer,( RAT(CHR(75),cBuffer) - 6), 6) 聽// Letra K
聽 聽 聽 CASE nTipoBal = 3 聽 聽 聽 // Trebol
聽 聽 聽 聽 聽cString 聽:= SUBSTR( cBuffer,2, 3)+"."+SUBSTR( cBuffer,5, 3) 聽// D003450 = 3 enteros con 3 decimales
聽 聽 聽 CASE nTipoBal = 4 聽 聽 聽// 聽CAIR LR-22
聽 聽 聽 聽 聽cString 聽:= SUBSTR( cBuffer,2, 4)+"."+SUBSTR( cBuffer,6, 2) 聽// D003450 = 4 enteros con 2 decimales
聽 聽ENDCASE

聽 聽nPesoLeido 聽:= VAL(cString)

聽 聽IF FlushComm( nComm, 0 ) != 0
聽 聽 聽 nError = GetCommError( nComm )
聽 聽 聽 MsgInfo( "FlushComm Error: " + Str( nError ) )
聽 聽ENDIF

聽 聽IF ! CloseComm( nComm )
聽 聽 聽 nError = GetCommError( nComm )
聽 聽 聽 MsgInfo( "CloseComm Error: " + Str( nError ) )
聽 聽ENDIF

聽 聽RETURN nPesoLeido

聽STATIC Function CommOpen ( aVars, cPort, cSpeed )
*----------------------------------------------------------------
聽 聽LOCAL cDcb, cRv := '', cS := 'COM' + cPort + ':' + cSpeed + ',' + 聽cParity + ',' + 聽cDataLen + ',' + 聽cStopBits

聽 聽nComm := OpenComm( 'COM' + cPort, 1024 , 128 )

聽 聽IF nComm >= 0 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // We have a valid port...
聽 聽 聽 IF BuildCommDcb( cS, @cDcb ) // Set Speed, etc
聽 聽 聽 聽 聽IF SetCommState( nComm, cDcb )
聽 聽 聽 聽 聽 聽 cRv := ''
聽 聽 聽 聽 聽ELSE
聽 聽 聽 聽 聽 聽 cRv := 'Fall贸 SetCommState en COM' + cPort + ' a ' + cSpeed
聽 聽 聽 聽 聽ENDIF
聽 聽 聽 ELSE
聽 聽 聽 聽 聽cRv := 'Fall贸 BuildCommDCB en COM' + cPort + ' a ' + cSpeed
聽 聽 聽 ENDIF
聽 聽ELSE
聽 聽 聽 cRv := 'Fall贸 al buscar COM' + cPort
聽 聽ENDIF

聽 聽Return cRv
Saludos,

Gonzalo

Puerto Montt - CHILE

FWH 22.12 - Harbour 3.2.0dev (r2008190002) - Borland/Embarcadero C++ 7.0(32-bit)
Posts: 1816
Joined: Wed Oct 26, 2005 02:49 PM
Re: Conexi贸n con balanza a trav茅s del puerto RS232
Posted: Tue Mar 10, 2020 10:34 PM
Este c贸digo nos funciona perfectamente, tambi茅n fue recuperado de este foro

Code (fw): Select all Collapse
nComm := AbrePuertoCom()
Sleep(250) //1000 = 1 segundo
cBloque := LeePuertoCom( nComm,31 ) 
Sleep(250) //1000 = 1 segundo
CloseComm( nComm )

STATIC FUNCTION AbrePuertoCom( cPuerto )
LOCAL nComm, cDcb, nError
Local vGVelo := "9600" //siempre es la misma para nuestra bascula

//BUSCAMOS EL PRTO DISPONIBLE EN LA DBF
SELE d_comm
Dbgotop()
cPuerto := d_comm->PUERTO

nComm := OpenComm( "COM" + cPuerto + ":"+vGVelo+",N,8,1",2048,512)

If nComm <= 0
nError := GetCommError( nComm )
MsgInfo( "Error al abrir bascula: " + Str( nError ) )
EndIf

If ! BuildCommDcb( "COM" + cPuerto + ":"+vGVelo+",n,8,1" , @cDcb )
nError := GetCommError( nComm )
MsgInfo( "Error al Configurar bascula: " + Str( nError ) )
Return 0
EndIf

If ! SetCommState( nComm, cDcb )
nError := GetCommError( nComm )
MsgInfo( "Error al setear bascula: " + Str( nError ) )
Return 0
EndIf

Return nComm

STATIC FUNCTION LeePuertoCom( nComm,n )
LOCAL cBloque := Space( n ), bcc1, nBytes, i :=1, nWBytes, c
Do While .t.
聽 聽 nBytes := ReadComm( nComm, @cBloque )
聽 聽 i++
聽 聽 If Empty( cBloque )
聽 聽 聽 聽 MsgWait( "Leyendo b谩scula...","Espere",0.1 )
聽 聽 聽 聽 If ( nWBytes := WriteComm( nComm, Chr( 80 ) ) ) <= 0 // <------ aqui le envias el codigo para que te devuelva el peso
聽 聽 聽 聽 聽 聽 MsgStop( "Error en la b谩scula al enviar datos","Error" )
聽 聽 聽 聽 聽 聽 Return " "
聽 聽 聽 聽 EndIf
聽 聽 EndIf
聽 聽 If i > 20 .or. cBloque <> Space( n )
聽 聽 聽 聽 Exit
聽 聽 EndIf
EndDo
Return cBloque
Saludos
LEANDRO AREVALO
Bogot谩 (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 25.01 ] [ xHarbour 64 bits) ]
Posts: 479
Joined: Fri Feb 16, 2007 10:29 AM
Re: Conexi贸n con balanza a trav茅s del puerto RS232 (SOLUCIONADO)
Posted: Wed Mar 11, 2020 01:09 PM

Gracias por contestar, compa帽eros.

El problema estaba en la balanza que habia que configurarla seg煤n "protocolo dollar". De esta manera, solo hay que enviar un "$" para que responda con el peso. NI en el manual de la balanza viene esa indicaci贸n... tuve que ponerme en contacto con el equipo SAT del fabricante.

WriteComm( nComm, '$' ) ) <= 0

Mil gracias, por todo!!

saludos.
LORENZO.

Continue the discussion