Commercial scale communication

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
zolysoftsolutions
Posts: 139
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Commercial scale communication

Post by zolysoftsolutions »

Hi my friends,
Is there somebody who solved a communication between commercial scale and software?
Can somehow to catch data from a virtual port (COM3) ?
Can somehow to send data to a virtual port (COM3) ?
Thank you very much..
_______________________________
Open eyes for you!
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Commercial scale communication

Post by mol »

What do you mean- virtual? Is it connected via USB? Ethernet? With emulated RS232?
User avatar
zolysoftsolutions
Posts: 139
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Re: Commercial scale communication

Post by zolysoftsolutions »

Virtual COM. Connected via usb.
_______________________________
Open eyes for you!
edk
Posts: 999
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Commercial scale communication

Post by edk »

IMHO, there should be no difference in communication over a physical COM port or "virtual COM"
Communication between the virtual COM port and the physical USB port is performed by the Windows system driver. I communicates without any problems with fiscal devices connected to the USB with the COM emulation in the same way as if they were connected to a physical COM port.

Code: Select all

#include "hbcom.ch"
#define cETX 	CHR(3)

nComPort := 3	//COM3
nTimeOut := 100 //ms

//to COM open
lIsOpened := HB_comOpen( nComPort )

//to Com Initialize
lIsInit := HB_ComInit( nComPort, 9600 , "N", 8, 1 )

//To send 
nBytesSended := HB_ComSend( nComPort, cAnyByteStringToSend, , nTimeOut )

//To receive
cReceiveBuffer := SPACE(512)
nBytesReceived := HB_ComRecv( nComPort, @cReceiveBuffer, , nTimeOut )
cReceivedStringByte := Left( cReceiveBuffer, nBytesReceived )

//To receive (waiting for expected the terminate string byte)
cTerminateByte := cETX
cReceivedStringByte := ""
nClock_start := Seconds()
nWaitingTime := 10 //sec

Do While Right ( cReceivedStringByte, 1 ) # cETX 
	cReceiveBuffer := Space( 512 )
	nBytesReceived := HB_ComRecv( nComPort, @cReceiveBuffer, , nTimeOut )
	cReceivedStringByte += Left( cReceiveBuffer, nBytesReceived )

	If Seconds() - nClock_start > nWaitingTime
		MsgStop( "The expected end-of-transmission mark was not received" )
		Exit
	Endif

	Do Events
EndDo


//to COM flush
Do While !HB_ComFlush( nComPort )
	Do Events
EndDo

//to COM close
lIsClosed := HB_ComClose( nComPort )

User avatar
danielmaximiliano
Posts: 2625
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: Commercial scale communication

Post by danielmaximiliano »

zolysoftsolutions wrote: Tue Jun 23, 2020 7:57 am Hi my friends,
Is there somebody who solved a communication between commercial scale and software?
Can somehow to catch data from a virtual port (COM3) ?
Can somehow to send data to a virtual port (COM3) ?
Thank you very much..
Hello:
More than 10 years ago I used this to configure the virtual scanner port.
my need was to connect the scanner to the corresponding port.
To send data, it is necessary to add the functions used by your device's library
2020-06-23 11_02_13-Window.png
2020-06-23 11_02_13-Window.png (9.98 KiB) Viewed 19532 times
Bartools.rar
(6.88 KiB) Downloaded 157 times
if you need help you can use the communication channels in my signature
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Commercial scale communication

Post by mol »

Communication via RS232 emulated on USB is the same as on real RS. No difference.
You have to know communication protocol.
What is the model of scale?
User avatar
zolysoftsolutions
Posts: 139
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Re: Commercial scale communication

Post by zolysoftsolutions »

CAS PRII 30CB usb.
I found a lot of free small software "scale to pc" and is working absolutelly fine, but i'd like to handle directly inside my application.
I will try the solution posted by EDK with HBComm. I'm curious what kind of data i will recieve.
Thank you very much.
Zoli B.
_______________________________
Open eyes for you!
edk
Posts: 999
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Commercial scale communication

Post by edk »

