Gast
#3788609
Hallo Freunde,
ich möchte Daten (Zeichenketten) von meine PC zu einem µC (MSP430g2553)
senden.
Da mein PC der eine RS232-Schnittstelle hat noch auf dem Weg zu mir ist
benutze ich momentan ein PC mit USB->RS232 Wandler.
Mit einem Programm (Hercules) funktioniert die Kommunikation
einwandfrei.
Allerdings möchte ich ein C-Programm verwenden, dass Daten zunächst
einmal zum µC schickt. Wenn das dann klappt möchte ich über ein
Echo-Programm Daten vom µC dann wieder Empfangen.
Einen Port(in meinem Fall COM15) kann ich "erstellen" und öffnen. Die
Datei befindet sich in dem Ordner des C-Programmes.
In diese Datei kann ich auch was reinschreiben.
Problem ist wohl nur das der Befehl SetCommState nicht klappt.
Zumindestens bekomm ich den Fehler "Unable to configure the serial port"
Dies wird wohl auch der Grund sein warum der µC keine Daten Empfängt, da
ich wohl die Bautrate und ByteSize nicht einstellen kann.
Hier mein Code:
/*******************************************
Kommunikation mit einer seriellen Schnittstelle
Win32-API
Datei öffnen - CreateFile(...)
Datei beschreiben - WriteFile(...)
Datei lesen - ReadFile(...)
*******************************************/
#include <stdint.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
int main (void)
{
HANDLE hComm;
DWORD CtsStatus;
DWORD dwRead;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};
unsigned char sendBuff[255 + 1] = {0};
int len;
//Datei öffnen - CreateFile(...)
//COM15 öffnen:
//nonoverlapped Betrieb.
hComm = CreateFile (TEXT("COM15"), GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING , 0, NULL);
if(hComm==INVALID_HANDLE_VALUE)//error opening port; abort
{
MessageBox (NULL, TEXT("Unable to OPEN the serial port"),
TEXT("Error"), MB_OK);
getchar();
return 0;
}
else{
printf("opening port\n");
}
DCB PortDCB;
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hComm, &PortDCB);
PortDCB.DCBlength=sizeof(PortDCB);
PortDCB.BaudRate=CBR_9600;
PortDCB.ByteSize=7;
PortDCB.StopBits=ONESTOPBIT;
PortDCB.Parity=ODDPARITY;
//Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hComm, &PortDCB))
{
DWORD dwError;
// Could not configure the serial port.
dwError = GetLastError ();
MessageBox (NULL, TEXT("Unable to configure the serial port"),
TEXT("Error"), MB_OK);
}
DWORD myBytesRead = 0;
strcpy ((char *) sendBuff, "abc");
len = strlen ((char *) sendBuff);
printf("Bits gesendet: %i \n",len);
printf("Uebertragene Daten: %s \n",sendBuff);
WriteFile(hComm, sendBuff, len, &myBytesRead, NULL);
//Schließen den Ports
CloseHandle (hComm);
getchar();
return 0;
}
Kann mir einer nen Tipp geben wieso die SetCommState nicht klappt?
Mein Betriebssystem ist übrigens Windows 8.
Danke im Vorraus!!
Christian