statt normale COM1 benutze ich USB zu RS232: Problem mit Kommunikation
-
Hallo!
ich habe ein programm, das wunderbar läuft, wenn ich die COM1 benutze.
Nun hänge ich statt COM1 ein anderes Gerät (Genannt SC Board), wofür ich eine RS232 zu USB Schnittstelle habe.
An meinem Windows PC erscheint auch das gerät als COM18, das kann ich dann im Programm auswählen.Nun gibt es 2 Probleme: mein SC Board geht sofort ins Bootmode, wenn ich die USB Schnittstelle ans PC einstecke.
Deswegen wollte ich die RTS und DTR Leitungen beide auf 0 setzen, wenn ich das Programm starte und dabei die Serielle Kommunikation öffne.
Aber egal, wie ich diese beiden Leitungen (RTS und DTR) setze, ich bekomme immer error 4 beim Öffnen der Seriellen Schnittstelle.(GetCommState gibt 4 zurück)
(siehe Code unten)- Kann mir jemand weiter helfen? Ich kann nicht fesstellen, woran es happert, schon auf der PC Seite oder an de SC Board Seite.
- kann man mit einem Tool die USB Kommunikation beobachten? Bisher hatte ich eine Art "Horchkabel" für die TXD und RXD Leitungen und habe das mit einem Hyperterminal beobachtet, aber mit dem USB Stecker kann ich nichts mehr sehen)
Vielen Dank im Voraus für die Hilfe
frenchcancanAufruf:
err = s.OpenConnection(cp.ComPort, BD_19200,NOPARITY,NORESET, NOBOOTMODE);
Funktion:
int CSer::OpenConnection(int ComPort, int Baudrate, int Parity, int ResetMode, int Bootmode) { char szPort[15] ; DCB dcb; DWORD EvtMask; DWORD Errors; // pointer to variable to receive error codes COMSTAT Stat; // pointer to buffer for communications status if(ComPort<0 ) return SERERR_INVALIDCOMPORT; if(Baudrate<BD_1200 || Baudrate>BD_115200) return SERERR_INVALIDBAUDRATE; CloseConnection(); // Schliessen falls geoeffnet Sleep(10); wsprintf( szPort, "%s%d", "COM", ComPort ) ; hCom = CreateFile( szPort, // open COMM device GENERIC_READ | GENERIC_WRITE, 0, // exclusive access NULL, // no security attrs OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,// overlapped I/O //0, // no overlapped I/O NULL ); if (hCom == 0) return SERERR_OPENPORT; ClearCommError( hCom, &Errors, &Stat ) ; // zum Laenge lesen if( GetCommState(hCom, &dcb)==FALSE) return SERERR_GETCOMSTATE; switch(Baudrate) { case BD_1200 : dcb.BaudRate = 1200; break; case BD_9600 : dcb.BaudRate = 9600; break; case BD_19200: dcb.BaudRate = 19200; break; case BD_57600: dcb.BaudRate = 57600; break; case BD_115200: dcb.BaudRate = 115200; break; } switch(Parity) { case NOPARITY : dcb.Parity = NOPARITY; break; case EVENPARITY : dcb.Parity = EVENPARITY; break; } dcb.ByteSize = 8; // number of bits/byte, 4-8 dcb.StopBits = 0; // 0,1,2 = 1, 1.5, 2 dcb.fBinary = TRUE; // binary mode, no EOF check dcb.fParity = FALSE; // enable parity checking dcb.fOutxCtsFlow = FALSE; // CTS output flow control dcb.fOutxDsrFlow = FALSE; // DSR output flow control dcb.fDtrControl = ResetMode; //DTR_CONTROL_DISABLE; // DTR flow control type // = Reset dcb.fRtsControl = Bootmode; //RTS_CONTROL_DISABLE; // RTS flow control // = Bootmode dcb.fDsrSensitivity = FALSE ; // DSR sensitivity dcb.fTXContinueOnXoff = FALSE; // XOFF continues Tx dcb.fOutX = FALSE; // XON/XOFF out flow control dcb.fInX = FALSE; // XON/XOFF in flow control dcb.fErrorChar = TRUE; // enable error replacement dcb.fAbortOnError = TRUE ; // abort reads/writes on error dcb.XonChar = 0x01; // Tx and Rx XON character dcb.XoffChar = 0x02; // Tx and Rx XOFF character dcb.ErrorChar = 0x1F; // error replacement character dcb.EofChar = 0; // end of input character dcb.EvtChar = 0x0D; // received event character if (SetCommState( hCom, &dcb)== FALSE) return SERERR_SETCOMSTATE; EvtMask = EV_TXEMPTY; if(SetCommMask( hCom , EvtMask ) == FALSE) return SERERR_SETCOMMASK; osWrite.hEvent = CreateEvent( NULL, // no security TRUE, // explicit reset req FALSE, // initial event reset NULL ) ; // no name if (osWrite.hEvent == NULL) return SERERR_CREATEWRITEBUFFER; osRead.hEvent = CreateEvent( NULL, // no security TRUE, // explicit reset req FALSE, // initial event reset NULL ) ; // no name if (NULL == osRead.hEvent) return SERERR_CREATEREADBUFFER; GetCommState( hCom, &dcb); osWrite.Offset = 0; // workaround wegen Fehler in den Win API Funktionen osWrite.OffsetHigh = 0; // Writefile() und Readfile() osRead.Offset = 0; // [see VC Doku ArticleID: Q110148] osRead.OffsetHigh = 0; m_PortConnected = TRUE; return SER_INITIALISATION_OK; }
-
COM1.. COM9 sind Shortcuts der Serial Ports.
Die "richtigen" Namen sind \.\COMx.
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspxGoogle nach COM ports higher than 9
-
Serielle Kommunikation ist *nicht* trivial! Verwende bitte eine fertige Klasse, die schon alles kann und beachtet, was Du willst. Z.B.
http://www.codeproject.com/KB/system/serial.aspxDu übergibst z.B. den Namen des COM-Ports falsch dem CreateFile...
-
Das war tatsächlich der COM Port Name über 9, den ich falsch bildete
Mit"wsprintf(szPort,"\\\\.\\COM%d", ComPort);"
lief es sofort,
Vielen Dank!
frenchcancanPS:Habe mir das Codeproject auch noch heruntergelden, danke für den Tip