Hi.
I found the protocol description here: https://wagicas.pl/download/software/Pr ... otocol.pdf
I am not sure if it is compatible with this scale, it seems OK.
On its basis I prepared an example of a program. I am not sure if it works because I do not have such scale ;) , but who knows ... :roll:

Code: Select all

#include <hmg.ch>
#include "hbcom.ch"

Function Main

Local aCOMs  := {}
Private aPorts := EnumCOMs()
AEVAL (aPorts, { |x| AADD( aCOMS, x[3]) } )

#define ENQ				 	Chr(0x05)
#define ACK				 	Chr(0x06)
#define NAK				 	Chr(0x15)
#define SOH				 	Chr(0x01)
#define STX				 	Chr(0x02)
#define ETX				 	Chr(0x03)
#define EOT				 	Chr(0x04)
#define DC1				 	Chr(0x11)
#define DC2				 	Chr(0x12)
#define DC3				 	Chr(0x13)
#define DC4				 	Chr(0x14)
 
Define Window ScaleForm At 0, 0 Width 450 Height 550 Title 'HMG Scale' Main

	Define Label scalelabel
		Row 10
		Col 10
		Width 120
		Value 'Scale connected to'
	End Label

	Define Combobox comports
		Row 7
		Col 125
		Width 290
		Height 100
		Items aCOMs
		Value 1
	End Combobox
	
	
	Define Label loglabel
		Row 30
		Col 10
		Width 200
		Value 'Transmision log' 
	End Label
	
	Define Editbox _log
		Row 50
		Col 10
		Width 410
		Height 400
	End Editbox
	
	Define Button Weight
		Row 470
		Col 50
		Width 80
		Caption 'Get Weight'
		Action Get_weight()
	End Button
	
	Define Button close
		Row 470
		Col 230
		Width 80
		Caption 'Close'
		Action ScaleForm.Release()
	End Button
	      
End Window 

On Key ESCAPE Of ScaleForm Action ScaleForm.Release()
ScaleForm.Center()
ScaleForm.Activate()   
Return nil

*****************************************************
Function Get_weight
Local nComPort := aPorts [ ScaleForm.comports.Value ] [ 2 ]
Local cComPort := aPorts [ ScaleForm.comports.Value ] [ 1 ]
Local nTimeOut := 100 //ms
Local lIsOpen, lIsInit, lIsClose
Local cReceiveBuffer, nBytesReceived, cReceivedStringByte, nBytesSended, cTerminateByte
Local nClock_start, nWaitingTime := 3 //sec
Local cWeight := ""

ScaleForm.Weight.Enabled:=.F.
ScaleForm._log.Value:=""

//to COM open
ScaleForm._log.Value := ScaleForm._log.Value + "Opening port " + cComPort + CRLF
lIsOpen := HB_comOpen( nComPort )
ScaleForm._log.Value := ScaleForm._log.Value + "Port " + cComPort + " is open: " + hb_ValToStr(lIsOpen) + CRLF

//to Com Initialize
ScaleForm._log.Value := ScaleForm._log.Value + "Initializing port " + cComPort + CRLF
lIsInit := HB_ComInit( nComPort, 9600 , "N", 8, 1 )
ScaleForm._log.Value := ScaleForm._log.Value + "Port " + cComPort + " is initialized: " + hb_ValToStr(lIsInit) + CRLF


//To send ENQ 
ScaleForm._log.Value := ScaleForm._log.Value + "Sending ENQ..." + CRLF
nBytesSended := HB_ComSend( nComPort, ENQ, , nTimeOut )
ScaleForm._log.Value := ScaleForm._log.Value + "Sended " + hb_ValToStr( nBytesSended) + "  bytes" + CRLF

//To receive ACK/NAK
ScaleForm._log.Value := ScaleForm._log.Value + "Receiving..." + CRLF
cReceiveBuffer := SPACE(512)
nBytesReceived := HB_ComRecv( nComPort, @cReceiveBuffer, , nTimeOut )
cReceivedStringByte := Left( cReceiveBuffer, nBytesReceived )
ScaleForm._log.Value := ScaleForm._log.Value + "Received " + hb_ValToStr( nBytesReceived) + "  bytes" + CRLF
ScaleForm._log.Value := ScaleForm._log.Value + "Response " + IF( cReceivedStringByte = ACK, "ACK", IF (cReceivedStringByte = NAK , "NAK", cReceivedStringByte ) ) + CRLF

