ModBus C++ source code on DOS

A

Thread Starter

Anonymous

I´m looking for free source code (or examples) of ModBus protocol in C or C++. I would like to establish a communication between a computer (DOS) and a messure equipment, that communicates via ModBus protocol. The physical Layer is RS485 or TCP/IP.
 
<p>Hallo

<p>See the following, hope to help.

<pre>
// ÀɦW¡G RS232_x.C--
// ½t¥Ñ:¬°¤FVIGOR PLC modbus»PPC¹Ï±±³nÅ骺RS232³s½u
// ¨Ï¥Î»¡©ú:
//
/*
01: Read coil status
02: Read input status
03: Read holding register
04: Read input registers
05: Force single coil
06: Preset single register
15: Force multiple coils
16: Preset multiple registers
22: Mask write register
23: Read/Write registers
*/
#pragma option w32

#include "msvcrt.h--"
#include "windows.h--"
#include "wingdi.h--"
//2006/10/19 HK
#include "winbase.h--"
#include "winerror.h--"
#include "advapi.h--"
#include "winuser.h--"

#define NUM 50
#define NUM_1 NUM+1
#define PI 3.141592654
#define ID_TIMER 1

byte Text="Åwªï±z RS232 MODBUS´ú¸Õµ{¦¡ 2006/10/19 ";
// ÁÙ¬O¨ú¦n°O¤@ÂIªº¦W¦r¸û¦n¡AÁקK¥Î a¡Bb¡Bc ¨Ó¨ú¦W
WNDCLASS wc;
//WNDCLASSEX wc;

