Byte Frame em porta serial

HMG en Español

Moderator: Rathinagiri

Post Reply
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Byte Frame em porta serial

Post by jairpinho »

a gente necesita para enviar mensajes y recibir información con esta estructura

Example #1
Query Message 02 14 07 06 10 00 00 02 00 02 (CRC16)
Where:
02 = Address
14 = Function Code 20 (14 hex)
07 = Byte Count
06 = Reference Type
10,00 = Register Address (Input Hi Range)
00,02 = File Number (Access Data Value)
00 02 = Register Count (Floating Point Data)
(CRC16)

Response Message 02 14 06 05 06 47 64 00 00 (CRC16)
Where:
02 = Address
14 = Function Code 20 (14 Hex)
06 = Byte Count
05 = Sub Message Length
06 = Reference Type
47 64 00 00 = 100.0 (Value of Input Hi Range)
(CRC16)



Alguien sabe cómo puedo hacer esto utilizando el puerto serie COM1
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
mol
Posts: 3720
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Byte Frame em porta serial

Post by mol »

If I understand you good, you can use hbcomm for serial communication.
There is a piece of my code for opening COM port, sendig and receiveing data :

Code: Select all

	cPortName := "COM1"
	nBaud := 9600
	oWinPort := win_com():Init(cPortName, nBaud, NOPARITY, 8, ONESTOPBIT)
	
    if !oWinPort:Open
        msgstop("Error opening COM port!" )
		return .F.
    else
        // COM port opened successfully
		oWinPort:QueueSize( 1000, 1000 )
		oWinPort:Purge()
		//TimeOuts( nReadInterval, nReadMultiplier, nReadConstant, nWriteMultiplier, nWriteConstant )
		oWinPort:Timeouts(1500,1000,20000,1000,20000)
		
		// set control of communication flow
        oWinPort:RTSFlow(.t.)
        oWinPort:DTRFlow(.t.)
        oWinPort:XonXoffFlow(.t.)
		
		// set modem lines states
		
        oWinPort:SetDTR(.t.)
        oWinPort:SetDTR(.t.)
        oWinPort:SetRTS(.t.)
    endif
now you can use this function to send data:

Code: Select all

	oWinPort:Write("any string to send via COM Port")
and this function for read data from serial port

Code: Select all

	cAnswer := ""
	oWinPort:Read(@cAnswer, nNumberOfBytesToRead)
User avatar
mol
Posts: 3720
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Byte Frame em porta serial

Post by mol »

I've placed two useful functions below:

Code: Select all

function ComClose
	local nCount := 0, cAnswer := ""
	
	if oWinPort <> NIL
		oWinPort:QueueStatus( , , , , , @nCount,  )
		oWinPort:Read(@cAnswer, nCount)
		oWinPort:Purge()
		oWinPort:RTSFlow(.f.)
		oWinPort:DTRFlow(.f.)
		oWinPort:XonXoffFlow(.f.)
		oWinPort:SetDTR(.f.)
		oWinPort:SetDTR(.f.)
		oWinPort:SetRTS(.f.)
		oWinPort:Close()
	endif
	oWinPort := NIL
return .t.
and second
User avatar
mol
Posts: 3720
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Byte Frame em porta serial

Post by mol »

Sorry, I don't know why a software of this forum not accept my next post...
while sending post, I get:

Code: Select all

Not Acceptable

An appropriate representation of the requested resource /posting.php could not be found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

A function to calculate CRC is in attachment:
Attachments
com.zip
(1.77 KiB) Downloaded 346 times
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Byte Frame em porta serial

Post by jairpinho »

compiled your example but not connecting on port COM1 COM2 and COM3. where I think the command that is using the serial port thanks for the help


#include <hmg.ch>

Function Main
PRivate oWinPort
Load Window Main
Inicia_port()
Main.Center
Main.Activate

Return

Function Inicia_port()
cPortName := "COM3"
nBaud := 9600
oWinPort := win_com():Init(cPortName, nBaud, .F., 8, 1)

if !oWinPort:Open
msgstop("Error opening COM port!" )
return .F.
else
// COM port opened successfully
oWinPort:QueueSize( 1000, 1000 )
oWinPort:Purge()
//TimeOuts( nReadInterval, nReadMultiplier, nReadConstant, nWriteMultiplier, nWriteConstant )
oWinPort:Timeouts(1500,1000,20000,1000,20000)

// set control of communication flow
oWinPort:RTSFlow(.t.)
oWinPort:DTRFlow(.t.)
oWinPort:XonXoffFlow(.t.)

// set modem lines states

oWinPort:SetDTR(.t.)
oWinPort:SetDTR(.t.)
oWinPort:SetRTS(.t.)
endif

Return(oWinPort)

Function Leporta()


cAnswer := ""
//oWinPort:Read(@cAnswer, nNumberOfBytesToRead)
main.text_1.Value := oWinPort:Read(@cAnswer, 8)

Return
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Byte Frame em porta serial

Post by jairpinho »

rename the file that accepts the attachment


mol wrote:Sorry, I don't know why a software of this forum not accept my next post...
while sending post, I get:

Code: Select all

Not Acceptable

An appropriate representation of the requested resource /posting.php could not be found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

A function to calculate CRC is in attachment:
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
Post Reply