// 档名: RS232_x.C--
// 缘由:为了VIGOR PLC modbus与PC图控软体的RS232连线
// 使用说明:
//
/*
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 NUM50
#define NUM_1NUM+1
#define PI 3.141592654
#define ID_TIMER1
byte Text="欢迎您 RS232 MODBUS测试程式 2006/10/19 "
// 还是取好记一点的名字较好,避免用 a、b、c 来取名
WNDCLASS wc
//WNDCLASSEX wc
HWND hwnd
DCB dcb //2006/9/29 HK
// 用小写 p 开头的变数多半是指标功能。
//站号为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)//无此行用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 ) //判断COM开启是否成功
{
MessageBox(0,"开启通讯埠 错误!!","Comm Error",MB_OK)
return
}
//将dcb位址传入,以开启通序参数
GetCommState(hFile ,#dcb )//得知目前COM的状态
dcb.BaudRate = 19200//设定鲍率19200
dcb.ByteSize = 8// 8 bits位元组
dcb.Parity = NOPARITY//同位元检查为none
dcb.StopBits = ONESTOPBIT//Stop bit =1
//设定通讯埠
if( !SetCommState(hFile,#dcb) )
{
MessageBox(0,"通序埠设定错误!!!","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[i].x = i * cxClient /NUM
f = sin(PI * 2 / NUM * i)
pt[i].y = float 1-f * cyClient/2
}
Polyline (hdc, #pt,NUM_1)
EndPaint (hwnd, #ps) // 这行原本有错喔!改了变数名字就看出来了
DeleteObject (hBrush)
break
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER)
PostQuitMessage(0)
break
}
return DefWindowProc(hwnd,msg,wparam,lparam)
}
modbus程序应包含通讯模块,modbus读写指令生成模块,人机交互等主要组成部分。通过人机交互,设定前端设备id,以及通讯参数,寄存器地址等,通过modbus指令生成模块生成指令,并将指令通过通讯模块送出,并接收返回数据 ,数据解析后通过人机交互窗口显示。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)