2 Replies - 1219 Views - Last Post: 15 January 2009 - 10:43 AM Rate Topic: -----

#1 damennix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-January 09

serial port control

Posted 14 January 2009 - 10:28 AM

I am new to C++ have some experience with VB, Assembly, and ladder logic.
Iím trying to write to a microprocessor through the serial port using DevC++. When I use the code below I get a compiler error stating dcb is not recognized "first use of this function"?. Am I missing a header or will this not work with devC++.
#include <iostream>
#include <stdlib.h>
#include <windows.h>


int main(int argc, char *argv[]){
// Open the serial port.
  hPort = CreateFile (lpszPortName,com1 // Pointer to the name of the port
					  GENERIC_READ | GENERIC_WRITE,
									// Access (read-write) mode
					  0,			// Share mode
					  NULL,		 // Pointer to the security attribute
					  OPEN_EXISTING,// How to open the serial port
					  0,			// Port attributes
					  NULL);		// Handle to port with attribute
									// to copy
  system("PAUSE");	
  return 0;
  
  DCB dcbSerialParams = {0};
	dcbSerial.DCBlength=sizeof(dcbSerialParams);
	if (!GetCommState(hSerial, &dcbSerialParams)) {
	//error getting state
	}
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
	if(!SetCommState(hSerial, &dcbSerialParams)){
	//error setting serial port state
}
}




Is This A Good Question/Topic? 0
  • +

Replies To: serial port control

#2 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: serial port control

Posted 14 January 2009 - 12:33 PM

The DCB structure, according to MSDN, is included with winbase.h which is included by windows.h, so you should be good there. That page also indicates the minimum OS is win2k, so this won't work on 98, 95, me, etc.. You didn't say what Windows version you are developing on.

I would suspect this line in your code:

DCB dcbSerialParams = {0};

I think you are trying to initialize all the values in the structure zero? I don't think what you did will work, at least I would not do it that way. Try this instead:

#include <string.h>   // memset


DCB dcbSerialParams;
memset(&dcbSerialParams, 0, sizeof(dcbSerialParams));



Matthew
Was This Post Helpful? 0
  • +
  • -

#3 damennix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-January 09

Re: serial port control

Posted 15 January 2009 - 10:43 AM

Well thanks Matthew. This fixed my problem, but now I have about 13 more. I need to go back and try to get a better understanding of CreateFile, Handels, DCB , and the hole 9-yards. Im trying to programe a microprocessror. I have only used trainer terminals but i like assembly langage much better
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1