C++ Builder
| | | | FAQ | | Downloads | | | RXLib | |

 , ( | )   | | |
  :   y   
     
 
EAccessViolation,
« | » | E-mail |
polizei
: 22.08.2006, 01:05


-


:
: 2
2014
: 22.08.06



CODE

int main(int argc, char* argv[])
{
int data;
unsigned short TransBytes;
MSerial* com = new MSerial;
cout << "Enter data number to write (from 0 to 31): ";
cin >> data;
if(com->Open("COM1", 1200, 5, 'N', 1))
{
TransBytes = com -> Write((BYTE*)&data, 1);
}
else {cout << "Port not opened";}

com->Close();
delete com;
getch();
return 0;
}

class MSerial
{
public:
MSerial();
~MSerial();
boolean Open(char port[], int BaudRate, int ByteSize,
char Parity, int StopBits);
void Close(void);
unsigned short Read(BYTE *buffer, unsigned short numbytes);
unsigned short Write(BYTE *buffer, unsigned short numbytes);
private:
HANDLE handle;
COMMTIMEOUTS CommTimeOuts;
DCB dcb;
COMSTAT ComState;
OVERLAPPED Overlap;
char String_Mode[];

};

MSerial::MSerial()
{
};
MSerial::~MSerial()
{
};
boolean MSerial::Open(char port[], int BaudRate, int ByteSize,
char Parity, int StopBits)
{
handle = CreateFile
(
port,
GENERIC_READ, //| GENERIC_WRITE,
NULL,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);

if(handle == INVALID_HANDLE_VALUE){return FALSE;}
dcb.DCBlength = sizeof(DCB);
if(!BuildCommDCB("1200,n,5,1", &dcb))
{
cout << "DCB faild" << endl;
}
CommTimeOuts.ReadIntervalTimeout = TimeoutInterval;
CommTimeOuts.ReadTotalTimeoutMultiplier = NumBytesMultiplier;
CommTimeOuts.ReadTotalTimeoutConstant = Constant;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;

if(!SetCommState(handle, &dcb))
{
return FALSE;
}
if(!SetCommTimeouts(handle, &CommTimeOuts))
{
cout << "Configure faild" << endl;
return FALSE;
}
return TRUE;
};

void MSerial::Close(void)
{
if(!CloseHandle(handle)){cout << "Port not closed" << endl;}
}

unsigned short MSerial::Read(BYTE *buffer, unsigned short numbytes)
{
DWORD temp;
ClearCommError(handle, &temp, &ComState);
if (!temp)
{
boolean result=ReadFile(handle, buffer, numbytes, &temp, &Overlap);
if(result)
{
return (unsigned short)temp;
}
}
return 0;
}

unsigned short MSerial::Write(BYTE* buffer, unsigned short numbytes)
{
DWORD temp;
if(numbytes)
{
ClearCommError(handle, &temp, &ComState);
if((numbytes + ComState.cbOutQue)>=SizeBuffer)
MessageBox(NULL, "Íå õâàòàåò ñâîáîäíîãî ìåñòà â âûõîäíîì áóôåðå ",
"", MB_OK);
boolean result=WriteFile(handle, buffer, numbytes, &temp, &Overlap);
if(result)
{
return (unsigned short)temp;
}
}
return 0;
}

COM-. delete exception EAccessViolation. . , Help , ,

polizei - 22/08/2006, 23:24
 
  E-mail   Top
avc*
: 22.08.2006, 08:00


Unregistered









[QUOTE]
COM-. delete exception EAccessViolation. . , Help , ,
[QUOTE]
.

PS. , :
- , - .
- -.
 
Top
1 : 22.08.2006, 01:05 | E-mail |

Back to