您现在所在的是:

传感器论坛

回帖:0个,阅读:1347 [上一页] [1] [下一页]
811
心急如焚!!!
文章数:2
年度积分:50
历史总积分:811
注册时间:2002/7/10
发站内信
发表于:2002/7/10 17:33:00
#0楼
高手:
您好!在运用WIN API实现PC机与电度表的485串口通信编程方面,我已花费了半个月时间,但我所编写的程序仍旧不能实现PC机与外设的正常通信,近日我感到十分的困惑与迷茫,
为此,特向各位过路大侠请教。
我所编写的源程序如下。由于目前尚处于测试阶段,程序在结构方面可能不太清晰,请多包涵!
#include <vcl.h>
#pragma hdrstop

#include "test.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
HANDLE h_Comm;
HANDLE hwnd;
HANDLE h_WriteEvent;
HANDLE h_ReadEvent;
HANDLE h_ShutdownEvent;
OVERLAPPED ovWrite;
OVERLAPPED ovRead;
COMSTAT Commstat;
COMMTIMEOUTS CommTimeOuts;
CRITICAL_SECTION CriticalSection;
COMMCONFIG CommConfig;
COMMPROP CommProp;
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
       : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD buf5=0;
DWORD dwErrorflags1=0;
DWORD dwErrorflags2=0;
DWORD dwEvtMask;
BOOL ReadState;
DWORD buf4=0;
String buf1[50];
String buf3[50];
DWORD buf2=0;
DWORD dwError;
BOOL WriteState;
DCB dcb;
InitializeCriticalSection(&CriticalSection);
EnterCriticalSection(&CriticalSection);
SetEvent(h_WriteEvent);
h_WriteEvent=CreateEvent(NULL,true,false,NULL);
SetEvent(ovWrite.hEvent);
ovWrite.hEvent=CreateEvent(NULL,true,false,NULL);
SetCommMask(h_Comm, EV_RXCHAR|EV_TXEMPTY );
if (!SetCommMask)
ShowMessage("SetCommMask Error!");
h_Comm=CreateFile("COM2",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,0);
 SetupComm(h_Comm,2048,2048);
CommTimeOuts.ReadIntervalTimeout=1000;
CommTimeOuts.ReadTotalTimeoutMultiplier=1000;
CommTimeOuts.ReadTotalTimeoutConstant=1000;
CommTimeOuts.WriteTotalTimeoutMultiplier=1000;
CommTimeOuts.WriteTotalTimeoutConstant=1000;
SetCommTimeouts(h_Comm,&CommTimeOuts);
GetCommState(h_Comm,&dcb);
dcb.DCBlength=sizeof(dcb);
dcb.fBinary=true;
dcb.fParity=true;
dcb.BaudRate=1200;
dcb.ByteSize=8;
dcb.StopBits=ONESTOPBIT;
dcb.Parity=EVENPARITY;
dcb.fOutxDsrFlow=0;
SetCommState(h_Comm,&dcb);
GetCommProperties(h_Comm,&CommProp);
CommConfig.dwSize=sizeof(COMMCONFIG);
COMMCONFIG("COM2",hwnd,CommConfig);
ClearCommError(h_Comm,&dwErrorflags1,&Commstat);
editout2->Text=dwErrorflags1;
PurgeComm(h_Comm,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
WriteState=true;
Buf1[22]="68","80","09","25","01","00","00","68","04","0A","43","F3","33","33","33","33","34","35","36","34","62","16";/注释:该串16进制数用与向电表中写入一定数据
WriteState=WriteFile(h_Comm,buf1,14,&buf2,&ovWrite);
if (!WriteState)
{
dwError=GetLastError();
editout1->Text=dwError;
 ShowMessage("WriteFile Error!");
}
CloseHandle(ovWrite.hEvent);
CloseHandle(h_WriteEvent);
CloseHandle(h_Comm);
SetEvent(h_ReadEvent);
h_ReadEvent=CreateEvent(NULL,true,false,NULL);
SetEvent(ovRead.hEvent);
ovRead.hEvent=CreateEvent(NULL,true,false,NULL);
SetCommMask(h_Comm, EV_RXCHAR|EV_TXEMPTY );
if (!SetCommMask)
ShowMessage("SetCommMask Error!");
h_Comm=CreateFile("COM2",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,0);

 SetupComm(h_Comm,1024,1024);
CommTimeOuts.ReadIntervalTimeout=1000;
CommTimeOuts.ReadTotalTimeoutMultiplier=1000;
CommTimeOuts.ReadTotalTimeoutConstant=1000;
CommTimeOuts.WriteTotalTimeoutMultiplier=1000;
CommTimeOuts.WriteTotalTimeoutConstant=1000;
SetCommTimeouts(h_Comm,&CommTimeOuts);
GetCommState(h_Comm,&dcb);
dcb.fParity=true;
dcb.fBinary=false;
dcb.BaudRate=CBR_1200;
dcb.ByteSize=8;
dcb.StopBits=ONESTOPBIT;
dcb.Parity=EVENPARITY;
SetCommState(h_Comm,&dcb);
ovRead.Offset=0;
ovWrite.OffsetHigh=0;
PurgeComm(h_Comm,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
ClearCommError(h_Comm,&dwErrorflags2,&Commstat);
editout3->Text=dwErrorflags2;
CommConfig.dwSize=sizeof(COMMCONFIG);
COMMCONFIG("COM2",hwnd,CommConfig);
ReadState=false;
 Buf3[14]="68","80","09","25","01","00","00","68","01","02","43","F3","B8","16";//注释:
该串16进制数用于读取电表中的当前时间
ReadState=ReadFile(h_Comm,buf3,22,&buf4,&ovRead);
if (ReadState)
{
ShowMessage("ReadFile Right!");
editout4->Text=buf4;
}
if (!ReadState)
{
dwError=GetLastError();
 editout4->Text=dwError;
 ShowMessage("ReadFile Error!");
}
LeaveCriticalSection(&CriticalSection);
CloseHandle(ovRead.hEvent);
CloseHandle(h_ReadEvent);
CloseHandle(h_Comm);

}
//---------------------------------------------------------------------------
该程序在C++ Builder下运行的情况:ReadFile 和WriteFile的两个操作过程的GetLastError()的返回值都是997,我不理解997是何意思;当把ovWrite.hEvent 和ovRead.hEvent两个事件删去后再运行,GetLastError()的返回值变为6,同样我也不理解6是何意思。请高手仔细看看该程序的问题出在何出。
除该程序外,在API编程方面还有一个问题一直不太清楚,命令电表进行相应工作的命令数据如何从计算机的串口发送到电表中,如: 68 80 09 25 01 00 00 68 04 0A 43 F3 33 33 33 33 34 35 36 34 62 16, 该串16进制数从计算机发送到电表中的作用是修改电表时间,不知道这串数据如何发送。
另外,如哪位大侠知道有关这方面知识的好书籍或精彩网站,也不妨推荐给本人。
总之,现在不用讲,您也清楚本人的心情。各位过路大侠能帮多少忙就帮多少,本人在此非常非常非常感谢!
                                                      ——“心急如焚”!!!

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 非法和不良信息举报

工控网客服热线:0755-86369299
版权所有 工控网 Copyright©2024 Gkong.com, All Rights Reserved

46.8003