📄 tserial_event.cpp
字号:
/* ------------------------------------------------------------------------ --
-- --
-- PC serial port connection object --
-- for event-driven programs --
-- --
-- --
-- --
-- Copyright @ 2001-2002 Thierry Schneider --
-- thierry@tetraedre.com --
-- --
-- --
-- --
-- ------------------------------------------------------------------------ --
-- --
-- Filename : Tserial_event.cpp --
-- Author : Thierry Schneider --
-- Created : April 4th 2000 --
-- Modified : June 22nd 2002 --
-- Plateform: Windows 95, 98, NT, 2000, XP (Win32) --
-- ------------------------------------------------------------------------ --
-- --
-- This software is given without any warranty. It can be distributed --
-- free of charge as long as this header remains, unchanged. --
-- --
-- ------------------------------------------------------------------------ --
-- --
-- 01.04.24 Comments added --
-- 01.04.28 Bug 010427 corrected. OnDisconnectedManager was not --
-- initialized --
-- 01.04.28 connect() function prototype modified to handle 7-bit --
-- communication --
-- 01.04.29 "ready" field added to remove a bug that occured during --
-- reconnect (event manager pointers cleared) --
-- I removed the "delete" in Tserial_event_thread_start --
-- because it was destroying the object even if we would --
-- use it again --
-- --
-- 02.01.30 Version 2.0 of the serial event object --
-- --
-- --
-- 02.06.22 - wait for the thread termination before --
-- quiting or restarting --
-- - "owner" field added to be able to call C++ object from --
-- the event manager routine --
-- - Correction of a bug that occured when receiving data --
-- (setting twice the SIG_READ_DONE event) --
-- --
-- --
-- --
-- ------------------------------------------------------------------------ --
-- --
-- Note to Visual C++ users: Don't forget to compile with the --
-- "Multithreaded" option in your project settings --
-- --
-- See Project settings --
-- | --
-- *--- C/C++ --
-- | --
-- *--- Code generation --
-- | --
-- *---- Use run-time library --
-- | --
-- *---- Multithreaded --
-- --
-- --
-- --
-- ------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------- */
#define STRICT
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <conio.h>
#include <windows.h>
#include "Tserial_event.h"
#define SIG_POWER_DOWN 0
#define SIG_READER 1
#define SIG_READ_DONE 2 // data received has been read
#define SIG_WRITER 3
#define SIG_DATA_TO_TX 4 // data waiting to be sent
#define SIG_MODEM_EVENTS 5
#define SIG_MODEM_CHECKED 6
void Tserial_event_thread_start(void *arg);
typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC) (LPVOID lpThreadParameter);
typedef unsigned *PBEGINTHREADEX_THREADID;
/* ---------------------------------------------------------------------- */
/* --------------------- Tserial_event_thread_start ------------------- */
/* ---------------------------------------------------------------------- */
/**
This function is not part of the Tserial_event object. It is simply used
to start the thread from an external point of the object.
*/
void Tserial_event_thread_start(void *arg)
{
class Tserial_event *serial_unit;
serial_unit = (Tserial_event *) arg;
if (serial_unit!=0)
serial_unit->run();
}
/* -------------------------------------------------------------------- */
/* ------------------------- Tserial_event ------------------------- */
/* -------------------------------------------------------------------- */
Tserial_event::Tserial_event()
{
int i;
ready = false;
parityMode = SERIAL_PARITY_NONE;
port[0] = 0;
rate = 0;
threadid = 0;
serial_handle = INVALID_HANDLE_VALUE;
thread_handle = 0;
owner = 0;
tx_in_progress = 0;
rx_in_progress = 0;
max_rx_size = 1;
tx_size = 0;
received_size = 0;
check_modem = false;
manager = 0;
/* -------------------------------------------------------------- */
// creating Events for the different sources
for (i=0; i<SERIAL_SIGNAL_NBR; i++)
{
if ((i==SIG_READER) || (i==SIG_WRITER) || (i==SIG_MODEM_EVENTS))
serial_events[i] = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
else
serial_events[i] = CreateEvent(NULL, FALSE, FALSE, NULL); // Auto reset
}
}
/* -------------------------------------------------------------------- */
/* -------------------------- ~Tserial_event ----------------------- */
/* -------------------------------------------------------------------- */
Tserial_event::~Tserial_event()
{
int i;
if (thread_handle!=0)
WaitForSingleObject(thread_handle, 2000);
thread_handle = 0;
/* -------------------------------------------------------- */
for (i=0; i<SERIAL_SIGNAL_NBR; i++) // deleting the events
{
if (serial_events[i]!=INVALID_HANDLE_VALUE)
CloseHandle(serial_events[i]);
serial_events[i] = INVALID_HANDLE_VALUE;
}
if (serial_handle!=INVALID_HANDLE_VALUE)
CloseHandle(serial_handle);
serial_handle = INVALID_HANDLE_VALUE;
}
/* -------------------------------------------------------------------- */
/* -------------------------- disconnect ------------------------- */
/* -------------------------------------------------------------------- */
void Tserial_event::disconnect(void)
{
ready = false;
SetEvent(serial_events[SIG_POWER_DOWN]);
if (thread_handle!=0)
WaitForSingleObject(thread_handle, 2000);
thread_handle = 0;
}
/* -------------------------------------------------------------------- */
/* -------------------------- connect ------------------------- */
/* -------------------------------------------------------------------- */
/**
Serial port, file and overlapped structures initialization
*/
int Tserial_event::connect (char *port_arg, int rate_arg, int parity_arg,
char ByteSize , bool modem_events)
{
int erreur;
DCB dcb;
int i;
bool hard_handshake;
COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
/* --------------------------------------------- */
if (serial_handle!=INVALID_HANDLE_VALUE)
CloseHandle(serial_handle);
serial_handle = INVALID_HANDLE_VALUE;
if (port_arg!=0)
{
strncpy(port, port_arg, 10);
rate = rate_arg;
parityMode = parity_arg;
check_modem = modem_events;
erreur = 0;
ZeroMemory(&ovReader ,sizeof(ovReader) ); // clearing the overlapped
ZeroMemory(&ovWriter ,sizeof(ovWriter) );
ZeroMemory(&ovWaitEvent,sizeof(ovWaitEvent));
memset(&dcb,0,sizeof(dcb));
/* -------------------------------------------------------------------- */
// set DCB to configure the serial port
dcb.DCBlength = sizeof(dcb);
/* ---------- Serial Port Config ------- */
dcb.BaudRate = rate;
switch(parityMode)
{
case SERIAL_PARITY_NONE:
dcb.Parity = NOPARITY;
dcb.fParity = 0;
break;
case SERIAL_PARITY_EVEN:
dcb.Parity = EVENPARITY;
dcb.fParity = 1;
break;
case SERIAL_PARITY_ODD:
dcb.Parity = ODDPARITY;
dcb.fParity = 1;
break;
}
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = (BYTE) ByteSize;
// -------------- modified 2005-05-16 ---------------
dcb.fInX = FALSE;
dcb.fOutX = FALSE;
hard_handshake = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -