Make your own free website on Tripod.com

CAutoModbus Class Members

class CAutoModbus : public CModbus

CreateInstance - Create an automation object inside Modbus COM Server.

Connect - Opens serial port or update com port parameters.

TxRxMessage virtual CModbus implementation. Sends and receives a message from a slave.

ErrorMessage virtual CModbus implementation.Return string description of an error code.

 

ByteSize - Specifies the number of bits in the bytes transmitted and received.

BaudRate - Specifies the baud rate

ComPort - Comm serial port

FlowControl - Specifies flow control.

Parity - Specifies the parity scheme to be used.

SilentInterval - silent interval marking the begin and the end of message in ms.

StopBits - number of stop bits.

Timeout - message timeout in ms.

TransmissionMode - Specifies RTU or ASCII transmission mode.

CAutoModbus::CreateInstance

HRESULT CreateInstance(LPCSTR szMachine,LPCSTR szConnectionName);

Return Value

S_OK if function succeeds or HRESULT error code returned by win api functions.

Parameters

szMachine - name of the machine to be used, if szMachine is an empty string the function will try to create the object on the same machine, common COM automation.

szConnectionName - A string that specifies the connection's name.

Remarks

The machine name is used in when filling COSERVERINFO structure, no security features of DCOM is used. Maybe you have to enhance this function if you need to implement security features. See CoCreateInstanceEx in VC++ help for more information about creating remote objects.

The connection name can be any text , but you can only share connections among apps with the same connection name. Use , for instance , szConnectionName = "com2" for opening com2 , "com3" for opening com3 and so on.

example:

HRESULT hRes;
CAutoModbus autoModbus;
//You need to call CoInitializeEx in any thread that use automation
// this include the main thread.
hRes = ::CoInitializeEx(NULL,COINIT_MULTITHREADED);

if FAILED(hRes) {
goto ExitError;
}


//Create an object on machine with IP Address 200.251.186.30.

// If you are using win95 the server must be running before clients on other machines can create

//objects instances.


if FAILED(autoModbus.CreateInstance("200.251.186.30","com2")) {

AfxMessageBox(_T("Error creating automation object "));
goto ExitError;

}

autoModbus.ComPort(2);
autoModbus.StopBits(TWOSTOPBITS);
autoModbus.ByteSize(8);
autoModbus.FlowControl(mbFC_NONE);
autoModbus.Timeout(300);
autoModbus.SilentInterval(20);
autoModbus.TransmissionMode(mbMODE_RTU);

// this function that actually try to open a comm port.


if (!autoModbus.Connect()) {
goto ExitError;

}


pModbus= (CModbus*)&autoModbus;

//preset register 40002 to 03 in slave device 17
wError = pModbus->PresetSingleRegister(17,1,3) ;

if (CModbus::ERR_OK!=
wError) {

AfxMessageBox( pModbus->ErrorMessage(wError));

}


ExitError:
::CoUninitialize();

See also: StopBits , ByteSize , FlowControl , Timeout , SilentInterval , TransmissionMode.

CAutoModbus::Connect

BOOL Connect();

Return Value

TRUE if function succeeds or FALSE if fails.

Remarks

Calling "Connect" actually try to open a comm port. The comm parameters StopBits , ByteSize , FlowControl , Timeout ... must be set before calling this method.

See also: CreateInstance

 

CAutoModbus::ByteSize

void ByteSize(WORD wSize);
WORD ByteSize();

Remarks

Specifies the number of bits in the bytes transmitted and received. RTU uses 8 and ASCII 7.

See also: CreateInstance

CAutoModbus::BaudRate

DWORD BaudRate(); //actual baudrate value
void BaudRate(DWORD dwBaudRate);

Remarks

Specifies the baud rate , 300 , 1200 , 9600. You can use the following windows constants:
CBR_110 CBR_19200
CBR_300 CBR_38400
CBR_600 CBR_56000
CBR_1200 CBR_57600
CBR_2400 CBR_115200
CBR_4800 CBR_128000
CBR_9600 CBR_256000
CBR_14400

See also: CreateInstance

 

 

CAutoModbus::ComPort

BYTE ComPort();
void ComPort(BYTE byComPort);

Remarks

Specifies the Comm serial port 1 to 8.

See also: CreateInstance

CAutoModbus::FlowControl

BYTE FlowControl();
void FlowControl(BYTE byFlowControl);

Remarks

Specifies the flow control. This member can a combination of the following values:
mbFC_NONE No Flow control
mbFC_DTRDSR Hardware Flow Control DTR/CTS
mbFC_RTSCTS Hardware Flow Control DTR/CTS
mbFC_XONXOFF Software Flow Control XON/XOFF

If you use , for instance , software and hardware flow control set this member as mbFC_RTSCTS+mbFC_XONXOFF.

See also: CreateInstance

CAutoModbus::Parity

BYTE Parity();
void Parity(BYTE byParity);

Remarks

Specifies the parity scheme to be used.
Value Meaning
EVENPARITY Even
MARKPARITY Mark
NOPARITY No parity
ODDPARITY Odd
SPACEPARITY Space

See also: CreateInstance

CAutoModbus::SilentInterval

DWORD SilentInterval() const;
void SilentInterval(DWORD dwSilentInterval);

Remarks

Silent interval marking the begin and the end of a message in ms.

RTU messages start and end with a silent interval of at least 3.5 character times. For instance , for baud rate of 9600 bps silent interval is 4 ms , but when doing some practical tests this value was increased to 20 ms for correct communication with devices, try to increase this value if you get many communication errors.

When using ASCII set silent interval 500 or 1000 ms.

See also: CreateInstance

CAutoModbus::StopBits

BYTE StopBits();
void StopBits(BYTE byStopBits);

Remarks

Specifies the number of stop bits to be used.

Value Meaning
ONESTOPBIT 1 stop bit
ONE5STOPBITS 1.5 stop bits
TWOSTOPBITS 2 stop bits

Use 1(ONESTOPBIT) stop bit if parity is used and 2(TWOSTOPBITS) bits if no parity.

See also: CreateInstance

CAutoModbus::Timeout

DWORD Timeout() const;
void Timeout(DWORD dwTimeout);

Remarks

Time-out period for slave response in ms.

See also: CreateInstance

 

CAutoModbus::TransmissionMode

WORD TransmissionMode();
void TransmissionMode(WORD wMode);

Remarks

Modbus use either of two transmission modes: ASCII or RTU. This member can be one of the values below:

mbMODE_RTU RTU transmission mode
mbMODE_ASCII ASCII transmission mode

See also: CreateInstance