Click here to Skip to main content
15,921,660 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralWhere have the wizards gone, ActiveX Controls Pin
greekgoddj21-Dec-04 3:54
greekgoddj21-Dec-04 3:54 
GeneralRe: Where have the wizards gone, ActiveX Controls Pin
Antti Keskinen21-Dec-04 5:07
Antti Keskinen21-Dec-04 5:07 
GeneralRe: Where have the wizards gone, ActiveX Controls Pin
greekgoddj22-Dec-04 4:42
greekgoddj22-Dec-04 4:42 
GeneralRe: Where have the wizards gone, ActiveX Controls Pin
greekgoddj22-Dec-04 5:03
greekgoddj22-Dec-04 5:03 
GeneralRe: Where have the wizards gone, ActiveX Controls Pin
Antti Keskinen22-Dec-04 5:46
Antti Keskinen22-Dec-04 5:46 
GeneralVisual C++ read autocad(dxf) Pin
yangxjn21-Dec-04 3:49
yangxjn21-Dec-04 3:49 
GeneralRe: Visual C++ read autocad(dxf) Pin
David Crow21-Dec-04 4:44
David Crow21-Dec-04 4:44 
Questionserial port buffer full? Pin
Nik0n20-Dec-04 23:56
Nik0n20-Dec-04 23:56 
Hi,

I am currently trying to communicate via the COM port with a device of mine. For now, the device just echoes immediatly the character it receives.

PC Platform: WinXP Pro, MS VStudio 6.0, VC++

I read some articles from the site (really helpful, like to thank konchat, Ashish Dar - good articles for head start in Win API serial port programming) and tried to send some chars through the serial port and subsequently reading the char return by the device. (note: already tried the device with hyperterminal and it echoes well, no hang-ups or smth).

I used 8n1, non-overlapped mode for the serial port.

at first it seemed the program works, when trying to send (e.g. characters 65 to 90 ASCII) by the 24 char (Y) the program doesn't receive characters any more (they still get to the device as it shows me which chars arrived by flashing LED-s connected to a register).

Even stranger, if a run the program.. stop it, say it.. after the 10 sent character and run it again, exactly at the 14 char it will stop receiving data. (10+14= ??!!! exactly, 24).

Is there something i am doing wrong?. Here the code that does the sending:

<br />
<br />
DCB dcb = {0};<br />
dcb.DCBlength = sizeof(DCB);<br />
DWORD dwBaudRate=9600;<br />
BYTE  byStopBits=1,<br />
	  byByteSize=8, <br />
	  byParity=NOPARITY;<br />
COMMTIMEOUTS cto;<br />
//Open COM Port<br />
const HANDLE hPort = CreateFile( portname,<br />
			GENERIC_READ | GENERIC_WRITE, <br />
			0, <br />
			0, <br />
			OPEN_EXISTING, <br />
			0,//FILE_FLAG_OVERLAPPED, <br />
			0);<br />
<br />
if (hPort == INVALID_HANDLE_VALUE)<br />
	AfxMessageBox("Could not open "+portname+" port.");<br />
