Hallo,
versuche gerade, über eine serielle Schnittstelle ein Gerät anzusteuern. Ich würde gerne an das Gerät ein Statusrequest senden. Das ist im letzten Codeausschnitt unter data_status_req zu finden, nur antwortet das Gerät nicht.
Wie kann ich denn überprüfen, ob ich auch das richtige Sende. Irgendwie sagt mir eine Ausgabe nur, dass data_status_req nur 1 BYte groß sei. aber wieso?
ich schicke Euch mal den aktuellen Stand. Vielleicht kann mir ja jemand helfen.Würde mir wirklich weiterhelfen.
vielen Dank Jower
Cport_m_dcb.h:
#ifndef CPORT_M_DCB_H
#define CPORT_M_DCB_H "cport_m_dcb.h"
#include <windows.h>
class ComPort
{
private:
HANDLE hCom;
DCB m_dcb;
public:
enum {OFF,ON};
enum {MAX_PORT = 6};
enum {STATE_ERR = -3, OPEN_ERR, WRONG_PORT, COMM_OK};
ComPort(void);
~ComPort(void);
HANDLE GetHCom(void);
int OpenCom(int portnr);
void CloseCom(void);
BOOL SetUART(void);
BOOL SetUART(DWORD baud, BYTE bytes, BYTE parity, BYTE stopbit);
BOOL SetTimeouts(COMMTIMEOUTS *timeouts);
BOOL SetReadTimeouts(long interval, int multiplier, int constant);
BOOL SetWriteTimeouts(int multiplier, int constant);
BOOL GetUART(DWORD &baud, BYTE &bytes, BYTE &parity, BYTE &stopbit);
unsigned long Send(const char *text);
unsigned long Receive(char *text, DWORD maxsize);
static const char *GetCP(int index);
};
#endif
Cport_m_dcb.cpp
#include "stdafx.h"
#include "cport_m_dcb.h"
ComPort::ComPort(void)
{
m_dcb.DCBlength = sizeof(m_dcb);
hCom=INVALID_HANDLE_VALUE;
}
ComPort::~ComPort(void)
{
CloseCom();
}
HANDLE ComPort::GetHCom(void)
{
return hCom;
}
int ComPort::OpenCom(int portnr)
{
BOOL res;
if(portnr<=0 || portnr>MAX_PORT)
return(WRONG_PORT);
hCom = CreateFile(GetCP(portnr-1), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hCom==INVALID_HANDLE_VALUE)
return(OPEN_ERR);
res=GetCommState(hCom, &m_dcb);
if(!res) {
CloseCom();
return(STATE_ERR);
} //end if
return(COMM_OK);
}
void ComPort::CloseCom(void)
{
CloseHandle(hCom);
hCom=INVALID_HANDLE_VALUE;
}
BOOL ComPort::SetUART(void)//DCB dcb)//funktioniert nicht
{
if(hCom!=INVALID_HANDLE_VALUE)
return(SetCommState(hCom, &m_dcb));
else
return(false);
}
BOOL ComPort::SetUART(DWORD baud, BYTE bytes, BYTE parity, BYTE stopbits)
{
if(hCom!=INVALID_HANDLE_VALUE)
{
m_dcb.BaudRate=baud;
m_dcb.ByteSize=bytes;
m_dcb.Parity=parity;
m_dcb.StopBits=stopbits;
if (SetCommState(hCom, &m_dcb)){
std::cout << "Baudrate: " << m_dcb.BaudRate;
std::cout << " ByteSize: " << m_dcb.ByteSize;
std::cout << " Parity: " << m_dcb.Parity;
std::cout << " StopBits: " << m_dcb.StopBits;
std::cout << "SetUART OK" << std::endl ;
return (true);
}
std::cout << "SetUART Fehler (SetCommState)" << std::endl ;
return(false);
}
else {
std::cout << "SetUART Fehler (INVALID_HANDLE_VALUE)" << std::endl ;
return(false);
}
}
BOOL ComPort::GetUART(DWORD &baud, BYTE &bytes, BYTE &parity, BYTE &stopbit)
{
if(hCom!=INVALID_HANDLE_VALUE)
{
GetCommState(hCom, &m_dcb);
baud = m_dcb.BaudRate;
bytes = m_dcb.ByteSize;
parity = m_dcb.Parity;
stopbit = m_dcb.StopBits;
return(true);
}
else
return(false);
}
BOOL ComPort::SetReadTimeouts(long interval, int multiplier, int constant)
{
if(hCom!=INVALID_HANDLE_VALUE)
{
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout=interval;
timeouts.ReadTotalTimeoutMultiplier=multiplier;
timeouts.ReadTotalTimeoutConstant=constant;
return(SetCommTimeouts(hCom, &timeouts));
}
else
return(false);
}
BOOL ComPort::SetWriteTimeouts(int multiplier, int constant)
{
if(hCom!=INVALID_HANDLE_VALUE)
{
COMMTIMEOUTS timeouts;
timeouts.WriteTotalTimeoutMultiplier=multiplier;
timeouts.WriteTotalTimeoutConstant=constant;
return(SetCommTimeouts(hCom, &timeouts));
}
else
return(false);
}
unsigned long ComPort::Send(const char *text)
{
if(hCom!=INVALID_HANDLE_VALUE)
{
unsigned long sent;
if (WriteFile(hCom, text, (DWORD) strlen(text), &sent, NULL)){
std::cout << "Send OK" << " Daten: " << text << " gesendete Bytes " << sent << std::endl;
std::cout << std::endl;
return(sent);
}
else
return(0);
}
else
return(0);
}
unsigned long ComPort::Receive(char *text, DWORD maxsize)
{
if(hCom!=INVALID_HANDLE_VALUE)
{
unsigned long received;
BOOL success = false;
success = ReadFile(hCom, text, maxsize, &received, NULL);
if (success==(BOOL)true){
text[received]='\0';
std::cout << "Receive OK" << " Daten: " << std::showbase << std::hex << text << std::dec << std::endl;
std::cout << std::endl;
return(received);
}
else{
std::cout << "Readfile ZERO" << " Daten: " << std::showbase << std::hex << text << std::dec << "gelesene Bytes" << received << std::endl;
return(0);
}
}
else {
std::cout << "Readfile INVALID_HANDLE_VALUE" << " Daten: " << text << std::endl;
return(0);
}
}
const char *ComPort::GetCP(int index)
{
static const char *CP[]={"COM1","COM2","COM3","COM4","COM5","COM6"};
if(index>-1&&index<ComPort::MAX_PORT)
return(CP[index]);
else
return("");
}
und hier die Aufrufe
// COM.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "cport_m_dcb.h"
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
ComPort comport;
int portnr = 1;
// Open Com1
if (comport.OpenCom(portnr) != 0)
std::cout << "Der Port COM" << portnr << "konnte nicht geoeffnet werden" << std::endl ;
else
std::cout << "Der Port COM" << portnr << " ist geoeffnet" << std::endl;
std::cout << std::endl;
//define DCB Parameters
DWORD BaudRate = CBR_38400;
BYTE ByteSize = 8;
BYTE Parity = 0; //PARITY_NONE;
BYTE StopBits = 0; //NOPARITY;
// set DCB Parameters
comport.SetUART(BaudRate, ByteSize, Parity, StopBits);
comport.GetUART(BaudRate, ByteSize, Parity, StopBits);
comport.SetReadTimeouts(10000,10,10000);
//comport.SetWriteTimeouts(100,100);
DWORD max_datasize = 30;
char data[] = {0};
DWORD dwBytesRead = 0;
dwBytesRead = comport.Receive(data, max_datasize);std::cout << "Auf Start-up Meldung gewartet" << std::endl;
if (dwBytesRead){
std::cout << "start-up Meldung empfangen," << " Daten: "<< data << " bytesread: " << dwBytesRead << std::endl;
}
else
std::cout << "keine start-up Meldung empfangen," << " Daten: " << data << " bytesread: " << dwBytesRead << std::endl;// Fehler
//Status Request - STX, NUL, SOH, NUL, 1, NAK, DC2
char data_status_req[7];
data_status_req[0] = 0x02;
data_status_req[1] = 0x00;
data_status_req[2] = 0x01;
data_status_req[3] = 0x00;
data_status_req[4] = 0x31;
data_status_req[5] = 0x15;
data_status_req[6] = 0x12;
std::cout << "Status Request Daten: " << std::showbase << std::hex << data_status_req << " Bytes: " << std::showbase << std::dec << (DWORD) strlen(data_status_req) << std::endl;
DWORD dwBytesWRITE = 0;
dwBytesWRITE = comport.Send(data_status_req); std::cout << "Status Request Daten gesendet - gesendete Bytes:" << std::showbase << std::dec << dwBytesWRITE<< std::endl;
dwBytesRead = 0;
data[0] = '\0';
dwBytesRead = comport.Receive(data, max_datasize+100);
if (dwBytesRead){
std::cout << "Status Meldung empfangen," << " Daten: " << std::showbase << std::hex << data << std::dec << " bytesread: " << dwBytesRead << std::endl; // Fehler
}
else
std::cout << "keine Status Meldung empfangen," << " Daten: " << std::showbase << std::hex << data << std::dec << " bytesread: " << dwBytesRead << std::endl; std::cout << std::endl;
/**/
// close COM
comport.CloseCom();
std::cout << "Port geschlossen" << std::endl;
return 0;
}
Vielen Dank Jower