| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 725 人关注过本帖
标题:C语言 串口通信问题
只看楼主 加入收藏
andson870420
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-12-20
收藏
 问题点数:0 回复次数:2 
C语言 串口通信问题
打开两个串口,COM3和COM4,程序如下:



#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <commdlg.h>
#include <windef.h>

int nread,nwrite;


void main()
{

HANDLE hSerial;
COMMTIMEOUTS timeouts;
COMMCONFIG dcbSerialParams;
char *buffRead;
char *buffWrite;
//BYTE data;
DWORD dwBytesWritten, dwBytesRead;
//int k;


_retry:
hSerial = CreateFile("COM4",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED, // FILE_ATTRIBUTE_NORMAL
NULL);

if ( hSerial == INVALID_HANDLE_VALUE)  
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
printf(" serial port does not exist \n");
}
printf(" some other error occured. Inform user.\n");
}
else
  printf("port open successed!\n");

//DCB dcbSerialParams ;
//GetCommState( hSerial, &dcbSerialParams.dcb);
if (!GetCommState(hSerial, &dcbSerialParams.dcb))  
{
printf("error getting state \n");
}

dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);


dcbSerialParams.dcb.BaudRate = 9600;
dcbSerialParams.dcb.ByteSize = 8;
dcbSerialParams.dcb.StopBits = TWOSTOPBITS;
dcbSerialParams.dcb.Parity = NOPARITY;

dcbSerialParams.dcb.fBinary = TRUE;
dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcbSerialParams.dcb.fOutxCtsFlow = FALSE;
dcbSerialParams.dcb.fOutxDsrFlow = FALSE;
dcbSerialParams.dcb.fDsrSensitivity= FALSE;
dcbSerialParams.dcb.fAbortOnError = TRUE;

if (!SetCommState(hSerial, &dcbSerialParams.dcb))  
{
printf(" error setting serial port state \n");
}


GetCommTimeouts(hSerial,&timeouts);
//COMMTIMEOUTS timeouts = {0};

timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier= 10;

if(!SetCommTimeouts(hSerial, &timeouts))  
{
printf("error setting port state \n");
}




//****************Write Operation*********************//

buffWrite=(char *)malloc(256);
printf("please input your data:");
scanf("%s",buffWrite);  
nwrite=255;
//nwrite =1024;

//dwBytesWritten = 0;

if (!WriteFile(hSerial, buffWrite, nwrite, &dwBytesWritten, NULL))  
{  
printf("error writing to output buffer \n");
}

else
printf("written data:%s\n",buffWrite);
system("pause");


//***************Read Operation******************//
//buffRead=0;
//buffRead = (char *)malloc(256);
SetFilePointer(hSerial, 0, 0, FILE_BEGIN);  
nread = GetFileSize(hSerial,NULL);
buffRead = (char *)malloc(nread+1);
//memset(buffRead, 0, 1024);


if (!ReadFile(hSerial,buffRead,nread, &dwBytesRead, NULL))  
{
printf("error reading from input buffer \n");

}

else

printf("Data read from read buffer is:%s\n\n",buffRead);
CloseHandle(hSerial);
Sleep(2000);
goto _retry;

}

到Writefile为止都可以运行,但是在Readfile这一块"Data read from read buffer is:"后面不能读取数据,显示的都是乱码,该如何解决呢?
搜索更多相关主题的帖子: C语言 include 通信 
2011-12-20 10:27
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
buffRead读的是什么,你有下过断点调试么,读的是字符流? 二进制数据?
返回值 对应的错误码 调用是否成功 你啥都没搞啊!

[ 本帖最后由 hahayezhe 于 2011-12-20 15:26 编辑 ]
2011-12-20 15:24
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
  没分
2011-12-20 15:25
快速回复:C语言 串口通信问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016057 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved