Nov 3, 2012 at 1:40pm UTC
#include <iostream>
#include <stdarg.h>
#include <windows.h>
#include <stdio.h>
#include <vector.h>
#include <string.h>
#include <cstdlib>
//#include "utils.h"
//#include "stdafx.h"
const int QUEUE_SIZE=512 ;
LPCSTR port = "COM5";
class SERIAL
{
public:
HWND hDbg ;
HANDLE hComPort;
const char* error ;
COMMTIMEOUTS orgTimeouts ;
unsigned char rxbuffer[1024] ;
DWORD rxlen,rxpos ;
void debug(const char* txt)
{
if(hDbg)
{
std::cout<<txt<<std::endl;
//SetWindowText(hDbg,txt) ;
}
}
SERIAL(LPCSTR lpszDevice,int speed,HWND hWnd)
{
error=0 ; rxlen=0 ; rxpos=0 ; hDbg=hWnd ;
try
{
COMMTIMEOUTS timeouts;
DCB comDCB; /* COM-Port Control-Settings */
DWORD dwCommEvents;
hComPort= CreateFile(lpszDevice, GENERIC_READ | GENERIC_WRITE,
0, 0, OPEN_EXISTING, 0, 0);
/* In this application the serial port is used in
* nonoverlapped mode!
*/
if (hComPort == INVALID_HANDLE_VALUE) throw "CreateFile" ;
if (SetupComm(hComPort, QUEUE_SIZE, QUEUE_SIZE) == 0) throw "SetupComm" ;
/* Save original timeout values: */
GetCommTimeouts(hComPort, &orgTimeouts);
/* Set Windows timeout values (disable build-in timeouts): */
timeouts.ReadIntervalTimeout= 0;
timeouts.ReadTotalTimeoutMultiplier= 0;
timeouts.ReadTotalTimeoutConstant= 0;
timeouts.WriteTotalTimeoutMultiplier= 0;
timeouts.WriteTotalTimeoutConstant= 0;
if (!SetCommTimeouts(hComPort, &timeouts)) throw "SetCommTimeouts" ;
dwCommEvents= EV_RXCHAR | EV_TXEMPTY | EV_RXFLAG | EV_ERR;
SetCommMask(hComPort, dwCommEvents);
/* Get state and modify it: */
if (!GetCommState(hComPort, &comDCB)) throw "GetCommState" ;
comDCB.BaudRate = speed ; // variable speed here
comDCB.ByteSize = 8 ;
comDCB.Parity = NOPARITY ;
comDCB.StopBits = ONESTOPBIT ;
comDCB.fBinary = FALSE ; /* Enable Binary Transmission */
comDCB.fParity = FALSE ; /* Enable Parity Check */
comDCB.ErrorChar = (char)0xff ;
comDCB.fRtsControl = RTS_CONTROL_DISABLE ; /* For power supply */
comDCB.fDtrControl = DTR_CONTROL_DISABLE ; /* For power supply */
comDCB.fOutxCtsFlow= FALSE ;
comDCB.fOutxDsrFlow= FALSE ;
comDCB.fOutX = FALSE ;
comDCB.fInX = FALSE ;
comDCB.fNull = FALSE ;
comDCB.fErrorChar = FALSE ;
// Tx and Rx XON character
if (!SetCommState(hComPort, &comDCB)) throw "SetCommState" ;
/* Clear buffers: */
PurgeComm(hComPort, PURGE_TXCLEAR | PURGE_TXABORT);
PurgeComm(hComPort, PURGE_RXCLEAR | PURGE_RXABORT);
debug("port created") ;
}
catch(const char* err)
{ error=err ;
if(hComPort!=INVALID_HANDLE_VALUE)
{ CloseHandle(hComPort) ; hComPort=INVALID_HANDLE_VALUE ; }
}
}
~SERIAL()
{
if(hComPort!=INVALID_HANDLE_VALUE)
{ // PurgeComm // Alle Puffer zurücksetzen
PurgeComm(hComPort, PURGE_TXCLEAR | PURGE_TXABORT) ;
PurgeComm(hComPort, PURGE_RXCLEAR | PURGE_RXABORT) ;
SetCommTimeouts(hComPort, &orgTimeouts) ;
CloseHandle(hComPort) ;
}
}
bool ok()
{
return hComPort!=INVALID_HANDLE_VALUE ;
}
const char* what_error()
{
return error ;
}
void tx(unsigned char ch)
{
BOOL success = 0;
debug("txin") ;
DWORD dwWrite ;
success = WriteFile(hComPort,&ch,3,&dwWrite,NULL) ;
std::cout<<"Open success : "<<success<<std::endl;
debug("txout") ;
}
int SERIAL::rx(int timeout)
{
if(rxpos>=rxlen)
{
debug("rxin") ;
int rxCount=0,startTime=GetTickCount() ;
for(bool wait=true;wait;)
{ DWORD errors=0 ;
COMSTAT comState ; /* COM-Port Status-Information */
ClearCommError(hComPort, &errors, &comState) ;
debug("checking") ;
rxCount=comState.cbInQue ; /* dirty time consuming section */
if(rxCount>0 || timeout==0 || (int)GetTickCount()-startTime>=timeout)
{
wait=false ;
}
}
if(rxCount>=1024)
{
rxCount=1024 ;
}
if(rxCount>0)
{
rxpos=0 ; rxlen=0 ;
debug("reading") ;
bool ok=(ReadFile(hComPort,rxbuffer,rxCount,&rxlen, NULL)==TRUE) ;
Beep(999,200);
if(!ok || rxlen==0) throw "ReadFile failed" ;
}
debug("rxout") ;
}
for(int i=0;i<rxlen;i++)
{
std::cout <<rxbuffer[i];
}
std::cout<<std::endl;
return (rxpos<rxlen) ? rxbuffer[rxpos++] : -1 ;
}
} ;
class FEEDER
{
public:
SERIAL* port ;
int dir,pos ;
char input[4096] ;
//PROTOCOLLER& proto ;
//STATUS& sts,prs ;
void out(int chan,int ch) ;
void tx(unsigned char ch) ;
int rx(int timeout) ;
public:
// FEEDER(PROTOCOLLER& prt,STATUS& s,STATUS& p) ;
void setPort(SERIAL* p,const char* name,int speed) ;
bool ok()
{
return port!=0 ;
}
void flush() ;
void transmit(LPTSTR str) ;
void putline(LPTSTR line) ;
void putdataline(LPTSTR line) ;
bool getline(LPTSTR line,int max,int dly) ;
bool expect(LPCTSTR str,int dly) ;
int multiexpect(LPCTSTR str[],int dly) ;
} ;
int main()
{
HWND hwnd;
BYTE text[3];
int read =0;
text[0] = '@';
text[1] = 's';
text[2] = 'v';
int i=0;
SERIAL rs232(port,9600,hwnd);
std::cout<<"Befehl senden ? PRESS<RETURN>\n"<<std::endl;
system("PAUSE");
while(text[i]!=0)
{
rs232.tx(text[i]);
i++;
}
DWORD Errors;
COMSTAT a;
read = rs232.rx(3000);
std::cout<<"Zeichen im Puffer : "<<ClearCommError (hwnd, &Errors,&a )<<" "<<Errors<<std::endl;
rs232.~SERIAL();
std::cout<<"Return rx : "<<read<<std::endl;
return 0;
}
Sende Zeichen zu einem uController und warte auf seine Antwort.Doch ich emfange nix.Auf eienm TerminalProgramm läuft alles reibunglos.
Hat jemand eine Idee woran es liegt ???
Bitte um eure Hilfe Danke
Last edited on Nov 3, 2012 at 1:40pm UTC