?
Aufruf mit: progname <COM-Port-Nummer>
// DOS32-Programm zum Senden/Empfangen von Bytes über COM (9600-8N1)
// Es wird Bytes als Start gesendet.
// Alle empfangenen Bytes werden zurückgesendet.
// OS: W95, W98, W98SE, WinME, WinNT, Win2000, WinXP
// Note: Fast keine Fehlerbehandlung implementiert!
#include <windows.h>
#include <stdio.h>
#define COM_BUFFER_SIZE 256 // Read- und Write-Buffer-Size
#define BAUDRATE CBR_9600 // 9600 Baud
#define BYTESIZE 8
#define PARITY EVENPARITY
#define STOPBITS ONESTOPBIT
#define HELP_STRING TEXT("Aufruf mit: progname <COM-Port-Nummer>\r\n")
char COMMAND1[] = {(0x02), (0x49), (0x05), (0x12), (0x03)};
// Hauptprogramm: Aufruf mit: progname <COM-Port-Nummer>
int main (int argc, char **argv)
{
DCB dcb;
DWORD iBytesWritten;
BOOL bRet = true;
DWORD dwRead = 0;
DWORD dwSetMask = EV_RXCHAR | EV_ERR;
DWORD dwEvtMask;
OVERLAPPED o;
COMMTIMEOUTS ct;
unsigned char InString[COM_BUFFER_SIZE + 1];
TCHAR szCOM[6];
if (argc == 2 && // progname + COM-Port-Nummer ?
atoi (argv[1]) > 0 && atoi (argv[1]) < 5) // COM1 ... COM4?
wsprintf (szCOM, TEXT("COM%s"), argv[1]); // String "basteln" ...
else
{
printf (TEXT("\r\nERROR:\t %s"), HELP_STRING);
return (1); // und tschüß ...
}
memset (&o, 0, sizeof (OVERLAPPED)); // Struktur mit 0en füllen
o.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); // einen Event setzten
HANDLE hCom = CreateFile (szCOM, GENERIC_WRITE | GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (hCom == INVALID_HANDLE_VALUE)
{ // Fehlerausgabe:
LPVOID lpMsgBuf;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox (NULL, (LPCTSTR)lpMsgBuf, "Error: CreateFile", MB_OK | MB_ICONINFORMATION);
LocalFree (lpMsgBuf);
return (1); // und tschüß ...
}
dcb.DCBlength = sizeof(DCB); // Laenge des Blockes MUSS gesetzt sein!
GetCommState (hCom, &dcb); // COM-Einstellungen holen und aendern
dcb.BaudRate = BAUDRATE; // Baudrate
dcb.ByteSize = BYTESIZE; // Datenbits
dcb.Parity = PARITY; // Parität
dcb.StopBits = STOPBITS; // Stopbits
SetCommState (hCom, &dcb); // COM-Einstellungen speichern
GetCommTimeouts (hCom, &ct);
// Warte-Zeit [ms] vom Beginn eines Bytes bis zum Beginn des nächsten Bytes
ct.ReadIntervalTimeout = 1000 / BAUDRATE * (dcb.ByteSize +
(dcb.Parity == NOPARITY ? 0 : 1) +
(dcb.StopBits == ONESTOPBIT ? 1 : 2)) * 2;
ct.ReadTotalTimeoutMultiplier = 0; // [ms] wird mit Read-Buffer-Size multipliziert
ct.ReadTotalTimeoutConstant = 50; // wird an ReadTotalTimeoutMultiplier angehängt
ct.WriteTotalTimeoutMultiplier = 0;
ct.WriteTotalTimeoutConstant = 0;
SetCommTimeouts (hCom, &ct);
// Zwischenspeicher des serial-Drivers einstellen (für read und write):
SetupComm (hCom, COM_BUFFER_SIZE, COM_BUFFER_SIZE);
SetCommMask (hCom, dwSetMask); // Empfangssignale definieren
// Byte(s) senden:
if (!WriteFile (hCom, &COMMAND1, 5, &iBytesWritten, NULL)) // Senden der Bytes
{ // Fehlerausgabe:
LPVOID lpMsgBuf;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox (NULL, (LPCTSTR)lpMsgBuf, "Error: WriteFile", MB_OK | MB_ICONINFORMATION);
LocalFree (lpMsgBuf);
}
else
printf ("%d Byte(s) sent.", iBytesWritten);
do // in Endlos-Schleife auf Empfangssignale warten:
{
WaitCommEvent (hCom, &dwEvtMask, &o); // Event mit Empfangssignalen verknüpfen
if (WAIT_OBJECT_0 == WaitForSingleObject (o.hEvent, INFINITE)) // warten bis Event
{
if (dwEvtMask & EV_RXCHAR) // Zeichen an RxD empfangen:
{
bRet = ReadFile (hCom, &InString, sizeof (InString), &dwRead, NULL);
if (!bRet)
{ // Fehlerausgabe:
LPVOID lpMsgBuf;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox (NULL, (LPCTSTR)lpMsgBuf, "Error: ReadFile",
MB_OK | MB_ICONINFORMATION);
LocalFree (lpMsgBuf);
}
else
{ // Ausgabe (oder Verarbeitung) der empfangenen Bytes:
InString[dwRead] = '\0'; // in "zero-ended"-String verwandeln
printf (TEXT("\r\n\tRxD (%d Byte(s)): %s"), dwRead, InString);
WriteFile (hCom, &InString, dwRead, &iBytesWritten, NULL); // Senden der Bytes
}
}
if (dwEvtMask & EV_ERR)
{
MessageBox (NULL, "Error empfangen", "Error: ReadFile", MB_OK);
break; // Schleifen-Abbruch
}
}
}
while (1);
CloseHandle (hCom); // COM schließen
CloseHandle (o.hEvent); // Event-Handle zurückgeben
return (0);
}
Den Daten-Empfang solltest Du in einen eigenen Thread packen. Öffen und Schließen des Ports irgendwo zentral. Schreiben, da wo Du willst.
Die Empfangs-Daten der IO88 sind keine lesbaren ASCII-Zeichen - also vor der Ausgabe noch umwandeln.
Blackbird