使用RS232与设备进行串行通信 [英] Serial communication with a device using RS232

查看:99
本文介绍了使用RS232与设备进行串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我尝试通过RS232与温度设备进行通信.
毕竟我写了一个程序.该程序发送和接收命令.
我使用超级终端观察程序的行为.
它可以工作,但是有错误.例如,我发送我爱你",并且在超级终端中,我收到以下答复:àøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøà àøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøàøààààààà àøàøàøàø?"
此外,我必须向该温度设备发送作业编号".
================================================== ======================
示例:我必须写:< 2> < 1> < 8> < 14> < 5> < 16> < 3>
STX地址状态检查作业DLE ETX
0 * 08
================================================== ======================
您能帮我发送一些这样的工作号码吗?:confused:

这是我的程序:
================================================== ======================

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;
}

推荐答案

shrazavi写道:
shrazavi wrote:

我发送我爱你"在超级终端中,我收到以下答案:àøàøàøøàøàøàøàøàøàøàøàøàøàøàøà...

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



如果这是使用Hyperterm的响应,那么您应该期望使用程序获得相同的数据.当您告诉设备您喜欢它时,您会期望什么响应?



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?


理查德.

取而代之的是我爱你",我必须以协议"的形式发送一系列工作编号.您可以在我的第一个Massege中看到该协议的示例
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


我建​​议您使用功能强大的调试工具.
1.您使用原始的APP与温度设备联系,捕获数据;
2.编写自己的程序以实现此数据流;
很简单!
那么您可以自己调试RS232程序.

免费试用版下载
http://www.armecos.com/en/comtrace.html [
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[^]


这篇关于使用RS232与设备进行串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