Курсовая работа: Применение автоматизированного адаптивного интерферометра для исследования наносмещений микрообъектов
char* LocBuf2 = sv.GetBuffer(128);
sg.Send(LocBuf2, 2);
sv.ReleaseBuffer();
int len = this->m_vel.GetLength();
if(len > 0)
{
char* LocBuf = m_vel.GetBuffer(128);
sg.Send(LocBuf, len);
m_vel.ReleaseBuffer();
//enter
char ent=0x0D;
char* ent2 = &ent;
sg.Send(ent2, 1);
//проверкаty
CString ty="ty";
char* LocBuf3 = ty.GetBuffer(128);
sg.Send(LocBuf3, 2);
ty.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
}
}
void CSerialGateTestDlg::OnOnMotroon()
{
UpdateData(true); //включить мотор
CString mn="mn";
char* LocBuf2 = mn.GetBuffer(128);
sg.Send(LocBuf2, 2);
mn.ReleaseBuffer();// TODO: Add your control notification handler code here
//enter
char ent=0x0D;
char* ent2 = &ent;
sg.Send(ent2, 1);
}
void CSerialGateTestDlg::OnOnMotoroff() //выключить мотор
{
CString mf="mf";
char* LocBuf2 = mf.GetBuffer(128);
sg.Send(LocBuf2, 2);
mf.ReleaseBuffer();// TODO: Add your control notification handler code here
//enter
char ent=0x0D;
char* ent2 = &ent;
sg.Send(ent2, 1);
}
void CSerialGateTestDlg::OnOnMr() //перемещение
{
UpdateData(true);
CString mr="mr";
char* LocBuf2 = mr.GetBuffer(128);
sg.Send(LocBuf2, 2);
mr.ReleaseBuffer();
int len = this->m_gomr.GetLength();
if(len > 0)
{
char* LocBuf = m_gomr.GetBuffer(128);
sg.Send(LocBuf, len);
m_gomr.ReleaseBuffer();
//enter
char ent=0x0D;
char* ent2 = &ent;
sg.Send(ent2, 1);
//проверкаty
CString tp="tp";
char* LocBuf3 = tp.GetBuffer(128);
sg.Send(LocBuf3, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
//m_recieve.Clear();
}
}
void CSerialGateTestDlg::OnOnGo() //////////////////кнопка GO//////////////////////
{
UpdateData(true);
key=1;
int axis=0;
int res;
TP0:
//изменение активного мотора
char ctrlkey=0x01;
char* ctrlkey2 = &ctrlkey;
sg.Send(ctrlkey2, 1);
int len=1;
char axsis0='0';
char* axsis0_2 = &axsis0;
sg.Send(axsis0_2, 1);
CString tp="tp";
char* LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
char ent=0x0D;
char* ent2 = &ent;
sg.Send(ent2, 1);
Sleep(700); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
char buff[128];
int rcv = sg.Recv(buff, sizeof(buff));
int elmbuff=rcv-1;
buff[rcv]=0;
CString str;
for(int i = 0; i< rcv; i++)
str += buff[i];
char* j = strchr(buff, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
CString mraxsis0_2; //сюда записывается занчение е окошка edit axsis0
GetDlgItemText(IDC_MRAXSIS0,mraxsis0_2);
int posnum0=atoi(mraxsis0_2);
int posnum0_mr=res-posnum0/2;
CString mraxsis0_mr;
mraxsis0_mr =itoa(posnum0_mr,mraxsis0_mr.GetBuffer(10),10);
//выполним перемещение на нулевую позицию//
CString mr="MA";
char* LocBuf8 = mr.GetBuffer(128);
sg.Send(LocBuf8, 2);
mr.ReleaseBuffer();
len=mraxsis0_mr.GetLength();
char* LocBuf9 = mraxsis0_mr.GetBuffer(128);
sg.Send(LocBuf9, len);
mraxsis0_mr.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
int gto0=posnum0_mr;
int posicino00=res;
Sleep(400);
TP1:
//изменение активного мотора
sg.Send(ctrlkey2, 1);
len=1;
char axsis1='1';
char* axsis1_2 = &axsis1;
sg.Send(axsis1_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(700);
//организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
char buff2[128];
rcv = sg.Recv(buff2, sizeof(buff2));
elmbuff=rcv-1;
buff2[rcv]=0;
CString str2;
for(i = 0; i< rcv; i++)
str2 += buff2[i];
j = strchr(buff2, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff2, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
CString mraxsis1_2; //сюда записывается занчение е окошка edit axsis0
GetDlgItemText(IDC_MRAXSIS1,mraxsis1_2);
int posnum1=atoi(mraxsis1_2);
int posnum1_mr=res-posnum1/2;
CString mraxsis1_mr;
mraxsis1_mr =itoa(posnum1_mr,mraxsis1_mr.GetBuffer(10),10);
//выполним перемещение на нулевую позицию//
mr="MA";
LocBuf8 = mr.GetBuffer(128);
sg.Send(LocBuf8, 2);
mr.ReleaseBuffer();
len=mraxsis1_mr.GetLength();
LocBuf9 = mraxsis1_mr.GetBuffer(128);
sg.Send(LocBuf9, len);
mraxsis1_mr.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
int gto1=posnum1_mr;
int posicino01=res;
Sleep(130);
int pos01=gto1;
int pos00=gto0;
int modul;
do
{
Sleep(400);
//изменение активного мотора
sg.Send(ctrlkey2, 1);
sg.Send(axsis0_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(800); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
CString str;
char buff[128];
rcv = sg.Recv(buff, sizeof(buff));
if(rcv > 0)
elmbuff=rcv-1;
buff[rcv]=0;
for(int i = 0; i< rcv; i++)
str += buff[i];
j = strchr(buff, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
}//ожидание прихода подвижки в заданное место
modul=res-gto0;
if (modul<0) modul=-modul;
} while (modul>20);
do
{
Sleep(800);
//изменение активного мотора
sg.Send(ctrlkey2, 1);
sg.Send(axsis1_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(400); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
char buff[128];
rcv = sg.Recv(buff, sizeof(buff));
elmbuff=rcv-1;
buff[rcv]=0;
CString str;
for(int i = 0; i< rcv; i++)
str += buff[i];
j = strchr(buff, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff, '-');
res = atoi(j); //в res здесь храниться текущие положение
}//ожидание прихода подвижки в заданное место
modul=res-gto1;
if (modul<0) modul=-modul;
}while (modul>20);
MessageBox("peremecheno","ok", MB_ICONINFORMATION);
//UpdateData(false);
//////////////организуем сканирование////////////////
int tape0=0;
int tape1=0;
int tape0t=0; //текущий проход
int tape1t=0;
//CString tapeaxsis0_1;
//GetDlgItemText(IDC_TAPEAXSIS0,tapeaxsis0_1);
//tape0=atoi(tapeaxsis0_1);
CString tapeaxsis1_1;
GetDlgItemText(IDC_TAPEAXSIS1,tapeaxsis1_1);
tape1=atoi(tapeaxsis1_1);
//перемещение по оси 1//
while (tape1t<tape1)
{
//премещение по оси 0//
//UpdateData(false);
//CString mraxsis0_mr;
//mraxsis0_mr =itoa(posnum0_mr,mraxsis0_mr.GetBuffer(10),10);
//this->m_pastcontrol.SetWindowText("");
//CString tape1tstr;
//tape1tstr =itoa(tape1t,tape1tstr.GetBuffer(10),10);
//this->m_past=tape1tstr;
tape1t++;
//изменение активного мотора
char ctrlkey=0x01;
char* ctrlkey2 = &ctrlkey;
sg.Send(ctrlkey2, 1);
int len=1;
char axsis0='0';
char* axsis0_2 = &axsis0;
sg.Send(axsis0_2, 1);
CString tp="tp";
char* LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
char ent=0x0D;
char* ent2 = &ent;
sg.Send(ent2, 1);
Sleep(600); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
char buff1[128];
int rcv = sg.Recv(buff1, sizeof(buff1));
buff1[rcv]=0;
char* j = strchr(buff1, '+');
int res = atoi(j);
if (j==0)
{
j = strchr(buff1, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
CString mraxsis0_2; //сюда записывается занчение е окошка edit axsis0
GetDlgItemText(IDC_MRAXSIS0,mraxsis0_2);
posnum0=atoi(mraxsis0_2);
posnum0_mr=posicino00+posnum0/2; //перемещаемся вперед
CString mraxsis0_mr;
mraxsis0_mr =itoa(posnum0_mr,mraxsis0_mr.GetBuffer(10),10);
//выполним перемещение//
CString mr="MA";
char* LocBuf8 = mr.GetBuffer(128);
sg.Send(LocBuf8, 2);
mr.ReleaseBuffer();
len=mraxsis0_mr.GetLength();
char* LocBuf9 = mraxsis0_mr.GetBuffer(128);
sg.Send(LocBuf9, len);
mraxsis0_mr.ReleaseBuffer();//enter
sg.Send(ent2, 1);
gto0=posnum0_mr;
//считывание данных из файла//
CString inputfile;
GetDlgItemText(IDC_DATAEXCHANGE, inputfile);
CString outputfile;
GetDlgItemText(IDC_DATAEXCHANGE2, outputfile);
CString strinput;
CString stroutput;
CStdioFile FileInPut(inputfile, CFile::shareDenyNone);
CStdioFile FileOutPut(outputfile, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyNone);
FileInPut.SeekToEnd();
int positionFileInPut1=FileInPut.GetPosition();
//считывание данных из файла//
Sleep(300);
do
{
Sleep(400);
//изменение активного мотора
sg.Send(ctrlkey2, 1);
sg.Send(axsis0_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(800); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
CString str;
char buff[128];
rcv = sg.Recv(buff, sizeof(buff));
if(rcv > 0)
{
elmbuff=rcv-1;
buff[rcv]=0;
for(int i = 0; i< rcv; i++)
str += buff[i];
j = strchr(buff, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
}//ожидание прихода подвижки в заданное место
modul=res-gto0;
if (modul<0) modul=-modul;
} while (modul>20);
//дождемся пока подвижка примет первоночальное положение//
//считывание данных из файла//
FileInPut.SeekToEnd();
FileOutPut.SeekToEnd();
int positionFileInPut2=FileInPut.GetPosition();
FileInPut.Seek(positionFileInPut1, CFile::begin);
CString space=" ";
//char buffstr[128];
for (int positionFileInPutCurrent=FileInPut.GetPosition(); positionFileInPutCurrent<positionFileInPut2; positionFileInPutCurrent=FileInPut.GetPosition())
{
//FileOutPut.ReadString(stroutput);
FileInPut.ReadString(strinput);
char* buffstr = strinput.GetBuffer(128);
int lenstr=strinput.GetLength();
buffstr[lenstr]=0;
char* j = strchr(buffstr, ',');
stroutput=j+1;
FileOutPut.WriteString(stroutput);
FileOutPut.WriteString(space);
}
//перемещение по оси 1//
FileOutPut.WriteString("\r\n");
//изменение активного мотора//
sg.Send(ctrlkey2, 1);
len=1;
char axsis1='1';
char* axsis1_2 = &axsis1;
sg.Send(axsis1_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(300); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
char buff4[128];
rcv = sg.Recv(buff4, sizeof(buff4));
buff4[rcv]=0;
j = strchr(buff4, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff4, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
CString mraxsis1_2; //сюда записывается занчение е окошка edit axsis0
GetDlgItemText(IDC_MRAXSIS1,mraxsis1_2);
int posnum1=atoi(mraxsis1_2);
int posnum1_mr=pos01+tape1t*posnum1/tape1;
CString mraxsis1_mr;
mraxsis1_mr =itoa(posnum1_mr,mraxsis1_mr.GetBuffer(10),10);
gto1=posnum1_mr;
//выполним перемещение//
mr="MA";
LocBuf8 = mr.GetBuffer(128);
sg.Send(LocBuf8, 2);
mr.ReleaseBuffer();
len=mraxsis1_mr.GetLength();
LocBuf9 = mraxsis1_mr.GetBuffer(128);
sg.Send(LocBuf9, len);
mraxsis1_mr.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(300);
do
{
Sleep(400);
//изменение активного мотора
sg.Send(ctrlkey2, 1);
len=1;
char axsis1='1';
char* axsis1_2 = &axsis1;
sg.Send(axsis1_2, 1);
sg.Send(ctrlkey2, 1);
sg.Send(axsis1_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(800); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
CString str;
char buff[128];
rcv = sg.Recv(buff, sizeof(buff));
if(rcv > 0)
{
elmbuff=rcv-1;
buff[rcv]=0;
for(int i = 0; i< rcv; i++)
str += buff[i];
j = strchr(buff, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
}//ожидание прихода подвижки в заданное место
modul=res-gto1;
if (modul<0) modul=-modul;
} while (modul>20);
//переведем подвижку в первоначальное положение//
//изменение активного мотора//
Sleep(300);
ctrlkey=0x01;
ctrlkey2 = &ctrlkey;
sg.Send(ctrlkey2, 1);
len=1;
axsis0='0';
axsis0_2 = &axsis0;
sg.Send(axsis0_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
ent=0x0D;
ent2 = &ent;
sg.Send(ent2, 1);
Sleep(300); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
charbuff7[128];
rcv = sg.Recv(buff7, sizeof(buff7));
elmbuff=rcv-1;
buff7[rcv]=0;
CStringstr7;
for(i = 0; i< rcv; i++)
str7 += buff7[i];
j = strchr(buff7, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff7, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
//сюда записывается занчение е окошка edit axsis0
GetDlgItemText(IDC_MRAXSIS0,mraxsis0_2);
posnum0=atoi(mraxsis0_2);
posnum0_mr=pos00; //перемещаемся назад
mraxsis0_mr =itoa(posnum0_mr,mraxsis0_mr.GetBuffer(10),10);
//выполним перемещение на нулевую позицию//
mr="MA";
LocBuf8 = mr.GetBuffer(128);
sg.Send(LocBuf8, 2);
mr.ReleaseBuffer();
len=mraxsis0_mr.GetLength();
LocBuf9 = mraxsis0_mr.GetBuffer(128);
sg.Send(LocBuf9, len);
mraxsis0_mr.ReleaseBuffer();//enter
sg.Send(ent2, 1);
gto0=pos00;
Sleep(300);
do
{
Sleep(300);
//изменение активного мотора
sg.Send(ctrlkey2, 1);
sg.Send(axsis0_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(300); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
CString str8;
char buff8[128];
rcv = sg.Recv(buff8, sizeof(buff8));
if(rcv > 0)
{
elmbuff=rcv-1;
buff8[rcv]=0;
for(int i = 0; i< rcv; i++)
str8 += buff8[i];
j = strchr(buff8, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff8, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
}//ожидание прихода подвижки в заданное место
modul=res-gto0;
if (modul<0) modul=-modul;
} while (modul>20);//дождемся пока подвижка примет первоночальное положение//
UpdateData(true);
//перемещение по оси 1//
}
/////////////////////////////////////////////
//вернем систему в первоночальное положение//
/////////////////////////////////////////////
Tp0:
//изменение активного мотора
sg.Send(ctrlkey2, 1);
len=1;
axsis0='0';
axsis0_2 = &axsis0;
sg.Send(axsis0_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(300); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
posnum0_mr=posicino00;
mraxsis0_mr =itoa(posnum0_mr,mraxsis0_mr.GetBuffer(10),10);
//выполним перемещение на нулевую позицию//
mr="MA";
char* LocBuf11 = mr.GetBuffer(128);
sg.Send(LocBuf11, 2);
mr.ReleaseBuffer();
len=mraxsis0_mr.GetLength();
char* LocBuf14 = mraxsis0_mr.GetBuffer(128);
sg.Send(LocBuf14, len);
mraxsis0_mr.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(200);
UpdateData(true);
gto0=posicino00;
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
Sleep(300);
char buff10[128];
rcv = sg.Recv(buff10, sizeof(buff10));
Tp1:
//изменение активного мотора
sg.Send(ctrlkey2, 1);
len=1;
axsis1='1';
axsis1_2 = &axsis1;
sg.Send(axsis1_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(300);
//организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
rcv = sg.Recv(buff10, sizeof(buff10));
posnum1_mr=posicino01;
mraxsis1_mr =itoa(posnum1_mr,mraxsis1_mr.GetBuffer(10),10);
gto1=posicino01;
//выполним перемещение на нулевую позицию//
mr="MA";
LocBuf8 = mr.GetBuffer(128);
sg.Send(LocBuf8, 2);
mr.ReleaseBuffer();
len=mraxsis1_mr.GetLength();
LocBuf9 = mraxsis1_mr.GetBuffer(128);
sg.Send(LocBuf9, len);
mraxsis1_mr.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
UpdateData(true);
Sleep(130);
do
{
Sleep(300);
//изменение активного мотора
sg.Send(ctrlkey2, 1);
sg.Send(axsis0_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(300); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
CString str;
char buff[128];
rcv = sg.Recv(buff, sizeof(buff));
if(rcv > 0)
{
elmbuff=rcv-1;
buff[rcv]=0;
for(int i = 0; i< rcv; i++)
str += buff[i];
j = strchr(buff, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff, '-');
res = atoi(j); //в res здесь храниться текущие положение
}
}//ожидание прихода подвижки в заданное место
modul=res-gto0;
if (modul<0) modul=-modul;
} while (modul>40);
do
{
Sleep(400);
//изменение активного мотора
sg.Send(ctrlkey2, 1);
sg.Send(axsis1_2, 1);
tp="tp";
LocBuf2 = tp.GetBuffer(128);
sg.Send(LocBuf2, 2);
tp.ReleaseBuffer();
//enter
sg.Send(ent2, 1);
Sleep(300); //организуем паузу чтобы контроллер успел ответить
//считаем текущую позицию//
char buff[128];
rcv = sg.Recv(buff, sizeof(buff));
elmbuff=rcv-1;
buff[rcv]=0;
CString str;
for(int i = 0; i< rcv; i++)
str += buff[i];
j = strchr(buff, '+');
res = atoi(j);
if (j==0)
{
j = strchr(buff, '-');
res = atoi(j); //в res здесь храниться текущие положение
}//ожидание прихода подвижки в заданное место
modul=res-gto1;
if (modul<0) modul=-modul;
}while (modul>20);
key=0;
////////////////////////////////////////////
}
Листинг SerialGate.dll
SerialGate.cpp:
#include "stdafx.h"
#include "SerialGate.h"
#include <iostream>
#include <Winspool.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec (dllexport) bool SerialGate::Open(int port, int baud)
{
char COM_string[20];
sprintf(COM_string,"\\\\.\\COM%d", port);
m_hFile = CreateFile(COM_string, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
if(m_hFile == INVALID_HANDLE_VALUE)
{
return false;
}
DCB dcb;
GetCommState(m_hFile, &dcb);
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(m_hFile, &CommTimeOuts);
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.BaudRate = baud;
SetCommState(m_hFile, &dcb);
this->state = true;
return true;
}
extern "C" __declspec (dllexport) SerialGate::SerialGate()
{
this->state = false;
}
extern "C" __declspec (dllexport) SerialGate::~SerialGate()
{
this->Close();
}
extern "C" __declspec (dllexport) void SerialGate::Close()
{
this->state = false;
CloseHandle(m_hFile);
}
extern "C" __declspec (dllexport) void SerialGate::Clean()
extern "C" __declspec (dllexport) int SerialGate::Send(char* buff, int szBuff)
{
if(!state)
return 0;
if(buff == NULL || szBuff <= 0)
{
return 0;
}
DWORD lpdwBytesWrittens = 0;
WriteFile(m_hFile, buff, szBuff, &lpdwBytesWrittens, NULL);
return lpdwBytesWrittens;
}
extern "C" __declspec (dllexport) int SerialGate::Recv(char* buff, int szBuff)
{
if(!state)
return 0;
if(buff == NULL || szBuff <= 0)
{
return 0;
}
DWORD dwBytesRead = 0;
ReadFile(m_hFile, buff, szBuff, &dwBytesRead, NULL);
return dwBytesRead;
}
extern "C" __declspec (dllexport) void SerialGate::SetLine(OUT_LINES_NAME ln, bool state)
{
if(!state)
return ;
unsigned char value;
if(ln == DTR)
{
if(state)
value = 6;
else
value = 5;
}
if(ln == RTS)
{
if(state)
value = 4;
else
value = 3;
}
EscapeCommFunction(m_hFile, value);
}
extern "C" __declspec (dllexport) bool SerialGate::GetLine(IN_LINES_NAME ln)
{
if(!state)
return 0;
unsigned long ul = 0;
GetCommModemStatus(m_hFile, &ul);
if(ul == 0x10 && ln == CTS)
{
return true;
}
if(ul == 0x20 && ln == DSR)
{
return true;
}
if(ul == 0x40 && ln == RING)
{
return true;
}
if(ul == 0x80 && ln == RLSD)
{
return true;
}
return false;
}
extern "C" __declspec (dllexport) void SerialGate::GetPortsInfo(PortInfo* pi)
{
DWORD Ports_MemSize = 0;
DWORD Ports_Count = 0;
BYTE* lpPorts = NULL;
//Getting Ports_MemSize value...
EnumPorts(NULL,
1,
lpPorts,
0,
&Ports_MemSize,
&Ports_Count);
//Getting lpPorts...
lpPorts = new BYTE[Ports_MemSize];
EnumPorts(NULL,
1,
lpPorts,
Ports_MemSize,
&Ports_MemSize,
&Ports_Count);
//Forming List Of Ports...
DWORD dw;
TCHAR str[8];
char temp[4];
int port = -1;
PORT_INFO_1 *pPortInfo;
pPortInfo = (PORT_INFO_1 *)lpPorts;
int counter = 0;
bool av = false;
for (dw = 0; dw < Ports_Count; dw++)
{
lstrcpyn(str, pPortInfo->pName, 4);
str[4] = 0;
if (lstrcmpi(str, "com") == 0)
{
//printf("%s\n", pPortInfo->pName);
memset(temp, '\0', sizeof(temp));
temp[0] = pPortInfo->pName[3];
if(pPortInfo->pName[4]!= ':' && pPortInfo->pName[4]!= '\0')
temp[1] = pPortInfo->pName[4];
if(pPortInfo->pName[5]!= ':' && pPortInfo->pName[5]!= '\0')
temp[2] = pPortInfo->pName[5];
port = atoi(temp);
//printf("%d\n", port);
char COM_string[20];
sprintf(COM_string,"\\\\.\\COM%d", port);
HANDLE h = CreateFile(COM_string, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
if(h == INVALID_HANDLE_VALUE)
{
//return false;
av = false;
}
else
{
av = true;
CloseHandle(h);
}
pi->p[counter].Id = port;
pi->p[counter].Availbl = av;
counter++;
}
pPortInfo++;
}
pi->koll = counter;
delete [] lpPorts;
}
Листинг SerialGate.dll
SerialGate.h:
#include <windows.h>
#define MAX_WIN_PORT 255
struct Port
{
unsigned char Id;
bool Availbl;
};
struct PortInfo
{
Port p[MAX_WIN_PORT];
unsigned char koll;
};
extern "C" class __declspec (dllexport) SerialGate
{
public:
enum IN_LINES_NAME {CTS, DSR, RING, RLSD};
enum OUT_LINES_NAME {DTR, RTS};
SerialGate();
~SerialGate();
bool Open(int port, int baud);
int Send(char* buff, int szBuff);
int Recv(char* buff, int szBuff);
void SetLine(OUT_LINES_NAME ln, bool state);
bool GetLine(IN_LINES_NAME ln);
void GetPortsInfo(PortInfo* pi);
void Close();
void Clean();
private:
HANDLE m_hFile;
bool state;
};