Hi bojo,
I found a partially implemented win32 com port handling class of mine, perhaps something like this could get you started:
Comport.h
#ifndef __COMPORT_H_
#define __COMPORT_H_
typedef unsigned char comportBits;
#undef PARITY_NONE
#undef PARITY_ODD
#undef PARITY_EVEN
#undef PARITY_MARK
#undef PARITY_SPACE
typedef enum { // as xxxPARITY in winbase.h
PARITY_NONE = 0x00,
PARITY_ODD = 0x01,
PARITY_EVEN = 0x02,
PARITY_MARK = 0x03,
PARITY_SPACE = 0x04,
} comportParity;
typedef enum { // as xxxSTOPBIT(S) in winbase.h
STOP_1 = 0x00,
STOP_1_5 = 0x01,
STOP_2 = 0x02
} comportStop;
typedef enum { // as DTR_CONTROL_xxx in winbase.h
DTR_LOW = 0x00,
DTR_HIGH = 0x01,
DTR_HANDSHAKE = 0x02
} comportDTR;
typedef enum { // as RTS_CONTROL_xxx in winbase.h
RTS_LOW = 0x00,
RTS_HIGH = 0x01,
RTS_HANDSHAKE = 0x02,
RTS_TOGGLE = 0x03
} comportRTS;
class ComPort {
private:
HANDLE hComHandle;
protected:
void init(const char *port, int baudrate, comportBits bits, comportParity parity,
comportStop stop, comportDTR dtr, comportRTS rts);
public:
ComPort(const char *port);
ComPort(const char *port, int baudrate);
ComPort(const char *port, int baudrate, comportBits bits, comportParity parity,
comportStop stop, comportDTR dtr, comportRTS rts);
~ComPort();
void setBaud(int baudrate);
void setParity(comportParity parity);
void setBits(comportBits bits);
void setStop(comportStop stop);
void setDTR(comportDTR dtr);
void setRTS(comportRTS cts);
void txChar(unsigned char byte);
void txStringN(int len, const char *str);
void txString(const char *str);
int rxStatus();
unsigned char rxChar();
};
#endif
Comport.cpp
#include <windows.h>
#include "ComPort.h"
ComPort::ComPort(const char *port)
{
init(port,9600,8,PARITY_NONE,STOP_2,DTR_HIGH,RTS_HIGH);
}
ComPort::ComPort(const char *port, int baudrate)
{
init(port,baudrate,8,PARITY_NONE,STOP_2,DTR_HIGH,RTS_HIGH);
}
ComPort::ComPort(const char *port, int baudrate, comportBits bits, comportParity parity,
comportStop stop, comportDTR dtr, comportRTS rts)
{
init(port,baudrate,bits,parity,stop,dtr,rts);
}
void ComPort::init(const char *port, int baudrate, comportBits bits, comportParity parity,
comportStop stop, comportDTR dtr, comportRTS rts)
{
if (!port) {
hComHandle = INVALID_HANDLE_VALUE;
return;
}
char device[MAX_PATH];
strcpy(device,"\\\\.\\"); strcat(device,port);
hComHandle = CreateFileA(device,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (hComHandle == INVALID_HANDLE_VALUE)
throw win32_exception::win32_exception(GetLastError());
if (!SetupComm(hComHandle,4096,1024)) {
DWORD err = GetLastError();
CloseHandle(hComHandle); hComHandle = INVALID_HANDLE_VALUE;
throw win32_exception::win32_exception(err);
}
DCB dcb;
memset(&dcb,0,sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = baudrate;
dcb.fBinary = TRUE;
dcb.fParity = (parity == PARITY_NONE) ? 0 : 1;
dcb.ByteSize = bits;
dcb.Parity = parity;
dcb.StopBits = stop;
dcb.fDtrControl = dtr;
dcb.fRtsControl = rts;
if (!SetCommState(hComHandle,&dcb)) {
DWORD err = GetLastError();
CloseHandle(hComHandle); hComHandle = INVALID_HANDLE_VALUE;
throw win32_exception::win32_exception(err);
}
COMMTIMEOUTS ctmo;
ctmo.ReadIntervalTimeout = 0;
ctmo.ReadTotalTimeoutMultiplier = 0;
ctmo.ReadTotalTimeoutConstant = 0;
ctmo.WriteTotalTimeoutMultiplier = 0;
ctmo.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(hComHandle,&ctmo)) {
DWORD err = GetLastError();
CloseHandle(hComHandle); hComHandle = INVALID_HANDLE_VALUE;
throw win32_exception::win32_exception(err);
}
}
ComPort::~ComPort()
{
if (hComHandle != INVALID_HANDLE_VALUE)
CloseHandle(hComHandle);
}
void ComPort::setBaud(int baudrate)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(hComHandle,&dcb);
dcb.BaudRate = baudrate;
SetCommState(hComHandle,&dcb);
}
void ComPort::setBits(comportBits bits)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(hComHandle,&dcb);
dcb.ByteSize = bits;
SetCommState(hComHandle,&dcb);
}
void ComPort::setParity(comportParity parity)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(hComHandle,&dcb);
dcb.Parity = parity;
SetCommState(hComHandle,&dcb);
}
void ComPort::setStop(comportStop stop)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(hComHandle,&dcb);
dcb.StopBits = stop;
SetCommState(hComHandle,&dcb);
}
void ComPort::setDTR(comportDTR dtr)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(hComHandle,&dcb);
dcb.fDtrControl = dtr;
SetCommState(hComHandle,&dcb);
}
void ComPort::setRTS(comportRTS rts)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(hComHandle,&dcb);
dcb.fRtsControl = rts;
SetCommState(hComHandle,&dcb);
}
void ComPort::txChar(unsigned char byte)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DWORD nb;
WriteFile(hComHandle,&byte,1,&nb,NULL);
}
void ComPort::txStringN(int len, const char *str)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DWORD nb;
WriteFile(hComHandle,str,len,&nb,NULL);
}
void ComPort::txString(const char *str)
{
if (hComHandle == INVALID_HANDLE_VALUE)
return;
DWORD nb;
WriteFile(hComHandle,str,strlen(str),&nb,NULL);
}
int ComPort::rxStatus()
{
if (hComHandle == INVALID_HANDLE_VALUE)
return 0;
// TODO: implement
return 0;
}
unsigned char ComPort::rxChar()
{
if (hComHandle == INVALID_HANDLE_VALUE)
return 0;
// TODO: implement
return 0;
}
foo_gen_whatever.cpp
#include "../SDK/foobar2000.h"
#include "../helpers/helpers.h"
#include "ComPort.h"
class my_initquit : public initquit, public play_callback {
protected:
ComPort *m_pComPort;
static_api_ptr_t<playback_control> m_playbackControl;
service_ptr_t<titleformat_object> m_titleFormat;
public:
// initquit methods
virtual void on_init()
{
m_pComPort = new ComPort("COM5", 9600, 8, PARITY_NONE, STOP_1, DTR_LOW, RTS_LOW);
static_api_ptr_t<titleformat_compiler>()->compile(m_titleFormat, "$cut(%artist%,32)$char(8)$cut(%title%,32)$char(9)");
static_api_ptr_t<play_callback_manager>()->register_callback(this,
flag_on_playback_new_track | flag_on_playback_dynamic_info | flag_on_playback_dynamic_info_track, true);
}
virtual void on_quit()
{
static_api_ptr_t<play_callback_manager>()->unregister_callback(this);
delete m_pComPort;
}
//---
void updateTrackTitle()
{
pfc::string8_fast str;
if (!m_playbackControl->playback_format_title(NULL, str, m_titleFormat, NULL, playback_control::display_level_titles))
str = "---\x08---\x09";
m_pComPort->txString(str);
}
// play_callback methods
virtual void FB2KAPI on_playback_starting(play_control::t_track_command p_command,bool p_paused) {}
virtual void FB2KAPI on_playback_new_track(metadb_handle_ptr p_track) { updateTrackTitle(); }
virtual void FB2KAPI on_playback_stop(play_control::t_stop_reason p_reason) {}
virtual void FB2KAPI on_playback_seek(double p_time) {}
virtual void FB2KAPI on_playback_pause(bool p_state) {}
virtual void FB2KAPI on_playback_edited(metadb_handle_ptr p_track) {}
virtual void FB2KAPI on_playback_dynamic_info(const file_info & p_info) {}
virtual void FB2KAPI on_playback_dynamic_info_track(const file_info &p_info) { updateTrackTitle(); }
virtual void FB2KAPI on_playback_time(double p_time) {}
virtual void FB2KAPI on_volume_change(float p_new_val) {}
};
static initquit_factory_t<my_initquit> foo0;
DECLARE_COMPONENT_VERSION("component name", "0.0.1", "additional info");
Be warned, I haven't tried to compile this.