else<br />
{<br />
      cto.ReadIntervalTimeout = 1000;<br />
	cto.ReadTotalTimeoutConstant = 1000;<br />
	cto.ReadTotalTimeoutMultiplier = 1000;<br />
	cto.WriteTotalTimeoutConstant = 1000;<br />
	cto.WriteTotalTimeoutMultiplier = 1000;<br />
	SetCommTimeouts(hPort, &cto);<br />
	int index=1;<br />
	for (int k=0; k < 10; k++)<br />
		for (int i=65; i< 91; i++)<br />
		{<br />
			char outb[1],inb[1];<br />
			DWORD sent,read;<br />
			outb[0]=(i);<br />
			if (WriteFile(hPort,outb,1,&sent,NULL)==0)<br />
			{<br />
				AfxMessageBox("Error Writing to "+portname+" port.");<br />
				nRetCode=1;<br />
			}<br />
			std::cout << index << ":  sent=" << char(outb[0]) << "  ";<br />
			index++;<br />
                  inb[0]=0;<br />
			if (ReadFile(hPort, inb, 1, &read, NULL)==0)<br />
			{<br />
				AfxMessageBox("Error reading from "+portname+" port.");<br />
				nRetCode=1;<br />
			}<br />
			std::cout << "rcvd=" << char(inb[0]);<br />
			if (outb[0]==inb[0]) <br />
				std::cout << "  ... OK";<br />
			else<br />
				std::cout << "  ... NOK";<br />
			std::cout << std::endl << std::endl;<br />
			PurgeComm(hPort, PURGE_TXABORT | PURGE_RXABORT | <br />
									 PURGE_TXCLEAR | PURGE_RXCLEAR);<br />
			//Sleep(250);<br />
		}//for	<br />
	<br />
}//else<br />
if(CloseHandle(hPort) == 0)<br />
{<br />
	AfxMessageBox("Error closing "+portname+" port.");<br />
	nRetCode=1;<br />
}<br />
std::cout << "Port " << portname << " closed." << std::endl << std::endl;<br />

Any ideas, anyone? Smile | :)
Looking forward to your replies.


Found the bug. If anyone steps across this one in future: when using Win API for Serial Port access, always set the DCB Structure or else the code will work only partially (only God and Bill Gates know why but... who am I to judge :P ). I used a system call "system('mode COM1..... ');" to set the com parameters and the API doesn't seem to cope very well with that.

Anyways... Happy coding
Nik0n
AnswerRe: serial port buffer full? Pin
Trollslayer21-Dec-04 2:06
mentorTrollslayer21-Dec-04 2:06 
GeneralRe: serial port buffer full? Pin
Nik0n21-Dec-04 2:25
Nik0n21-Dec-04 2:25 
QuestionTransparent Label ? Pin
pc_dev20-Dec-04 23:13
pc_dev20-Dec-04 23:13 
GeneralUpload picture Pin
henryzal20-Dec-04 22:25
susshenryzal20-Dec-04 22:25 
GeneralRe: webservice Pin
Antti Keskinen20-Dec-04 22:17
Antti Keskinen20-Dec-04 22:17 
GeneralDynamic Crystal report viewer control Pin
Rahim Rattani20-Dec-04 20:40
Rahim Rattani20-Dec-04 20:40 
GeneralRe: Dynamic Crystal report viewer control Pin
naren-vc21-Dec-04 21:57
naren-vc21-Dec-04 21:57 
Generalset cprintdialog.m_pd.hdevmode Pin
nancysangeetha20-Dec-04 20:20
nancysangeetha20-Dec-04 20:20 
GeneralRe: set cprintdialog.m_pd.hdevmode Pin
David Crow21-Dec-04 2:17
David Crow21-Dec-04 2:17 
GeneralRe: set cprintdialog.m_pd.hdevmode Pin
nancysangeetha21-Dec-04 17:54
nancysangeetha21-Dec-04 17:54 
GeneralCalling a web service form MFC... Pin
anderslundsgard20-Dec-04 20:06
anderslundsgard20-Dec-04 20:06 
GeneralRe: Calling a web service form MFC... Pin
Antti Keskinen20-Dec-04 22:19
Antti Keskinen20-Dec-04 22:19 
GeneralRe: Calling a web service form MFC... Pin
anderslundsgard20-Dec-04 23:42
anderslundsgard20-Dec-04 23:42 
GeneralRe: Calling a web service form MFC... Pin
Antti Keskinen21-Dec-04 4:45
Antti Keskinen21-Dec-04 4:45 
GeneralConvert CString to Integer Pin
Anonymous20-Dec-04 19:46
Anonymous20-Dec-04 19:46 
GeneralRe: Convert CString to Integer Pin
SivaramanDhamodharan20-Dec-04 19:58
SivaramanDhamodharan20-Dec-04 19:58 
GeneralRe: Convert CString to Integer Pin
Jon Hulatt20-Dec-04 21:56
Jon Hulatt20-Dec-04 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.