Hallo,
ich möchte einzelne Bits an meinen COM-Port senden. Im Prinzip 0-0-0-0-0-0-0-1 mit jeweils 200ms Abstand.
Mein Ansatz wäre jetzt:
Code:
#include <iostream>
#include <conio.h>
#include <windows.h>
int main(int argc, char* argv[])
{
char INBUFFER[2]; // Create Input Buffer
bool Zero = 0x00;
bool One = 0x01;
/* Komplement berechnen
char Komplement = '\xd5' ^ '\xff';
printf("%d\r\n",Komplement);
*/
DWORD bytes_read = 1; // Number of bytes read from port
DWORD bytes_written = 1; // Number of bytes written to the port
HANDLE comport = NULL; // Handle COM port
int bStatus; // Status indicator
DCB comSettings; // Contains various port settings
COMMTIMEOUTS CommTimeouts; // Contains various COM timeouts
// Open COM port
if ((comport =
CreateFile("\\\\.\\COM4", // open com4:
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
printf("CreateFile() funktioniert nicht\r\n"); // error processing code goes here
system("pause");
exit(0);
}
CommTimeouts.ReadIntervalTimeout = -1; // Timeout in ms
CommTimeouts.ReadTotalTimeoutMultiplier = 0; // Timeout in ms
CommTimeouts.ReadTotalTimeoutConstant = 0; // Timeout in ms
CommTimeouts.WriteTotalTimeoutMultiplier = 0; // Timeout in ms
CommTimeouts.WriteTotalTimeoutConstant = 65000; // Timeout in ms
bStatus = SetCommTimeouts(comport,&CommTimeouts); // Set COM timeouts
if (bStatus != 0)
{
printf("SetCommTimeouts() funktioniert nicht\r\n"); // error processing code goes here
}
GetCommState(comport, &comSettings); // Getting COM state
comSettings.BaudRate = 9600; // COM setting
comSettings.StopBits = ONESTOPBIT; // COM setting
comSettings.ByteSize = 7; // COM setting
comSettings.Parity = ODDPARITY; // COM setting
comSettings.fParity = FALSE; // COM setting
bStatus = SetCommState(comport, &comSettings); // Setting COM state
if (bStatus == 0)
{
printf("SetCommState() funktioniert nicht.\r\n"); // error processing code goes here
}
Sleep(5);
bStatus = WriteFile(comport, &Zero,1,&bytes_written,NULL);
Sleep(200);
bStatus = WriteFile(comport, &Zero,1,&bytes_written,NULL);
Sleep(200);
bStatus = WriteFile(comport, &Zero,1,&bytes_written,NULL);
Sleep(200);
bStatus = WriteFile(comport, &Zero,1,&bytes_written,NULL);
Sleep(200);
bStatus = WriteFile(comport, &Zero,1,&bytes_written,NULL);
Sleep(200);
bStatus = WriteFile(comport, &Zero,1,&bytes_written,NULL);
Sleep(200);
bStatus = WriteFile(comport, &Zero,1,&bytes_written,NULL);
Sleep(200);
bStatus = WriteFile(comport, &One,1,&bytes_written,NULL);
printf("Sending: %d\r\n", 0x46);
bStatus = ReadFile(comport,&INBUFFER,2,&bytes_read,NULL);
printf("Receiving: %d <-> %d\r\n", INBUFFER[0], INBUFFER[1]);
system("pause");
CloseHandle(comport);
return 0;
}
... allerdings ist WriteFile() nur für Bytes ausgelegt, kann ich irgendwie auch einzelne Bits senden? Der Code scheint so jedenfalls nicht zu funktionieren.
Danke & Gruß
COM