IF cReceivedStringByte = ACK

	//To send DC1 
	ScaleForm._log.Value := ScaleForm._log.Value + "Sending DC1..." + CRLF
	nBytesSended := HB_ComSend( nComPort, DC1, , nTimeOut )
	ScaleForm._log.Value := ScaleForm._log.Value + "Sended " + hb_ValToStr( nBytesSended) + "  bytes" + CRLF

	//To receive weight (waiting for expected the terminate string byte)
	ScaleForm._log.Value := ScaleForm._log.Value + "Receiving..." + CRLF
	cTerminateByte := ETX + EOT
	cReceivedStringByte := ""
	nClock_start := Seconds()
	
	Do While Right ( cReceivedStringByte, Len( cTerminateByte )  ) # cTerminateByte 
		cReceiveBuffer := Space( 512 )
		nBytesReceived := HB_ComRecv( nComPort, @cReceiveBuffer, , nTimeOut )
		cReceivedStringByte += Left( cReceiveBuffer, nBytesReceived )

		If Seconds() - nClock_start > nWaitingTime
			MsgStop( "The expected end-of-transmission mark was not received" )
			Exit
		Endif

		Do Events
	EndDo
	
	cWeight := StrTran ( cReceivedStringByte, SOH + STX , '')	//remove prefix commands
	cWeight := StrTran ( cWeight, cterminateByte, '')			//remove suffix commands
	cWeight := Left ( cWeight, Len (cWeight) - 1 )			//remove BCC (block check character)

	ScaleForm._log.Value := ScaleForm._log.Value + "Received weight " + cWeight + CRLF
ENDIF


//to COM flush
ScaleForm._log.Value := ScaleForm._log.Value + "Flushing port " + cComPort + CRLF
Do While !HB_ComFlush( nComPort )
	Do Events
EndDo


//to COM close
ScaleForm._log.Value := ScaleForm._log.Value + "Closing port " + cComPort + CRLF
lIsClosed := HB_ComClose( nComPort )
ScaleForm._log.Value := ScaleForm._log.Value + "Port " + cComPort + " is closed: " + hb_ValToStr(lIsClosed)

ScaleForm.Weight.Enabled:=.T.

RETURN
************************************************************
FUNCTION EnumCOMs()
Local oSerialPorts, oComs, cCaption, cCom, nPos
Local aPorts := {}

oSerialPorts := WmiService()
For Each oComs In oSerialPorts:ExecQuery( 'SELECT * FROM Win32_PnPEntity WHERE ClassGuid="{4d36e978-e325-11ce-bfc1-08002be10318}"' )		//https://docs.microsoft.com/en-us/windows-hardware/drivers/install/system-defined-device-setup-classes-available-to-vendors?redirectedfrom=MSDN
	cCaption := oComs:Caption
	IF (nPos := AT( '(COM', cCaption )) > 0
		cCom := SubStr( cCaption, nPos + 1 )
		cCom := Left ( cCom, AT( ')', cCom ) - 1 )
		AADD ( aPorts, { cCom, VAL( StrTran ( cCom, 'COM' ) ), cCaption } )
	ENDIF
Next
Return aPorts

***********************************************************************
FUNCTION WMIService()
Local oWMI, oLocator
oLocator   := win_oleCreateObject( "wbemScripting.SwbemLocator" )
oWMI       := oLocator:ConnectServer()
Return oWMI
*************************************************************************************

Last edited by edk on Fri Jun 26, 2020 1:04 pm, edited 2 times in total.
User avatar
danielmaximiliano
Posts: 2625
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: Commercial scale communication

Post by danielmaximiliano »

It also has a program to test the communication with your balance.
http://wagicas.pl/upload/Oprogramowanie ... _RS232.zip
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

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