串口通信代码

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

串口通信代码

TMSCOMN::~TMSCOMN()

{

if(hComs != INVALID_HANDLE_VALUE)

{

CloseHandle(hComs);

hComs = INVALID_HANDLE_VALUE;

}

};

void TMSCOMN::CloseComs()

{

if(hComs != INVALID_HANDLE_VALUE)

{

CloseHandle(hComs);

hComs = INVALID_HANDLE_VALUE;

}

};

AnsiString TMSCOMN::GetComError()

{

AnsiString Errors;

if(ComReg==0) Errors = AnsiString("串口设备与串口工作良好"); else if(ComReg==1) Errors = AnsiString("串口未打开");

else Errors = AnsiString(" 串口设备与串口未连接上");

return Errors;

};

void TMSCOMN::SetRs232Coms(int NoComs,int Bps)

{

AnsiString ComNo;

DCB dcb;

COMMTIMEOUTS TimeOuts;

char str[100];

ComErr = AnsiString("");

if(hComs != INVALID_HANDLE_VALUE)

{

CloseHandle(hComs);

hComs = INVALID_HANDLE_VALUE;

}

ComNo = AnsiString("COM")+AnsiString(NoComs+1);

hComs=CreateFile(ComNo.c_str(),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,0);

if(hComs == INVALID_HANDLE_VALUE)

{

ComReg = 1; ComErr = AnsiString("串口RS232未打开");

Application->MessageBoxA("串口RS232打开出错!","错误",MB_OK|MB_ICONERROR); }

else

{

GetCommState(hComs,&dcb);

if(Bps==0) dcb.BaudRate = CBR_300;

else if(Bps==1) dcb.BaudRate = CBR_1200;

else if(Bps==2) dcb.BaudRate = CBR_2400;

else if(Bps==3) dcb.BaudRate = CBR_4800;

else if(Bps==4) dcb.BaudRate = CBR_9600;

else if(Bps==5) dcb.BaudRate = CBR_19200;

else if(Bps==6) dcb.BaudRate = CBR_38400;

dcb.ByteSize = 8;

dcb.Parity = NOPARITY;

dcb.StopBits = ONESTOPBIT;

SetCommState(hComs,&dcb);

TimeOuts.ReadIntervalTimeout=0; //读间隔超时

TimeOuts.ReadTotalTimeoutMultiplier=10; //读时间系数

TimeOuts.ReadTotalTimeoutConstant=100; //读时间常量

TimeOuts.WriteTotalTimeoutMultiplier=10; //写时间系数

TimeOuts.WriteTotalTimeoutConstant=100; //写时间常数

SetCommTimeouts(hComs, &TimeOuts); // 保存设置值生效

lpOverlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);

ComReg = 0;

GetCommModemStatus(hComs,&Stats);

if(((Stats)&16)==0)

{

ComReg = 2;

sprintf(str,"串口%d 与设备PM300未连接上!",(NoComs+1));

ComErr = AnsiString(str);

Application->MessageBoxA(str,"错误",MB_OK|MB_ICONERROR);

}

}

};

void TMSCOMN::SetRs485Coms(int NoComs,int Bps)

AnsiString ComNo;

DCB dcb;

COMMTIMEOUTS TimeOuts;

char str[100];

ComErr = AnsiString("");

if(hComs != INVALID_HANDLE_VALUE)

{

CloseHandle(hComs);

hComs = INVALID_HANDLE_VALUE;

}

ComNo = AnsiString("COM")+AnsiString(NoComs+1);

hComs=CreateFile(ComNo.c_str(),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,0);

if(hComs == INVALID_HANDLE_VALUE)

{

ComReg = 1; ComErr = AnsiString("串口RS485未打开");

Application->MessageBoxA("串口RS485打开出错!","错误",MB_OK|MB_ICONERROR); }

else

{

GetCommState(hComs,&dcb);

if(Bps==0) dcb.BaudRate = CBR_300;

else if(Bps==1) dcb.BaudRate = CBR_1200;

else if(Bps==2) dcb.BaudRate = CBR_2400;

else if(Bps==3) dcb.BaudRate = CBR_4800;

else if(Bps==4) dcb.BaudRate = CBR_9600;

else if(Bps==5) dcb.BaudRate = CBR_19200;

else if(Bps==6) dcb.BaudRate = CBR_38400;

dcb.ByteSize = 8;

dcb.Parity = NOPARITY; //MARKPARITY

dcb.StopBits = ONESTOPBIT;

SetCommState(hComs,&dcb);

TimeOuts.ReadIntervalTimeout=0; //读间隔超时

TimeOuts.ReadTotalTimeoutMultiplier=10; //读时间系数

TimeOuts.ReadTotalTimeoutConstant=100; //读时间常量

TimeOuts.WriteTotalTimeoutMultiplier=10; //写时间系数

TimeOuts.WriteTotalTimeoutConstant=100; //写时间常数

SetCommTimeouts(hComs, &TimeOuts); // 保存设置值生效

lpOverlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);

相关文档
最新文档