Программирую com-порт на языке C++. Задача передать системное время. Столкнулся с ошибкой в функции WriteFile.
Вот код для отправляющей программы:
#include<iostream>
#include<conio.h>
#include <stdio.h>
#include<windows.h>
using namespace std;
int t[4];
HANDLE hPort;
LPCTSTR sPortName;
DCB dDCB;
int buffer;
DWORD RealRead;
DWORD RealWrite;
DWORD dw = 0;
void getTime()
{
SYSTEMTIME sm;
GetLocalTime(&sm);
t[0] = sm.wHour;
t[1] = sm.wMinute;
t[2] = sm.wSecond;
t[3] = sm.wMilliseconds;
}
void choosePort()
{
char n;
int a=0;
cout<<"Please, enter the numder of Com-port!"<<endl;
cout<<"Com1 - pressed (1)"<<endl;
cout<<"Com2 - pressed (2)"<<endl;
cout<<"Com3 - pressed (3)"<<endl;
cout<<"Com4 - pressed (4)"<<endl;
while(a == 0)
{
cin>>n;
if (n == '1')
{
cout<<"Com1 is active!"<<endl;
sPortName = "com1";
a = 1;
}
else if (n == '2')
{
cout<<"Com2 is active!"<<endl;
sPortName = "com2";
a = 1;
}
else if (n == '3')
{
cout<<"Com3 is active!"<<endl;
sPortName = "com3";
a = 1;
}
else if (n == '4')
{
cout<<"Com4 is active!"<<endl;
sPortName = "com4";
a = 1;
}
else
{
cout<<"Try agian, man!"<<endl;
a = 0;
}
}
}
int main()
{
choosePort();
hPort = CreateFile(
LPCTSTR (sPortName),
GENERIC_READ or GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
dDCB.DCBlength = sizeof(LPDCB);
GetCommState(
hPort,
&dDCB);
dDCB.BaudRate = CBR_115200;
dDCB.ByteSize = 8;
dDCB.Parity = EVENPARITY;
dDCB.StopBits = 2;
SetCommState(
hPort,
&dDCB);
getTime();
while(dw =! 3)
{
buffer = t[dw];
WriteFile(
hPort,
buffer,
1,
&RealWrite,
NULL);
inc dw;
}
closeHandle(hPort);
getch();
}
while(dw != 3)
// не путай = и ! местами !!! запись будет корректна, но смысл совсем другой
{
buffer = t[dw];
WriteFile(
hPort,
&buffer,
1,
&RealWrite,
NULL);
dw++;
}
CloseHandle(hPort);
Исправил ошибки. Написал код для принимающей машины.
Отправлюящая машина:
#include<iostream>
#include<conio.h>
#include <stdio.h>
#include<windows.h>
using namespace std;
int t[4];
HANDLE hPort;
LPCTSTR sPortName;
DCB dDCB;
int buffer;
DWORD RealRead = 0;
DWORD RealWrite = 0;
DWORD dw = 0;
void getTime()
{
SYSTEMTIME sm;
GetLocalTime(&sm);
t[0] = sm.wHour;
t[1] = sm.wMinute;
t[2] = sm.wSecond;
t[3] = sm.wMilliseconds;
}
void KillCom()
{
if (hPort != INVALID_HANDLE_VALUE)
{
CloseHandle(hPort);
hPort = INVALID_HANDLE_VALUE;
}
cout<<"Don't connected!"<<endl;
}
void choosePort()
{
char n;
int a=0;
cout<<"Please, enter the numder of Com-port!"<<endl;
cout<<"Com1 - pressed (1)"<<endl;
cout<<"Com2 - pressed (2)"<<endl;
cout<<"Com3 - pressed (3)"<<endl;
cout<<"Com4 - pressed (4)"<<endl;
while(a == 0)
{
cin>>n;
if (n == '1')
{
cout<<"Com1 is active!"<<endl;
sPortName = "com1";
a = 1;
}
else if (n == '2')
{
cout<<"Com2 is active!"<<endl;
sPortName = "com2";
a = 1;
}
else if (n == '3')
{
cout<<"Com3 is active!"<<endl;
sPortName = "com3";
a = 1;
}
else if (n == '4')
{
cout<<"Com4 is active!"<<endl;
sPortName = "com4";
a = 1;
}
else
{
cout<<"Try agian, man!"<<endl;
a = 0;
}
}
}
int main()
{
choosePort();
if (hPort != INVALID_HANDLE_VALUE) KillCom;
hPort = CreateFile(
LPCTSTR (sPortName),
GENERIC_READ or GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hPort == INVALID_HANDLE_VALUE) cout<<"Opening Com is impossible!"<<endl;
dDCB.DCBlength = sizeof(LPDCB);
GetCommState(
hPort,
&dDCB);
dDCB.BaudRate = CBR_115200;
dDCB.ByteSize = 8;
dDCB.Parity = EVENPARITY;
dDCB.StopBits = 2;
SetCommState(
hPort,
&dDCB);
getTime();
while(dw != 3)
{
buffer = t[dw];
WriteFile(
hPort,
&buffer,
1,
&RealWrite,
NULL);
dw++;
}
CloseHandle(hPort);
getch();
}
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
int t[4];
HANDLE hPort;
LPCTSTR sPortName;
DCB dDCB;
int buffer;
DWORD RealRead = 0;
DWORD RealWrite = 0;
DWORD dw = 0;
void setTime()
{
SYSTEMTIME sm;
sm.wHour = t[0] ;
cout<<sm.wHour<<endl;
sm.wMinute = t[1] ;
sm.wSecond = t[2];
sm.wMilliseconds = t[3] ;
SetLocalTime(&sm);
cout<<sm.wHour<<endl;
}
void KillCom()
{
if (hPort != INVALID_HANDLE_VALUE)
{
CloseHandle(hPort);
hPort = INVALID_HANDLE_VALUE;
}
cout<<"Don't connected!"<<endl;
}
void choosePort()
{
char n;
int a=0;
cout<<"Please, enter the numder of Com-port!"<<endl;
cout<<"Com1 - pressed (1)"<<endl;
cout<<"Com2 - pressed (2)"<<endl;
cout<<"Com3 - pressed (3)"<<endl;
cout<<"Com4 - pressed (4)"<<endl;
while(a == 0)
{
cin>>n;
if (n == '1')
{
cout<<"Com1 is active!"<<endl;
sPortName = "com1";
a = 1;
}
else if (n == '2')
{
cout<<"Com2 is active!"<<endl;
sPortName = "com2";
a = 1;
}
else if (n == '3')
{
cout<<"Com3 is active!"<<endl;
sPortName = "com3";
a = 1;
}
else if (n == '4')
{
cout<<"Com4 is active!"<<endl;
sPortName = "com4";
a = 1;
}
else
{
cout<<"Try agian, man!"<<endl;
a = 0;
}
}
}
int main()
{
choosePort();
if (hPort != INVALID_HANDLE_VALUE) KillCom;
hPort = CreateFile(
LPCTSTR (sPortName),
GENERIC_READ or GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hPort == INVALID_HANDLE_VALUE) cout<<"Opening Com is impossible!"<<endl;
dDCB.DCBlength = sizeof(LPDCB);
GetCommState(
hPort,
&dDCB);
dDCB.BaudRate = CBR_115200;
dDCB.ByteSize = 8;
dDCB.Parity = EVENPARITY;
dDCB.StopBits = 2;
SetCommState(
hPort,
&dDCB);
while(dw != 3)
{
ReadFile(
hPort,
&buffer,
1,
&RealRead,
NULL);
t[dw] = buffer;
dw++;
}
CloseHandle(hPort);
getch();
}
Во-первых, ты не все ошибки исправил. В частности, здесь:
if (hPort != INVALID_HANDLE_VALUE) KillCom;