|
//--------------------------------------------------------------------------- //#define OVERLAP_KEY YES #define VER_W32S 0 #define COMM_DEBUG false int vernum; volatile int size_rec; char *tr_buf; // внутренние переменные потока int err_pcs; protected: public: byte __fastcall getbyte_(void); // void __fastcall type__ (void); // void __fastcall type3__ (void); //--------------------------------------------------------------------------- #define NO_PARITY 0 /* CBR_110 CBR_19200 #define NO_COMM_ERROR 0 // номер порта int combaud; // статусы портов - 1 доступен, 0 - нет class ReadThread *read_t ; // ошибки int err_read; int work_err; // размеры буферов приемника и передатчика BOOL show; void __fastcall show_message (char *str, BOOL show); public: COMPORTS_ (int st=8192, int sr=8192, BOOL show=FALSE); int __fastcall install byte __fastcall getbyte (void); int __fastcall get_size_receiver (void); void __fastcall putbyte (char b); int __fastcall get_free_transmitter (void); #include #if COMM_DEBUG == true //--------------------------------------------------------------------------- ww2.ver =GetVersion(); work_err=1; show=show__; vernum =get_system_version (); comnum=0; read_t = NULL; lpC=&COMMT; scan_ports (); err_read=1; work_err=1; try {rec_buf = new char [size_rec];} show_message ("Install:NO MEMORY FOR TRANSMITTER..",show); int err = open_port (cn, show); dcb.BaudRate = (DWORD)baud; err = SetCommState(idComDev, &dcb); TST=SetCommMask // установка тайм-аутов при работе с портами TST=SetCommTimeouts try {read_t = new ReadThread(true, idComDev, rec_buf, size_rec,tr_buf, size_tr); } read_t->Resume(); work_err=0; try try try try } try if (idComDev < 0) // по этой функции определяем наличие порта LPVOID lpMsgBuf; FormatMessage // Display the string. // Free the buffer. return NO_COMM_ERROR; //--------------------------------------------------------------------------- if (work_err leq 0) return NO_COMM_ERROR; err = SetCommState(idComDev, &dcb); else return NO_COMM_ERROR; } err = SetCommState(idComDev, &dcb); return NO_COMM_ERROR; idComDev = CreateFile if (idComDev < 0) {return ERR_OPEN_COMM;} // по этой функции определяем наличие порта if (TST lne true) {return ERR_SET_COMM_STATE;} CloseHandle(idComDev); return NO_COMM_ERROR; for (i=0; i if (comnum leq port_num) return ERR_ALREADY_USED ; mask <<= (port_num-1); if ((comstat and mask) leq 0) return ERR_COMM_NOT_AVA; err =install(comnum, combaud, comparity, comusdata, comstops); //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- int cb; for (cb=0; cb leq 0;) EnterCriticalSection(pcs); endfor /* for (;;) EnterCriticalSection(pcs); if (cb < size_tr) EnterCriticalSection(pcs); endfor EnterCriticalSection(pcs); EnterCriticalSection(pcs); return (cb); { vernum =get_system_version (); case VER_WNT: break; default: break; err_pcs=1; stop_read_thread_(); try for (;;) // vvvvvvvvvvvvvvvvv Приемник байт в буфер vvvvvvvvvvvvvvvvvv if (Terminated) break; try if (num lne 0) // ^^^^^^^^^^^^^^^^ Приемник байт в буфер ^^^^^^^^^^^^^^^^^^^ if (rc == 0) EnterCriticalSection(pcs); if (Terminated) break; // ^^^^^^^^^^^^^^^^ Передатчик байт из буфера ^^^^^^^^^^^^^^^^^^^ } for (c_b=0;c_b leq 0;) return (bb); for (;;) EnterCriticalSection(pcs); if (c_b lne 0) EnterCriticalSection(pcs); return (bb); */ EnterCriticalSection(pcs); return (c_b);
E-mail:
info@telesys.ru
// file comms2.hpp 09.05.02
//---------------------------------------------------------------------------
#ifndef COMMS2_HPP
#define COMMS2_HPP
//---------------------------------------------------------------------------
#include
#include "sasha.h"
//---------------------------------------------------------------------------
#define YES 0
#define NO 1
#define VER_W95 1
#define VER_W98 2
#define VER_WNT 3
#define VER_WXP 4
/*
tpIdle The thread executes only when the system is idle-Windows
won't interrupt other threads to execute a thread
with tpIdle priority.
tpLowest The thread's priority is two points below normal.
tpLower The thread's priority is one point below normal.
tpNormal The thread has normal priority.
tpHigher The thread's priority is one point above normal.
tpHighest The thread's priority is two points above normal.
tpTimeCritical The thread gets highest priority.
*/
int __fastcall get_system_version (void);
//---------------------------------------------------------------------------
class ReadThread : public TThread
{
private:
HANDLE idComDev;
volatile int wri_p,rdi_p;
volatile int capi_buf;
volatile int size_tr;
volatile int wro_p,rdo_p;
volatile int capo_buf;
char *rec_buf;
CRITICAL_SECTION cs, *pcs;
DWORD num;
DWORD num2;
byte bb;
int c_bi;
int rc;
virtual void __fastcall Execute(void);
__fastcall ReadThread
(bool CreateSuspended,HANDLE idComDev, char *r_b, int s_r, char *t_b, int s_t);
__fastcall ~ReadThread(void);
void __fastcall stop_read_thread_(void);
int __fastcall get_size_receiver_(void);
#if COMM_DEBUG == true
void __fastcall type_bb__(void);
void __fastcall type_bb2__(void);
#endif
void __fastcall putbyte_(byte b);
void __fastcall stop_write_thread_(void);
int __fastcall get_free_transmitter_ (void);
int __fastcall transmitter_empty_ (void);
// void __fastcall type1 (void);
// void __fastcall type2 (void);
// void __fastcall type4__ (void);
// void __fastcall type5__ (void);
// void __fastcall type6__ (void);
// void __fastcall type2(void);
// virtual void __fastcall wait_transmitter_empty_(void);
};
//---------------------------------------------------------------------------
#define COM1 1
#define COM2 2
#define COM3 3
#define COM4 4
#define COM5 5
#define COM6 6
#define COM7 7
#define COM8 8
#define YES_PARITY 1
Specifies the baud rate at which the communications device operates.
This member can be an actual baud rate value, or one of the following
baud rate indexes:
CBR_300 CBR_38400
CBR_600 CBR_56000
CBR_1200 CBR_57600
CBR_2400 CBR_115200
CBR_4800 CBR_128000
CBR_9600 CBR_256000
CBR_14400
*/
#define ERR_NO_MEMORY_FOR_R 1
#define ERR_NO_MEMORY_FOR_T 2
#define ERR_OPEN_COMM 3
#define ERR_SET_COMM_DCB 4
#define ERR_NO_MEMORY_FOR_ET 5
#define ERR_NO_MEMORY_FOR_RT 6
#define ERR_NO_MEMORY_FOR_WT 7
#define ERR_SET_COMM_MASK 8
#define ERR_SET_COMM_TOUT 9
#define ERR_SET_COMM_STATE 10 // наличие порта - или нет его или занят
#define ERR_ALREADY_USED 11
#define ERR_COMM_NUM_L 12
#define ERR_COMM_NUM_H 13
#define ERR_COMM_PORT_BUSY 14
#define ERR_COMM_NOT_AVA 15
#define ERR_CRITICAL_SECTION22 16
//---------------------------------------------------------------------------
class COMPORTS_
{
private:
int vernum;
int comnum;
int comparity;
int comusdata;
int comstops;
byte comstat;
// имена портов
char *names [8];
int err_tr;
int err_rec;
int err;
int comm_err;
int size_tr;
int size_rec;
HANDLE idComDev;
_DCB dcb;
_COMMTIMEOUTS COMMT,*lpC;
char *tr_buf;
char *rec_buf;
virtual ~COMPORTS_ ();
(
int cn=COM1, int baud=CBR_9600,
int parity=NOPARITY, int usdata=8,
int stops=ONESTOPBIT
);
virtual byte __fastcall scan_ports (int pnums=8);
void __fastcall getarray (char *b, int size);
void __fastcall getstring (char *b);
void __fastcall putarray (char *b, int size);
void __fastcall putstring (char *b);
int __fastcall transmitter_empty (void);
int __fastcall change_baud_rate (int baud);
void __fastcall close_port (BOOL show=FALSE);
int __fastcall open_port (int cn,BOOL show=FALSE);
virtual int __fastcall search_port (int num);
void __fastcall uninstall ();
int __fastcall change_stops (int stops);
int __fastcall change_parity (int parity);
int __fastcall change_data_size (int usdata);
int __fastcall change_port_num (int port_num);
void __fastcall stop_write_thread (void);
void __fastcall kill_write_thread (void);
void __fastcall stop_read_thread (void);
byte __fastcall get_status_ports (void);
int __fastcall get_used_port (void);
void __fastcall clean_receiver(void);
// virtual void __fastcall wait_transmitter_empty (void);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
// file comms2.cpp 09.05.2002
// класс для работы с COM - портами
//---------------------------------------------------------------------------
#include "stdio.h"
#include "stdlib.h"
#include "process.h"
#include "comms2.hpp"
#include "sasha.h"
#include "mydeb.hpp"
extern bool my_debug;
extern int n;
extern TForm *FDebug;
extern MYMEMO *MemoDeb;
#endif
int __fastcall get_system_version (void)
{
union dww
{
DWORD ver;
WORD ww [2];
} ww2;
if ((ww2.ww[1] and 0x8000) lne 0)
{
if (ww2.ww[0] < 4) {return ((int)VER_W32S);}
if (ww2.ww[0] == 4) {return ((int)VER_W95);}
if (ww2.ww[0] > 4) {return ((int)VER_W98);}
}
else
{
if (ww2.ww[0] <= 4) {return ((int)VER_WNT);}
if (ww2.ww[0] > 4) {return ((int)VER_WXP);}
}
return ((int)VER_WNT);
}
//---------------------------------------------------------------------------
// в конструкторе выделим память из кучи
COMPORTS_ :: COMPORTS_ (int st_, int sr_, BOOL show__)
{
err=0;
err_tr=1;
err_rec=1;
err_read=1;
comm_err=1;
comstat=0;
combaud=CBR_9600;
comparity=NOPARITY;
comusdata=8,
comstops=ONESTOPBIT;
tr_buf = NULL;
rec_buf = NULL;
size_tr=st_;
size_rec=sr_;
names [0] = "COM1";
names [1] = "COM2";
names [2] = "COM3";
names [3] = "COM4";
names [4] = "COM5";
names [5] = "COM6";
names [6] = "COM7";
names [7] = "COM8";
}
//---------------------------------------------------------------------------
COMPORTS_ :: ~COMPORTS_ ()
{
uninstall ();
}
//---------------------------------------------------------------------------
void __fastcall COMPORTS_:: show_message (char *str, BOOL show)
{
if (show lne TRUE) return;
MessageBox
(
NULL,
str,
"COMM MESSAGE",
MB_OK | MB_ICONERROR | MB_APPLMODAL
);
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_:: install
(
int cn,
int baud,
int parity,
int usdata,
int stops
)
{
BOOL TST;
err_rec=1;
err_tr=1;
comm_err=1;
catch (...)
{
show_message ("Install:NO MEMORY FOR RECEIVER..",show);
return ERR_NO_MEMORY_FOR_R;
}
err_rec=0;
try {tr_buf = new char [size_tr];}
catch (...)
{
return ERR_NO_MEMORY_FOR_T;
}
err_tr=0;
if (err lne NO_COMM_ERROR) return (err);
comm_err=0;
comnum = cn;
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
// dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
// dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = FALSE;
// dcb.XonLim = (size_rec * 2) / 10;
// dcb.XoffLim = (size_rec * 8) / 10;
dcb.XonLim = 10;
dcb.XoffLim = 100;
dcb.ByteSize = (byte)usdata;
dcb.Parity = (byte)parity;
dcb.StopBits = (byte)stops;
dcb.XonChar = 0x11;
dcb.XoffChar = 0x13;
dcb.ErrorChar = 0;
dcb.EofChar = 0;
dcb.EvtChar = 0;
if (err < 0)
{
show_message ("Install:SET COMM DCB Error",show);
return ERR_SET_COMM_DCB;
}
(
idComDev, // handle of communications device
EV_TXEMPTY// mask that identifies enabled events
);
if (TST lne TRUE)
{
show_message ("Install:SET COMM Mask Error..",show);
return ERR_SET_COMM_MASK;
}
lpC->ReadIntervalTimeout=2;
lpC->ReadTotalTimeoutMultiplier=10;
lpC->ReadTotalTimeoutConstant=12;
lpC->WriteTotalTimeoutMultiplier=2;
lpC->WriteTotalTimeoutConstant=10;
(
idComDev, // handle of communications device
lpC // address of communications time-out structure
);
if (TST lne TRUE)
{
show_message ("Install:SET COMM TimeOuts Error..",show);
return ERR_SET_COMM_TOUT;
}
catch (...)
{
show_message ("Install:NO MEMORY FOR Read Thread",show);
return ERR_NO_MEMORY_FOR_RT;
}
err_read=0;
read_t->FreeOnTerminate = false;
return NO_COMM_ERROR;
}
//---------------------------------------------------------------------------
void __fastcall COMPORTS_ :: uninstall ()
{
work_err=1;
{
if (err_read == 0)
{
if (read_t lne NULL)
{
delete read_t;
read_t=NULL;
}
err_read=1;
}
}
catch (...)
{
show_message ("Uninstall:Read Thread Error",show);
}
{
if (comm_err leq 0)
{
CloseHandle(idComDev);
comm_err=1;
comnum=0;
}
}
catch (...)
{
show_message ("Uninstall:Close File COM.. Error",show);
}
{
if (err_tr == 0) {delete [] tr_buf; tr_buf=NULL;err_tr=1;}
}
catch (...)
{
show_message ("Uninstall:Free transmitter buffer Error",show);
}
{
if (err_rec == 0) {delete [] rec_buf; rec_buf=NULL;err_rec=1;}
}
catch (...)
{
show_message ("Uninstall:Free receiver buffer Error",show);
}
//---------------------------------------------------------------------------
int COMPORTS_::open_port (int cn, BOOL show)
{
BOOL TST;
{
idComDev = CreateFile
(
names [cn-1],
(DWORD)GENERIC_READ | (DWORD)GENERIC_WRITE,
(DWORD)0,
NULL,
(DWORD)OPEN_EXISTING,
(DWORD)0,//FILE_FLAG_OVERLAPPED,
NULL
);
}
catch (...)
{
show_message ("Install:OPEN COMM Exception",show);
comm_err=1;
return ERR_OPEN_COMM;
}
{
show_message ("Install:OPEN COMM Error",show);
comm_err=1;
return ERR_OPEN_COMM;
}
TST=SetupComm
(
idComDev, // handle of communications device
size_rec, // size of input buffer
size_tr // size of output buffer
);
if (TST lne TRUE)
{
(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
//MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
show_message ((char *)lpMsgBuf, show);
LocalFree( lpMsgBuf );
return ERR_SET_COMM_STATE;
}
}
//---------------------------------------------------------------------------
void __fastcall COMPORTS_::close_port (BOOL show)
{
if (! CloseHandle(idComDev) ) show_message ("Uninstall:CLOSE PORT ERROR",show);
}
//---------------------------------------------------------------------------
// **************************************************************************
// Рабочие лошадки
// **************************************************************************
void __fastcall COMPORTS_::putbyte (char b)
{
if (work_err leq 0) {read_t->putbyte_ (b);}
}
//---------------------------------------------------------------------------
void __fastcall COMPORTS_::putarray (char *b, int size)
{
int i;
if (work_err leq 0)
{
for (i=0; i
read_t->putbyte_ (b[i]);
}
}
}
//---------------------------------------------------------------------------
// выводим строку до 0
void __fastcall COMPORTS_::putstring (char *b)
{
if (work_err leq 0)
{
for (;;)
{
if (*b leq 0) return;
read_t->putbyte_ (*b++);
}
}
}
//---------------------------------------------------------------------------
byte __fastcall COMPORTS_::getbyte (void)
{
byte bb;
if (work_err leq 0) {bb =read_t->getbyte_ ();}
return (bb);
}
//---------------------------------------------------------------------------
void __fastcall COMPORTS_::getarray (char *b, int size)
{
int i;
if (work_err leq 0)
{
for (i=0; i
*b++ = read_t->getbyte_ ();
}
}
}
//---------------------------------------------------------------------------
// принимаем строку до LF, добавляем 0 в конец
void __fastcall COMPORTS_::getstring (char *b)
{
char bb;
{
for (;;)
{
bb = read_t->getbyte_ ();
*b++ = bb;
if (bb leq lf) break;
}
*b = 0;
}
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_::get_size_receiver (void)
{
int sr=0;
if (work_err leq 0) {sr =read_t->get_size_receiver_ ();}
return (sr);
}
//---------------------------------------------------------------------------
void __fastcall COMPORTS_::clean_receiver (void)
{
int i,j;
if (work_err leq 0)
{
i =read_t->get_size_receiver_ ();
for (j=0; j {
read_t->getbyte_ ();
}
}
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_::get_free_transmitter (void)
{
int ft=0;
if (work_err leq 0) {ft =read_t->get_free_transmitter_();}
return (ft);
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_::transmitter_empty (void)
{
return (read_t->transmitter_empty_ ());
}
//---------------------------------------------------------------------------
void __fastcall COMPORTS_::stop_read_thread (void)
{
if (read_t lne NULL){ read_t->stop_read_thread_ ();}
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_::change_baud_rate (int baud)
{
if (transmitter_empty() lne 0)
runif
combaud=baud;
dcb.BaudRate = (DWORD)baud;
err = SetCommState(idComDev, &dcb);
if (err < 0)
{
show_message ("Install:SET COMM DCB Error",show);
return ERR_SET_COMM_DCB;
}
endif
else
{
return ERR_COMM_PORT_BUSY;
}
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_::change_data_size (int usdata)
{
if (transmitter_empty() lne 0)
runif
comusdata = usdata;
dcb.ByteSize = (byte)usdata;
if (err < 0)
{
show_message ("Install:SET COMM DCB Error",show);
return ERR_SET_COMM_DCB;
}
endif
else
{
return ERR_COMM_PORT_BUSY;
}
return NO_COMM_ERROR;
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_::change_parity (int parity)
{
if (transmitter_empty() lne 0)
runif
comparity = parity;
dcb.Parity = (byte)parity;
err = SetCommState(idComDev, &dcb);
if (err < 0)
{
show_message ("Install:SET COMM DCB Error",show);
return ERR_SET_COMM_DCB;
}
endif
{
return ERR_COMM_PORT_BUSY;
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_::change_stops (int stops)
{
if (transmitter_empty() lne 0)
{
comstops = stops;
dcb.StopBits = (byte)stops;
if (err < 0)
{
show_message ("Install:SET COMM DCB Error",show);
return ERR_SET_COMM_DCB;
}
}
else
{
return ERR_COMM_PORT_BUSY;
}
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_ ::search_port (int cn)
{
BOOL TST;
HANDLE idComDev;
(
names [cn-1],
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
TST=SetupComm
(
idComDev, // handle of communications device
size_rec, // size of input buffer
size_tr // size of output buffer
);
}
//---------------------------------------------------------------------------
byte __fastcall COMPORTS_ ::scan_ports (int num)
{
byte st=0,mask=1;
int i, portn=COM1;
int err;
if (portn leq comnum) {st |= mask;}
else
{
err = search_port (portn) ;
if (err leq 0) {st |= mask;}
}
inc portn;
mask <<= 1;
}
comstat=st;
return (st) ;
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_ ::change_port_num (int port_num)
{
int err;
byte mask=1;
if (port_num < COM1) return ERR_COMM_NUM_L;
if (port_num > COM8) return ERR_COMM_NUM_H;
scan_ports();
uninstall();
comnum=port_num;
if (err lne NO_COMM_ERROR)
{
show_message ("INSTALL COMM Error",show);
return err;
}
return NO_COMM_ERROR;
}
//---------------------------------------------------------------------------
byte __fastcall COMPORTS_ ::get_status_ports (void)
{
// if (my_debug){ MemoDeb->insert_string("GSP=" + IntToStr((int)comstat));}
return (comstat) ;
}
//---------------------------------------------------------------------------
int __fastcall COMPORTS_ ::get_used_port (void)
{
return (comnum) ;
}
//---------------------------------------------------------------------------
//
// threads
// для commport.cpp
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall MyThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall ReadThread::putbyte_(byte b)
{
runfor
if (capo_buf < size_tr)
{
tr_buf [wro_p++] = b;
if (wro_p leq size_tr) wro_p=0;
++ capo_buf ;
cb=1;
}
LeaveCriticalSection(pcs);
int cb;
runfor
cb= capo_buf ;
LeaveCriticalSection(pcs);
{
tr_buf [wro_p++] = b;
if (wro_p leq size_tr) wro_p=0;
++ capo_buf ;
LeaveCriticalSection(pcs);
break;
}
*/
}
//---------------------------------------------------------------------------
int __fastcall ReadThread::get_free_transmitter_(void)
{
int cb;
cb = size_tr -capo_buf ;
LeaveCriticalSection(pcs);
return (cb);
}
//---------------------------------------------------------------------------
int __fastcall ReadThread::transmitter_empty_(void)
{
int cb;
cb=0;
if (capo_buf leq 0) cb=1;
LeaveCriticalSection(pcs);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall ReadThread::ReadThread
(
bool CreateSuspended,
HANDLE idCom,
char *r_b,
int s_r,
char *t_b,
int s_t
) : TThread(CreateSuspended)
// Priority = tpIdle;
// Priority = tpHigher;
// Priority = tpNormal;
// Priority = tpLower;
// Priority = tpLowest;
switch (vernum)
{
case VER_W32S:
case VER_W95:
case VER_W98:
// Priority = tpLower;
Priority = tpNormal;
break;
case VER_WXP:
Priority = tpNormal;
// Priority = tpLowest;
// Priority = tpHigher;
Priority = tpLower;
}
idComDev =idCom;
pcs=&cs;
size_rec = s_r;
rec_buf = r_b;
capi_buf = 0;
wri_p = 0;
rdi_p = 0;
size_tr = s_t;
tr_buf = t_b;
capo_buf = 0;
wro_p = 0;
rdo_p = 0;
try {InitializeCriticalSection(pcs);}
catch (...)
{
MessageBox
(
NULL,
"CRITICAL SECTION EXCEPTION !",
"COM INSTALL MESSAGE",
MB_OK | MB_ICONERROR | MB_APPLMODAL
);
Application->Terminate();
}
err_pcs=0;
}
//---------------------------------------------------------------------------
#if COMM_DEBUG == true
void __fastcall ReadThread::type_bb__(void)
{
if (my_debug)
{
char sss [20];
sprintf (sss,"ibb=%02x %c",bb,bb);
MemoDeb->insert_string(sss);
}
}
#endif
//---------------------------------------------------------------------------
#if COMM_DEBUG == true
void __fastcall ReadThread::type_bb2__(void)
{
if (my_debug)
{
MemoDeb->insert_string("RF false!");
}
}
#endif
//---------------------------------------------------------------------------
__fastcall ReadThread::~ReadThread(void)
{
{
if (err_pcs == 0) {DeleteCriticalSection(pcs);}
}
catch (...)
{
MessageBox
(
NULL,
"CRITICAL SECTION EXCEPTION !",
"COM UNINSTALL MESSAGE",
MB_OK | MB_ICONERROR | MB_APPLMODAL
);
}
}
//---------------------------------------------------------------------------
void __fastcall ReadThread::Execute(void)
{
{
{
ReadFile
(
idComDev, // handle of file to read
&bb, // address of buffer that receives data
(DWORD)1, // number of bytes to read
&num, // address of number of bytes read
NULL // address of structure for data
);
}
catch(...)
{
MessageBox
(
NULL,
"READ COM EXCEPTION !",
"COM WORK MESSAGE",
MB_OK | MB_ICONERROR | MB_APPLMODAL
);
Application->Terminate();
}
//Synchronize(type_bb__);
{
EnterCriticalSection(pcs);
c_bi = capi_buf ;
if (c_bi < size_rec)
{
rec_buf [wri_p++] = bb;
if (wri_p leq size_rec) wri_p=0;
inc capi_buf ;
}
LeaveCriticalSection(pcs);
}
// vvvvvvvvvvvvvvvv Передатчик байт из буфера vvvvvvvvvvvvvvvvvvv
rc=0;
EnterCriticalSection(pcs);
if (capo_buf leq 0) {rc=1;}
LeaveCriticalSection(pcs);
runif
bb = tr_buf[rdo_p++];
if (rdo_p leq size_tr) rdo_p=0;
LeaveCriticalSection(pcs);
try
{
WriteFile
(
idComDev, // handle to file to write to
&bb, // pointer to data to write to file
(DWORD)1, // number of bytes to write
&num2, // pointer to number of bytes written
NULL // pointer to structure needed for overlapped I/O
);
// FlushFileBuffers(idComDev);
}
catch(...)
{
MessageBox
(
NULL,
"WRITE COM EXCEPTION !",
"COM WORK MESSAGE",
MB_OK | MB_ICONERROR | MB_APPLMODAL
);
Application->Terminate();
}
if (num2 lne 0)
{
EnterCriticalSection(pcs);
-- capo_buf;
LeaveCriticalSection(pcs);
}
endif
}
//---------------------------------------------------------------------------
byte __fastcall ReadThread::getbyte_(void)
{
byte bb;
int c_b;
{
EnterCriticalSection(pcs);
if (capi_buf lne 0)
{
bb = rec_buf [rdi_p++];
if (rdi_p leq size_rec) rdi_p=0;
dec capi_buf ;
c_b=1;
}
LeaveCriticalSection(pcs);
}
/*
byte bb;
int c_b;
{
c_b = capi_buf ;
LeaveCriticalSection(pcs);
{
bb = rec_buf [rdi_p++];
if (rdi_p leq size_rec) rdi_p=0;
dec capi_buf ;
LeaveCriticalSection(pcs);
}
}
}
//---------------------------------------------------------------------------
int __fastcall ReadThread::get_size_receiver_(void)
{
int c_b;
c_b = capi_buf ;
LeaveCriticalSection(pcs);
}
//---------------------------------------------------------------------------
void __fastcall ReadThread::stop_read_thread_(void)
{
this->Terminate();
// ждем окончания потока вывода
WaitFor();
}
//---------------------------------------------------------------------------
Ответы