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
HANDLE hPort=0;
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;
dcb.StopBits = ONESTOPBIT;
dcb.EvtChar = 0x0A;
if (!SetCommState(hPort,&dcb)){
return FALSE;
};
return TRUE;
};
int main(int argc, char* argv[])
{
char line[100];
char command[9];
char readChar='e';
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;
}
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();
}
printf("Hello!\n");
CloseHandle( hPort );
return 0;
}