Jump to content

Com port

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
RandomCode

RandomCode

    Newbie

  • Members
  • Pip
  • 5 posts
Hello. I wrote a small program to work with RS232 (COM) port. But it only works when I run the program HyperTerminal. Otherwise, it stops at the ReadFile (). Can someone help me?

#include <windows.h>

#include <stdio.h>


#include "windows.h"

#include <stdlib.h>

#include <stdio.h>


float MPP=1;

char PORT[5];


float watt()

{

	float res=0;

	DCB dcb;

	HANDLE hCom;

	BOOL fSuccess;

	char *pcCommPort = PORT;

	hCom = CreateFile(	pcCommPort,

						GENERIC_READ | GENERIC_WRITE,

						0,

						NULL,

						OPEN_EXISTING,

						0,

						NULL

						);


	if (hCom == INVALID_HANDLE_VALUE) 

	{

		printf ("CreateFile failed with error %d.\n", GetLastError());

		return (1);

	}

	fSuccess = GetCommState(hCom, &dcb);


	if (!fSuccess) 

	{

		printf ("GetCommState failed with error %d.\n", GetLastError());

		return (2);

	}

	dcb.DCBlength=sizeof(DCB);

	dcb.fBinary=TRUE;

	dcb.BaudRate = CBR_4800;

	dcb.fDtrControl=DTR_CONTROL_ENABLE;

	dcb.fOutX=FALSE;

	dcb.fInX = FALSE;

	dcb.ByteSize = 8;

	dcb.Parity = EVENPARITY;

	dcb.StopBits = ONESTOPBIT;


	fSuccess = SetCommState(hCom, &dcb);


	if (!fSuccess) 

	{

		printf ("SetCommState failed with error %d.\n", GetLastError());

		return (3);

	}

	char buf[6];

	DWORD read;

	DWORD write;

	buf[0] = '#';

	buf[1] = '0';

	buf[2] = '0';

	buf[3] = '8';

	buf[4] = '3';

	buf[5] = 0x0D;

	char out[11]="";

	WriteFile(hCom,buf,sizeof(buf),&write,NULL);

	printf("%d\n",write);

	ReadFile(hCom,out,10,&read,NULL);

	printf("%d\n",read);

	res=((int)out[5]-48)*0.1+((int)out[3]-48)+((int)out[2]-48)*10+((int)out[1]-48)*100;

	CloseHandle(hCom);

	return(res);

}


void main()

{

	FILE *fl=fopen ("temp.csv", "w");


	POINT startp;

	POINT cp;

	POINT lp;

	int f=0;

	int r=0;

	fclose(fl);

	FILE *conf=fopen("config.ini", "r");

	fscanf(conf,"%s",PORT);

	fscanf(conf,"%f",&MPP);


	GetCursorPos(&startp);

	SetCursorPos(0,200);

	

	GetCursorPos(&lp);


	while(1)

	{

		GetCursorPos(&cp);

		if ((cp.x==lp.x)&&(f==0)&&(cp.x!=0))

		{

			r+=cp.x;

			fl=fopen("temp.csv", "a");

			fprintf(fl,"%3.1f;%f\n",watt(),(float)(r)/MPP);

			fclose(fl);

			f=1;

			SetCursorPos(0,200);

		}

		if ((cp.x!=lp.x)&&(f==1))

		{

			f=0;

		}

		lp.x=cp.x;

		Sleep(100);

	}

	SetCursorPos(startp.x,startp.y);


}


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

RandomCode said:

Hello. I wrote a small program to work with RS232 (COM) port. But it only works when I run the program HyperTerminal. Otherwise, it stops at the ReadFile (). Can someone help me?
Well, a blocking function such as ReadFile will block (pause and wait for data). There is more information in the MSDN on doing non-blocking reads.

#3
RandomCode

RandomCode

    Newbie

  • Members
  • Pip
  • 5 posts
Yes, but it should read, because when I run this code after stopping and starting HyperTerminal, it works, and receives data.

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
I don't know what you're asking. You've got a program that forever does send/receive. If there is nothing on the other end sending for you to receive, I don't know what you expect other than the program to stop and wait.

#5
RandomCode

RandomCode

    Newbie

  • Members
  • Pip
  • 5 posts
This program works with an external sensor. When it receives data ( "# 0083 + CR"), it send me a value. But if I did not start HyperTerminal program is not working.
Sorry for bad English, it not my native language.

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
The comm port doesn't really like to share, if I remember correctly. Don't you have the choice of (A) your program, or (B) hyperterminal? A © both option shouldn't be available.

So you've got an external sensor that communicates via the serial port. I am assuming that it is physically connected to the port you specify. Communication between your program and the sensor then proceeds in the following fashion?
  • You ask it a question (your WriteFile).
  • It responds with an answer (your ReadFile).
Is that an accurate summary?

And your issue is that after you ask, the sensor does not respond?


[edit]And in a separate bit of testing, you don't run your program but instead run hyperterminal. You initiate communication in hyperterminal and the sensor responds as expected. But you only observe this correct behavior when communicating with the sensor via hyperterminal?

#7
RandomCode

RandomCode

    Newbie

  • Members
  • Pip
  • 5 posts
Yes, if I run only my program, it writes the data and sleeping.
When I first run HyperTerminal only connect and disconnect it, and then exit the HyperTerminal and run my program, it sends data and receives data.
I do not run them in parallel.

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
To me this sounds like you may not be initializing the comm port fully in your code, something that hyperterminal does. I too have stumbled on this and not found an answer for my quick-and-dirty interface apps. So I am not going to be a great source of help.

I've tried to follow the MSDN examples to correctly set things up, buy I have not come out with a satisfactory solution. I wish you luck in searching the MSDN, and perhaps someone else here may be able to recommend a solution.

#9
RandomCode

RandomCode

    Newbie

  • Members
  • Pip
  • 5 posts
So, my worst fears have come true. Thank you for your help, I go into the debugger.