see the following, hope to help. // 档名: rs232_x.c--// 缘由:为了vigor plc modbus与pc图控软体的rs232连线// 使用说明:///*01: read coil status02: read input status03: read holding reg**ter04: read input reg**ters05: force single coil06: preset single reg**ter15: force multiple coils16: preset multiple reg**ters22: mask write reg**ter23: read/write reg**ters*/#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 1byte text="欢迎您 rs232 modbus测试程式 2006/10/19 ";// 还是取好记一点的名字较避免用 a、b、c 来取名wndclass wc;//wndclassex wc;hwnd hwnd;dcb dcb; //2006/9/29 hk// 用小写 p 开头的变数多半是指标功能。//站号为1,y0设为onbyte spd[8]={1,5,0,0,255,0,140,58}; //1,5,0,0,ff,0,8c,3a//站号为1,y0设为offbyte spdx[8]={1,5,0,0,0,0,205,202}; //1,5,0,0,0,0,cd,cabyte abcd[8]; msg msg; hdc hdc; hmenu hmenu ; handle hfile;void main(){/* wc.cbsize=sizeof(wndclassex);//无此行用reg**terclassex会看不到视窗*/ wc.hinstance = hth**inst; wc.lpszclassname = "hi" ; wc.lpfnwndproc = #wndproc ; wc.style = cs_hredraw | cs_vredraw ; wc.hbrbackground = getstockobject(white_brush); wc.hcursor = loadcursor(0,idc_cross); reg**terclass(#wc); hwnd=createwindowex(0,"hi",#text,ws_overlappedwindow, cw_usedefault,cw_usedefault,cw_usedefault,cw_usedefault, 0,0,hth**inst,0) ; settimer (hwnd, id_timer, 1000, **); hfile=createfile("com4", generic_read|generic_write, 0, **, open_ex**ting, file_attribute_normal, 0);if ( hfile == invalid_handle_value ) //判断com开启是否成功{ **box(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) ) { **box(0,"通序埠设定错误!!!","set error",mb_ok); closehandle(hfile); return; } showwindow(hwnd,1); while (get**(#msg,0,0,0)) d**patch**(#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, **, false) ; if(fflipflop) writefile(hfile,#spd,8,#abcd,**); else writefile(hfile,#spdx,8,#abcd,**); 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, **) ; 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) ; postquit**(0); break; } return defwindowproc(hwnd,msg,wparam,lparam);} 20210311