串行通讯错误.错误是给get的输出有错误或端口有错误 [英] Serial communication error.the error is it is giving the output of get has eror and port has erro

查看:174
本文介绍了串行通讯错误.错误是给get的输出有错误或端口有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <windows.h>

void main()
{
	DWORD dwBytesRead, dwBytesWritten, dwPos;
	char c[7];
	LPWSTR port_name=malloc(5);
	int cnt,i,len;
	DCB obj_dcb;
	int m_baud_rate=CBR_110,  m_byte_size=1;

	HANDLE fd= CreateFile(port_name,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
	
	
	if (fd == NULL)
		{ 
			printf("file doesnt exist");
		}
	else
	{
		if(GetCommState(fd,&obj_dcb)==0)
			{
				printf("get has problem");
			//	return FALSE;
		}
obj_dcb.BaudRate						= m_baud_rate;

	obj_dcb.DCBlength						= sizeof(obj_dcb);

	obj_dcb.Parity                          = 0;
	obj_dcb.ByteSize                        = m_byte_size;
	obj_dcb.StopBits                        = ONESTOPBIT ;
	obj_dcb.fBinary                         = TRUE;
	obj_dcb.fParity                         = FALSE;
	obj_dcb.fDsrSensitivity                 = TRUE;
	obj_dcb.fTXContinueOnXoff               = FALSE;
	obj_dcb.fOutX                           = FALSE;
	obj_dcb.fInX                            = FALSE;
	obj_dcb.fErrorChar                      = FALSE;
	obj_dcb.fNull                           = TRUE;
	obj_dcb.fRtsControl                     = RTS_CONTROL_ENABLE;
	obj_dcb.fAbortOnError                   = TRUE;
	if(SetCommState(fd,&obj_dcb)==0)
		{	printf("eror in port");
//			return FALSE;
		}
		
			for(i=0;i<=4 ;i++)
					{	
						c[i] = getchar();	
					}
						c[i] = '\0';

				dwBytesWritten=strlen(c) ;
						
				WriteFile (fd,c,dwBytesWritten,&dwBytesWritten, NULL);
			
		
	}
			c[0] = '\0';

	
				CloseHandle(fd);
		
				fd= CreateFile(TEXT("d:\\temp.txt"),GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
	
	if(fd == NULL)
		{	
			printf("do exist");
		}
	else
		{
				dwBytesRead=sizeof(char);
				ReadFile (fd, c, 1, &dwBytesRead, NULL);			
				printf("%s",c);
		}
	
}



[edit]已删除呼喊声-OriginalGriff [/edit]



[edit]SHOUTING removed - OriginalGriff[/edit]

推荐答案

此行包含多个错误:
This line contains multiple errors:
CreateFile(port_name,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);



  1. port_name未初始化
  2. dwCreationDisposition对于串行端口必须为OPEN_EXISTING
  3. dwFlagsAndAttributes对于串行端口必须为0

  1. port_name is not initialized
  2. dwCreationDisposition must be OPEN_EXISTING for serial ports
  3. dwFlagsAndAttributes should be 0 for serial ports


试试


Try

LPWSTR port_name = L"COM1";
CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

>
我没有检查其余代码是否存在其他错误.

[更新检查更多错误]

字节大小是一个位数:


I did not check the remaining code for other errors.

[UPDATE checking for more errors]

The byte size is a bit count:

// Wrong
//int m_byte_size = 1;
// Correct is 7 or 8
int m_byte_size = 8;


CreateFile在出现错误时返回INVALID_HANDLE_VALUE:


CreateFile returns INVALID_HANDLE_VALUE upon errors:

// Don't use this
//if (fd == NULL)
// But this
if (fd == INVALID_HANDLE_VALUE)


您未分配端口名称.
您只需执行
You did not assign a port name.
You just do
LPWSTR port_name=malloc(5);

,然后在某些行中尝试打开端口而不分配任何Com-Port.

对于COM1的首次测试,请尝试

and some lines later you try to open the port without assigning a Com-Port.

For the first test on COM1 just try

HANDLE fd = CreateFile( _T("\\\\.\\COM1"),  
                    GENERIC_READ | GENERIC_WRITE, 
                    0, 
                    0, 
                    OPEN_EXISTING,
                    0,
                    0);



看看 CreateFile函数 [ ^ ]

Win32中的串行通信 [



Take a look at CreateFile function[^]

and Serial Communications in Win32[^]

Hope it helps..


这篇关于串行通讯错误.错误是给get的输出有错误或端口有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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