Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi every body.

I try to communicate with a Temperature device with RS232.
After all i wrote a program. this program send and receive commands.
I use Hyper terminal to observe the behavior of program.
It works but it has an error. For example i send " i love you" and in hyper terminal i recieve this answer:"àøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàø
àøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàø
àøàøàøàø"
Furthermore i must send a "job number" to this temperature device.
=========================================================================
example: i must write : <2> <1> <8> <14> <5> <16> <3>
STX address status check job DLE ETX
0*08
=========================================================================
can you help me please to send some job number like this?:confused:

Here is my program:
=========================================================================
#include "stdafx.h"

#include <windows.h>
#include <stdlib.h>
#define ASCII_XON	0x11
#define ASCII_XOFF	0x13


// Global Port Handle COMX:
HANDLE hPort=0;

/***************** InitPort ****************************************/
bool InitPort(TCHAR szPort[6]){
	
	COMMTIMEOUTS	cto = {0,0,0,0,0};
	DCB dcb;

	hPort = CreateFile((LPCTSTR) szPort, GENERIC_READ|GENERIC_WRITE,0,NULL,
						OPEN_EXISTING,FILE_FLAG_RANDOM_ACCESS, NULL);

	if(hPort==INVALID_HANDLE_VALUE) return FALSE;

	if (!SetCommMask(hPort, EV_RXFLAG) ||
			!SetupComm(hPort, 4096, 4096)  ||
			!SetCommTimeouts(hPort, &cto)) return FALSE;

	dcb.DCBlength = sizeof(DCB);
	if (!GetCommState(hPort,&dcb)) return FALSE;

	dcb.DCBlength = sizeof(DCB);
	dcb.XonChar   = ASCII_XON;
	dcb.XoffChar  = ASCII_XOFF;
	dcb.XonLim    = 100;
	dcb.XoffLim   = 100;
	dcb.fBinary   = TRUE;
	dcb.fParity   = TRUE;
	dcb.BaudRate  = 9600; 
	dcb.ByteSize  = 8;  
	dcb.Parity    = NOPARITY; // NOPARITY; EVENPARITY; ODDPARITY; MARKPARITY; SPACEPARITY; 
	dcb.StopBits  = ONESTOPBIT; // ONESTOPBIT; TWOSTOPBITS;
	dcb.EvtChar   = 0x0A;		//   LF = 0x0A  CR =0x0D		

	if (!SetCommState(hPort,&dcb)){
		// printf("Error SetCommState \r\n");
		return FALSE;
	};

	// printf(" SetCommState ok \r\n");
	return  TRUE;
};
/********** End of InitPort ****************************************/



int main(int argc, char* argv[])
{

	char line[100];
	char command[9];
	char readChar='e';

	//char ChrCheck;
	DWORD dwLen = 0;
	DWORD dwBytes = 0;
	DWORD dwErr = 0;
	COMSTAT	comstat;	
  	DWORD dwWritten=0;
	int iCnt=0;


	if (!InitPort("COM1:")) {
		printf("Error Init COM-Port");
		return 1;
	}


	//for (iCnt=0; iCnt<10; iCnt++) {
	//	command[iCnt]=iCnt+40;
	//}
	command[0]=50;
	command[1]=51;
	command[2]=52;
	command[3]=53;
	command[4]=54;
	command[5]=55;
	command[6]=56;
	command[7]=0x0D;
	command[8]=0x0A;
	
	WriteFile(hPort, "Hello Terminal", strlen("Hallo Terminal"), &dwWritten, NULL);
	WriteFile(hPort, command, strlen(command), &dwWritten, NULL);
	printf("Stringlaenge: %d\n%d Bytes an COM1 geschrieben ...\n", strlen(command), dwWritten);

	readChar = getchar();

	while (readChar!='x') {

		if (ClearCommError(hPort,&dwErr,&comstat)) 
			printf("Buffer COM1 gelesen ...\n");
		else
			return 1;

		dwBytes = min(100, comstat.cbInQue);

		printf("Bytes in Buffer COM1 %d\n", dwBytes);

		if (dwBytes) {
			dwErr = ReadFile(hPort, line, dwBytes, &dwLen, NULL);
			printf("Buffer: %s\nAnzahl Zeichen in Buffer:%d\nAnzahl Zeichen gelesen:%d\n", line, dwBytes, dwLen);
		}

		readChar = getchar();


	} // end of loop: while (~dwBytes) 
	

	printf("Hello!\n");

	CloseHandle( hPort );	
	
	return 0;
}
Posted
Updated 21-Feb-10 22:24pm
v2

shrazavi wrote:
i send " i love you" and in hyper terminal i recieve this answer:"àøàøàøàøàøàøàøàøàøàøàøàøàøà ...


If this is the response using Hyperterm then you should expect to get the same data using your program. What response do you expect when you tell the device that you love it?
 
Share this answer
 
Hi richard.

Instead "i love you" i must send a series of job number in form of Protocol. You can see an example of this protocol in my first Massege
 
Share this answer
 
I suggest you to use the powerful debug tool.
1.you use the original APP to contact to Temperature device,capture the data;
2.write your own program to implement this dataflow;
very easy!
then you can debug your RS232 program by youself.

free trial download
http://www.armecos.com/en/comtrace.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900