HWND hwnd;
DCB dcb; //2006/9/29 HK
// ¥Î¤p¼g p ¶}ÀYªºÅܼƦh¥b¬O«ü¼Ð¥\¯à¡C
//¯¸¸¹¬°1,Y0³]¬°ON
byte spd[8]={1,5,0,0,255,0,140,58}; //1,5,0,0,ff,0,8c,3a
//¯¸¸¹¬°1,Y0³]¬°OFF
byte spdx[8]={1,5,0,0,0,0,205,202}; //1,5,0,0,0,0,cd,ca
byte abcd[8];
MSG msg;
HDC hdc;
HMENU hMenu ;
HANDLE hFile;
void main()
{
/*
wc.cbSize=sizeof(WNDCLASSEX);//µL¦¹¦æ¥ÎRegisterClassEx·|¬Ý¤£¨ìµøµ¡
*/
wc.hInstance = hThisInst;
wc.lpszClassName = "Hi" ;
wc.lpfnWndProc = #WndProc ;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(0,IDC_CROSS);

RegisterClass(#wc);

hwnd=CreateWindowEx(0,"Hi",#Text,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
0,0,hThisInst,0) ;
SetTimer (hwnd, ID_TIMER, 1000, NULL);
hFile=CreateFile("Com4",
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if ( hFile == INVALID_HANDLE_VALUE ) //§PÂ_COM¶}±Ò¬O§_¦¨¥\
{
MessageBox(0,"¶}±Ò³q°T°ð ¿ù»~!!","Comm Error",MB_OK);
return;
}

//±Ndcb¦ì§}¶Ç¤J,¥H¶}±Ò³q§Ç°Ñ¼Æ
GetCommState(hFile ,#dcb ); //±oª¾¥Ø«eCOMªºª¬ºA
dcb.BaudRate = 19200; //³]©wÀj²v19200
dcb.ByteSize = 8; // 8 bits¦ì¤¸²Õ
dcb.Parity = NOPARITY; //¦P¦ì¤¸Àˬd¬°none
dcb.StopBits = ONESTOPBIT; //Stop bit =1

//³]©w³q°T°ð
if( !SetCommState(hFile,#dcb) )
{
MessageBox(0,"³q§Ç°ð³]©w¿ù»~!!!","Set Error",MB_OK);
CloseHandle(hFile);
return;
}
ShowWindow(hwnd,1);

while (GetMessage(#msg,0,0,0))
DispatchMessage(#msg);
}

POINT pt[50];

long WndProc(HWND hwnd, UINT msg,WPARAM wparam, LPARAM lparam) // µøµ¡¨ç¦¡
{
static int cxClient, cyClient ;
static POINT pt [NUM_1] ;
static BOOL fFlipFlop = FALSE ;
HBRUSH hBrush ;
int i;
float f;
PAINTSTRUCT ps ;
RECT rect ;
switch (msg)
{
case WM_TIMER :
!fFlipFlop ;
InvalidateRect (hwnd, NULL, FALSE) ;
if(fFlipFlop)
WriteFile(hFile,#spd,8,#abcd,NULL);
else
WriteFile(hFile,#spdx,8,#abcd,NULL);
return 0 ;

case WM_SIZE :
cxClient = LOWORD (lparam) ;
cyClient = HIWORD (lparam) ;
break;

case WM_PAINT :
hdc = BeginPaint (hwnd, #ps) ;
GetClientRect (hwnd, #rect) ;

if(fFlipFlop)
{
hBrush = CreateSolidBrush ( RGB(255,0,0) ) ;
FillRect (hdc,#rect, hBrush) ;
DrawText (hdc, "¯¸¸¹¬°1,Y0³]¬°ON", 16, #rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
}
else
{
hBrush = CreateSolidBrush ( RGB(0,255,0) ) ;
FillRect (hdc,#rect, hBrush) ;
DrawText (hdc, "¯¸¸¹¬°1,Y0³]¬°OFF", 17, #rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
}
MoveToEx (hdc, 0, cyClient / 2, NULL) ;
LineTo(hdc, cxClient, cyClient / 2) ;
for (i = 0 ; i <= NUM ; i++)
{
pt.x = i * cxClient /NUM;
f = sin(PI * 2 / NUM * i);
pt.y = float 1-f * cyClient/2 ;
}
Polyline (hdc, #pt,NUM_1) ;

EndPaint (hwnd, #ps) ; // ³o¦æ­ì¥»¦³¿ù³á¡I§ï¤FÅܼƦW¦r´N¬Ý¥X¨Ó¤F
DeleteObject (hBrush) ;
break;
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
</pre>
 
<p>For those of you wanting some C++ you can stick in anywhere here some code (derived from the last post). This is confirmed to work for me.
<pre>
DCB dcb;
byte byReceive[27];
byte bySend[8]={0x01,0x03,0x00,0x64,0x00,0x0B,0x45,0xD2};
DWORD dwBytesWritten;
MSG msg;
HDC hdc;
HMENU hMenu ;
HANDLE hFile;

void modbusTest()
{

hFile=CreateFile("Com4",
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if ( hFile == INVALID_HANDLE_VALUE )
{
//TODO: An error occured notify user with a message
return;
}

GetCommState(hFile ,&dcb );
dcb.BaudRate = 115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;

if( !SetCommState(hFile,&dcb) )
{
//TODO: An error occured notify user with a message
CloseHandle(hFile);
return;
}

static bool fFlipFlop = true;

while(true)
{
if(fFlipFlop == true) fFlipFlop = false;
else fFlipFlop = true;

if(fFlipFlop) {
ReadFile(hFile,&byReceive,27,&dwBytesWritten,NULL);
Sleep(1000);
}
else
WriteFile(hFile,&bySend,8,&dwBytesWritten,NULL);

}
}
</pre>
 
A
I'm looking for free source code (or examples) of ModBus protocol in vhdl or C. I would like to establish a communication between two fpga, that communicates via ModBus rtu protocol. The physical Layer is RS323 rtu.

thanks
Ankur Bagul
 
C

curt wuollet

Modbus in vhdl? now, that would be interesting. They do have some C examples on Modbus.org, or did, last time I looked which was quite
a while ago.

Regards
cww
 
Top