10 个解决方案
#1
如果你是做控制系统那么你的通信协议要与控制单元的编程者共同商定!
#2
使用为软的标准通讯控件,mscomm.ocx,可以设置rate,端口,可直接读写input/output属性,即可发送/接受数据
#3
本人提意,最好不要用mscomm,而用一些API来做,如CreateFile等端口控件函数比较好!
#4
See: http://www.csdn.net/expert/topic/239/239441.shtm
#5
原来是南京的小妹?
留个e-mail我发一分程序给你.
南京的小哥~v~
留个e-mail我发一分程序给你.
南京的小哥~v~
#6
MSCOMM.ocx怎么往BCB里装?我怎么也用不了。
#7
装async控件吧,及其好用,免费使用,但源码就要钱了,有详细的帮助文件。
下载:URL http://www.torry.net/vcl/comms/modems/asyncd6.zip (for delphi)
http://www.torry.net/vcl/comms/modems/async32b5.zip (for cb5)
下载:URL http://www.torry.net/vcl/comms/modems/asyncd6.zip (for delphi)
http://www.torry.net/vcl/comms/modems/async32b5.zip (for cb5)
#8
to cwerror:
非常感谢:nj_lyf@263.net
你的E_mail呢?
非常感谢:nj_lyf@263.net
你的E_mail呢?
#9
如果协议比较间单可以做成函数,但最好做成一个子系统,如果是个几百页的协议,一个函数是不可能做到的。
#10
nj_lyf:
你的邮箱有问题:nj_lyf@263.net
我无法发送邮件:
/*
to nj_lyf:
我现在给你发送的仅是端口的初始化,关于通讯协议的问题,因为
我没还不清楚你在使用什么协议,是端口协议还是其他方式规定的协议
因为各种的原语描述不同,但这主要是数据的处理,我以前做电力系统
通讯的其中协议就有十几种。
关系到具体的你可以e-mail me :error_c_cn@sina.com
*/
//---------------------------------------------------------------------------
// cw_error write for jcj to try moxa_modem,
// create serieres port and more threads to used it,
// copyright of cw_error, 2000-may and cw_error at writing "man and nature"
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// Define may be used at same time,
// extern TForm2 *Form2;
HANDLE hcom;
HANDLE xModemThreadHandle;
DWORD idxModemThread;
// Define one extern thread and used serier port,
// extern void xModemThread();
//---------------------------------------------------------------------------
bool __fastcall TForm1::InitPort(AnsiString PortName)
{
_DCB myDCB;
COMMTIMEOUTS myTMO;
bool Result;
// simply define communication handle,if existing and closing
// renew to create,
if(hcom != 0/*INVALID_HANDLE_VALUE*/)
CloseHandle(hcom);
//create communication handle,
hcom=CreateFile(PortName.c_str(), // port name
GENERIC_READ|GENERIC_WRITE, // RW access mode
0, // exclusive mode
NULL, // no security attributes
OPEN_EXISTING, // open existing port
0, // not overlapped
0);
Result = (hcom!=INVALID_HANDLE_VALUE);
if(!Result)
MessageDlg("not open port:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0);
else
{
//set communlcation mask,set of events to be monitored,
if(!SetCommMask(hcom,EV_RXFLAG))
{
if(MessageDlg("not set mask:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//initializes the communications parameters,
if(!SetupComm(hcom,4096,4096))//4096 is specifies send and recvieve size,
{
if(MessageDlg("not setup communiation:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//get the communications state and DCB struct,
if(!GetCommState(hcom,&myDCB))
{
if(MessageDlg("not get state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set dcb state,include baudrate/bytebit/stopbit/parity,
myDCB = GetDCBParater(myDCB);
// if(!BuildCommDCB("COM1:baud=1200 parity=N data=8 stop=1",&myDCB));
// { 以上个的函数用法同下 }
if(!SetCommState(hcom,&myDCB))
{
//unsigned long TTT = GetLastError();
if(MessageDlg("not set state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
if(!GetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not get out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set out time function and coeff.
myTMO.ReadIntervalTimeout = MAXDWORD;
myTMO.ReadTotalTimeoutMultiplier = 0;
myTMO.ReadTotalTimeoutConstant = 0;
myTMO.WriteTotalTimeoutMultiplier =0;
myTMO.WriteTotalTimeoutConstant =1000;
if(!SetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not set out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//API_Function: clear send buffer,
PurgeComm(hcom,PURGE_TXCLEAR);
//API-Function: clear recvive buffer,
PurgeComm(hcom,PURGE_RXCLEAR);
}
return Result;
}
bool __fastcall TForm1::RecvData()
{
bool Result;
AnsiString ReadStr;
unsigned char ReadBuffer[100];
unsigned long ReadNum;
memset(ReadBuffer,0,sizeof(ReadBuffer));
Result = ReadFile(hcom,ReadBuffer,sizeof(ReadBuffer),&ReadNum,NULL);
Form1->RECommand->Lines->Add("Receive:\r"+ReadStr );
if(Result)
{
for(int i=0;i<sizeof(ReadBuffer);i++)
ReadStr = ReadStr + (char)ReadBuffer[i];
if(sizeof(ReadStr) == 4)
ReadStr = "0";
}
RECommand->Lines->Add(ReadStr );
return 1;
}
//在这里我发送的是字符串。
bool __fastcall TForm1::SendStrToPort(AnsiString ReadStr)
{
bool Result;
unsigned char B[100];
unsigned long SendNum;
for(int i=1;i<=ReadStr.Length();i++)
{
B[i-1]= (int)ReadStr[i];
}
SendNum = ReadStr.Length();
WriteFile(hcom,B,SendNum,&SendNum,NULL);
if(!WriteFile(hcom,B,SendNum,&SendNum,NULL))
{
Result = false;
if(Application->MessageBox("without write", NULL, MB_OKCANCEL) != IDOK)
exit(1);
}
return Result;
}
你的邮箱有问题:nj_lyf@263.net
我无法发送邮件:
/*
to nj_lyf:
我现在给你发送的仅是端口的初始化,关于通讯协议的问题,因为
我没还不清楚你在使用什么协议,是端口协议还是其他方式规定的协议
因为各种的原语描述不同,但这主要是数据的处理,我以前做电力系统
通讯的其中协议就有十几种。
关系到具体的你可以e-mail me :error_c_cn@sina.com
*/
//---------------------------------------------------------------------------
// cw_error write for jcj to try moxa_modem,
// create serieres port and more threads to used it,
// copyright of cw_error, 2000-may and cw_error at writing "man and nature"
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// Define may be used at same time,
// extern TForm2 *Form2;
HANDLE hcom;
HANDLE xModemThreadHandle;
DWORD idxModemThread;
// Define one extern thread and used serier port,
// extern void xModemThread();
//---------------------------------------------------------------------------
bool __fastcall TForm1::InitPort(AnsiString PortName)
{
_DCB myDCB;
COMMTIMEOUTS myTMO;
bool Result;
// simply define communication handle,if existing and closing
// renew to create,
if(hcom != 0/*INVALID_HANDLE_VALUE*/)
CloseHandle(hcom);
//create communication handle,
hcom=CreateFile(PortName.c_str(), // port name
GENERIC_READ|GENERIC_WRITE, // RW access mode
0, // exclusive mode
NULL, // no security attributes
OPEN_EXISTING, // open existing port
0, // not overlapped
0);
Result = (hcom!=INVALID_HANDLE_VALUE);
if(!Result)
MessageDlg("not open port:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0);
else
{
//set communlcation mask,set of events to be monitored,
if(!SetCommMask(hcom,EV_RXFLAG))
{
if(MessageDlg("not set mask:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//initializes the communications parameters,
if(!SetupComm(hcom,4096,4096))//4096 is specifies send and recvieve size,
{
if(MessageDlg("not setup communiation:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//get the communications state and DCB struct,
if(!GetCommState(hcom,&myDCB))
{
if(MessageDlg("not get state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set dcb state,include baudrate/bytebit/stopbit/parity,
myDCB = GetDCBParater(myDCB);
// if(!BuildCommDCB("COM1:baud=1200 parity=N data=8 stop=1",&myDCB));
// { 以上个的函数用法同下 }
if(!SetCommState(hcom,&myDCB))
{
//unsigned long TTT = GetLastError();
if(MessageDlg("not set state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
if(!GetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not get out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set out time function and coeff.
myTMO.ReadIntervalTimeout = MAXDWORD;
myTMO.ReadTotalTimeoutMultiplier = 0;
myTMO.ReadTotalTimeoutConstant = 0;
myTMO.WriteTotalTimeoutMultiplier =0;
myTMO.WriteTotalTimeoutConstant =1000;
if(!SetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not set out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//API_Function: clear send buffer,
PurgeComm(hcom,PURGE_TXCLEAR);
//API-Function: clear recvive buffer,
PurgeComm(hcom,PURGE_RXCLEAR);
}
return Result;
}
bool __fastcall TForm1::RecvData()
{
bool Result;
AnsiString ReadStr;
unsigned char ReadBuffer[100];
unsigned long ReadNum;
memset(ReadBuffer,0,sizeof(ReadBuffer));
Result = ReadFile(hcom,ReadBuffer,sizeof(ReadBuffer),&ReadNum,NULL);
Form1->RECommand->Lines->Add("Receive:\r"+ReadStr );
if(Result)
{
for(int i=0;i<sizeof(ReadBuffer);i++)
ReadStr = ReadStr + (char)ReadBuffer[i];
if(sizeof(ReadStr) == 4)
ReadStr = "0";
}
RECommand->Lines->Add(ReadStr );
return 1;
}
//在这里我发送的是字符串。
bool __fastcall TForm1::SendStrToPort(AnsiString ReadStr)
{
bool Result;
unsigned char B[100];
unsigned long SendNum;
for(int i=1;i<=ReadStr.Length();i++)
{
B[i-1]= (int)ReadStr[i];
}
SendNum = ReadStr.Length();
WriteFile(hcom,B,SendNum,&SendNum,NULL);
if(!WriteFile(hcom,B,SendNum,&SendNum,NULL))
{
Result = false;
if(Application->MessageBox("without write", NULL, MB_OKCANCEL) != IDOK)
exit(1);
}
return Result;
}
#1
如果你是做控制系统那么你的通信协议要与控制单元的编程者共同商定!
#2
使用为软的标准通讯控件,mscomm.ocx,可以设置rate,端口,可直接读写input/output属性,即可发送/接受数据
#3
本人提意,最好不要用mscomm,而用一些API来做,如CreateFile等端口控件函数比较好!
#4
See: http://www.csdn.net/expert/topic/239/239441.shtm
#5
原来是南京的小妹?
留个e-mail我发一分程序给你.
南京的小哥~v~
留个e-mail我发一分程序给你.
南京的小哥~v~
#6
MSCOMM.ocx怎么往BCB里装?我怎么也用不了。
#7
装async控件吧,及其好用,免费使用,但源码就要钱了,有详细的帮助文件。
下载:URL http://www.torry.net/vcl/comms/modems/asyncd6.zip (for delphi)
http://www.torry.net/vcl/comms/modems/async32b5.zip (for cb5)
下载:URL http://www.torry.net/vcl/comms/modems/asyncd6.zip (for delphi)
http://www.torry.net/vcl/comms/modems/async32b5.zip (for cb5)
#8
to cwerror:
非常感谢:nj_lyf@263.net
你的E_mail呢?
非常感谢:nj_lyf@263.net
你的E_mail呢?
#9
如果协议比较间单可以做成函数,但最好做成一个子系统,如果是个几百页的协议,一个函数是不可能做到的。
#10
nj_lyf:
你的邮箱有问题:nj_lyf@263.net
我无法发送邮件:
/*
to nj_lyf:
我现在给你发送的仅是端口的初始化,关于通讯协议的问题,因为
我没还不清楚你在使用什么协议,是端口协议还是其他方式规定的协议
因为各种的原语描述不同,但这主要是数据的处理,我以前做电力系统
通讯的其中协议就有十几种。
关系到具体的你可以e-mail me :error_c_cn@sina.com
*/
//---------------------------------------------------------------------------
// cw_error write for jcj to try moxa_modem,
// create serieres port and more threads to used it,
// copyright of cw_error, 2000-may and cw_error at writing "man and nature"
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// Define may be used at same time,
// extern TForm2 *Form2;
HANDLE hcom;
HANDLE xModemThreadHandle;
DWORD idxModemThread;
// Define one extern thread and used serier port,
// extern void xModemThread();
//---------------------------------------------------------------------------
bool __fastcall TForm1::InitPort(AnsiString PortName)
{
_DCB myDCB;
COMMTIMEOUTS myTMO;
bool Result;
// simply define communication handle,if existing and closing
// renew to create,
if(hcom != 0/*INVALID_HANDLE_VALUE*/)
CloseHandle(hcom);
//create communication handle,
hcom=CreateFile(PortName.c_str(), // port name
GENERIC_READ|GENERIC_WRITE, // RW access mode
0, // exclusive mode
NULL, // no security attributes
OPEN_EXISTING, // open existing port
0, // not overlapped
0);
Result = (hcom!=INVALID_HANDLE_VALUE);
if(!Result)
MessageDlg("not open port:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0);
else
{
//set communlcation mask,set of events to be monitored,
if(!SetCommMask(hcom,EV_RXFLAG))
{
if(MessageDlg("not set mask:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//initializes the communications parameters,
if(!SetupComm(hcom,4096,4096))//4096 is specifies send and recvieve size,
{
if(MessageDlg("not setup communiation:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//get the communications state and DCB struct,
if(!GetCommState(hcom,&myDCB))
{
if(MessageDlg("not get state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set dcb state,include baudrate/bytebit/stopbit/parity,
myDCB = GetDCBParater(myDCB);
// if(!BuildCommDCB("COM1:baud=1200 parity=N data=8 stop=1",&myDCB));
// { 以上个的函数用法同下 }
if(!SetCommState(hcom,&myDCB))
{
//unsigned long TTT = GetLastError();
if(MessageDlg("not set state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
if(!GetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not get out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set out time function and coeff.
myTMO.ReadIntervalTimeout = MAXDWORD;
myTMO.ReadTotalTimeoutMultiplier = 0;
myTMO.ReadTotalTimeoutConstant = 0;
myTMO.WriteTotalTimeoutMultiplier =0;
myTMO.WriteTotalTimeoutConstant =1000;
if(!SetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not set out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//API_Function: clear send buffer,
PurgeComm(hcom,PURGE_TXCLEAR);
//API-Function: clear recvive buffer,
PurgeComm(hcom,PURGE_RXCLEAR);
}
return Result;
}
bool __fastcall TForm1::RecvData()
{
bool Result;
AnsiString ReadStr;
unsigned char ReadBuffer[100];
unsigned long ReadNum;
memset(ReadBuffer,0,sizeof(ReadBuffer));
Result = ReadFile(hcom,ReadBuffer,sizeof(ReadBuffer),&ReadNum,NULL);
Form1->RECommand->Lines->Add("Receive:\r"+ReadStr );
if(Result)
{
for(int i=0;i<sizeof(ReadBuffer);i++)
ReadStr = ReadStr + (char)ReadBuffer[i];
if(sizeof(ReadStr) == 4)
ReadStr = "0";
}
RECommand->Lines->Add(ReadStr );
return 1;
}
//在这里我发送的是字符串。
bool __fastcall TForm1::SendStrToPort(AnsiString ReadStr)
{
bool Result;
unsigned char B[100];
unsigned long SendNum;
for(int i=1;i<=ReadStr.Length();i++)
{
B[i-1]= (int)ReadStr[i];
}
SendNum = ReadStr.Length();
WriteFile(hcom,B,SendNum,&SendNum,NULL);
if(!WriteFile(hcom,B,SendNum,&SendNum,NULL))
{
Result = false;
if(Application->MessageBox("without write", NULL, MB_OKCANCEL) != IDOK)
exit(1);
}
return Result;
}
你的邮箱有问题:nj_lyf@263.net
我无法发送邮件:
/*
to nj_lyf:
我现在给你发送的仅是端口的初始化,关于通讯协议的问题,因为
我没还不清楚你在使用什么协议,是端口协议还是其他方式规定的协议
因为各种的原语描述不同,但这主要是数据的处理,我以前做电力系统
通讯的其中协议就有十几种。
关系到具体的你可以e-mail me :error_c_cn@sina.com
*/
//---------------------------------------------------------------------------
// cw_error write for jcj to try moxa_modem,
// create serieres port and more threads to used it,
// copyright of cw_error, 2000-may and cw_error at writing "man and nature"
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// Define may be used at same time,
// extern TForm2 *Form2;
HANDLE hcom;
HANDLE xModemThreadHandle;
DWORD idxModemThread;
// Define one extern thread and used serier port,
// extern void xModemThread();
//---------------------------------------------------------------------------
bool __fastcall TForm1::InitPort(AnsiString PortName)
{
_DCB myDCB;
COMMTIMEOUTS myTMO;
bool Result;
// simply define communication handle,if existing and closing
// renew to create,
if(hcom != 0/*INVALID_HANDLE_VALUE*/)
CloseHandle(hcom);
//create communication handle,
hcom=CreateFile(PortName.c_str(), // port name
GENERIC_READ|GENERIC_WRITE, // RW access mode
0, // exclusive mode
NULL, // no security attributes
OPEN_EXISTING, // open existing port
0, // not overlapped
0);
Result = (hcom!=INVALID_HANDLE_VALUE);
if(!Result)
MessageDlg("not open port:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0);
else
{
//set communlcation mask,set of events to be monitored,
if(!SetCommMask(hcom,EV_RXFLAG))
{
if(MessageDlg("not set mask:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//initializes the communications parameters,
if(!SetupComm(hcom,4096,4096))//4096 is specifies send and recvieve size,
{
if(MessageDlg("not setup communiation:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//get the communications state and DCB struct,
if(!GetCommState(hcom,&myDCB))
{
if(MessageDlg("not get state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set dcb state,include baudrate/bytebit/stopbit/parity,
myDCB = GetDCBParater(myDCB);
// if(!BuildCommDCB("COM1:baud=1200 parity=N data=8 stop=1",&myDCB));
// { 以上个的函数用法同下 }
if(!SetCommState(hcom,&myDCB))
{
//unsigned long TTT = GetLastError();
if(MessageDlg("not set state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
if(!GetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not get out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set out time function and coeff.
myTMO.ReadIntervalTimeout = MAXDWORD;
myTMO.ReadTotalTimeoutMultiplier = 0;
myTMO.ReadTotalTimeoutConstant = 0;
myTMO.WriteTotalTimeoutMultiplier =0;
myTMO.WriteTotalTimeoutConstant =1000;
if(!SetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not set out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//API_Function: clear send buffer,
PurgeComm(hcom,PURGE_TXCLEAR);
//API-Function: clear recvive buffer,
PurgeComm(hcom,PURGE_RXCLEAR);
}
return Result;
}
bool __fastcall TForm1::RecvData()
{
bool Result;
AnsiString ReadStr;
unsigned char ReadBuffer[100];
unsigned long ReadNum;
memset(ReadBuffer,0,sizeof(ReadBuffer));
Result = ReadFile(hcom,ReadBuffer,sizeof(ReadBuffer),&ReadNum,NULL);
Form1->RECommand->Lines->Add("Receive:\r"+ReadStr );
if(Result)
{
for(int i=0;i<sizeof(ReadBuffer);i++)
ReadStr = ReadStr + (char)ReadBuffer[i];
if(sizeof(ReadStr) == 4)
ReadStr = "0";
}
RECommand->Lines->Add(ReadStr );
return 1;
}
//在这里我发送的是字符串。
bool __fastcall TForm1::SendStrToPort(AnsiString ReadStr)
{
bool Result;
unsigned char B[100];
unsigned long SendNum;
for(int i=1;i<=ReadStr.Length();i++)
{
B[i-1]= (int)ReadStr[i];
}
SendNum = ReadStr.Length();
WriteFile(hcom,B,SendNum,&SendNum,NULL);
if(!WriteFile(hcom,B,SendNum,&SendNum,NULL))
{
Result = false;
if(Application->MessageBox("without write", NULL, MB_OKCANCEL) != IDOK)
exit(1);
}
return Result;
}