rs485通讯测试

上传人:小** 文档编号:55575297 上传时间:2018-10-02 格式:DOC 页数:6 大小:62.50KB
返回 下载 相关 举报
rs485通讯测试_第1页
第1页 / 共6页
rs485通讯测试_第2页
第2页 / 共6页
rs485通讯测试_第3页
第3页 / 共6页
rs485通讯测试_第4页
第4页 / 共6页
rs485通讯测试_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《rs485通讯测试》由会员分享,可在线阅读,更多相关《rs485通讯测试(6页珍藏版)》请在金锄头文库上搜索。

1、WindowsXP/2000 下 RS485 通讯测试程序 此测试程序为 WindowsXP/2000 下的测试工具。 操作说明: 1. 把 COM1,COM2 的设置成 RS485, 。 2. 用短接线把 的 COM1,COM2 连接好,即 COM1 的 5/7 与 COM2 的 5/7 直接连接。 3. 进 入系统, 运行 RS485Test.exe。 4. 选择要测试的 COM 口, 工作模式,CHANGE COM MODE:SEND OR READ(S OR R)在这里选择是要发送还是接收。回车 5. 如果 COM1,COM2 出现相应的字符既表示这 2 个串口工作正常。 #inclu

2、de #include #include #include “Serial.h“ /RS485 测试程序 int RS485Test() unsigned int unComNum1 = 0; / unsigned int unComNum2 = 0; unsigned char ucCom1M od = 0; unsigned char ucCom2Mod = 0; char chCom5 = 0; CSerial serial1; CSerial serial2;char buffer1024 = 0; long lSendCounter = 0; char szSendBuff1024

3、= 0; DWORD dw Read = 0; int i = 0; /=输入 COM1 的端口号和工作模式 = / fprintf(stdout, “nEnter COM1 Number (1,2) : “); / scanf(“%d“, unComNum1= 1;fprintf(stdout, “Change COM1 mode: Send or Read(s or r) : “); scanf(“%s“, memset(chCom, 0, 5); sprintf(chCom, “COM%d“, unComNum1); BOOL bSer = serial1.InitCOM(chCom,

4、19200, 8, 0, 0, 1); if (!bSer) fprintf(stdout, “Cant Ini tCOM Com1.n“); return -1; /=输入 COM2 的端口号和工作模式 = / fprintf(stdout, “nEnter COM2 Number (1, 2) : “); / scanf(“%d“, unComNu m2 = 2; fprintf(stdout, “Change COM2 mode: Send or Read(s or r) : “); scanf(“%s“, memset(chCom, 0, 5); sprintf(chCom, “COM

5、%d“, unComNum2); bSer = serial2.InitCOM(chCom, 19200, 8, 0, 0, 1); if (!bSer) fprintf(stdout, “Cant InitCOM Com2.n“); return -1; fprintf(stdout, “Input Send data : “); scanf(“%s“, szSendBuff); for(int k = 0; k #include #incl ude #include #include “Serial.h“ / / Construction/Destruction / CSerial:CSe

6、rial() memset( memset( m_hIDComDev = NULL; fWaitingOnRead = FALSE; CSerial:CSerial() Close(); /* RS-485 通讯说明 由于 RS-485 是半双工通信,故同一时刻只能发送或接收。RS-485 的发送与接收由串行 口的 RTS 信号控制。一般在打开 485 端口时,需要将 RTS 设置为低,以便该端口处于接 收 状态,当该端口需要发送时需要将 RTS 置为高。 */ /*/ /*Function : 初始化 COM 端口 /*Input : strPort : 端口名称,比如“COM1“ /* n

7、Baud : 波特率, 比如9600 /* nByteSize : 数据位, 比如 8 /* nParity : 奇偶校验位, NOPARITY 表示 No parity, EVENPARITY 表示 Even; MARKPARITY 表示 Mark;ODDPARITY 表示 Odd; SPACEPARITY 表示 Space /* nStopBits : 停止位, ONESTOPBIT 表示 1 stop bit; ONE5STOPBITS 表示1.5 stop bits; TWOSTOPBITS 表示 2 stop bits /* iFlowControl : 暂时不用, /* 上面参数更

8、详细的说明,可参考 MSDN。 /*Output : 无 /*RetValue : 成功返回 TRUE, 失败返回 FALSE /*Author : xxx at 2010-11-17 16:11:41 /*/ BOOL CSerial:InitCOM(const char *strPort, int nBaud, BYTE nByteSize, BYTE nParity, BYTE nStopBits, int iFlowControl) DCB dcb = 0; /如果已经打开串口,则直接返回if (!(m_hIDComDev=INVALID_HANDLE_VALUE) | (m_hIDC

9、omDev = NULL) retu rn TRUE /打开串口m_hIDComDev = CreateFile( strPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPE N_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if (m_ hIDComDev = INVALID_HANDLE_VALUE) return FALSE; memset( memset( m_OverlappedRead.hEvent = CreateEvent(NULL, TRUE, FALS

10、E, NULL); m_OverlappedWrit e.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); SetupComm(m_hIDComDev, MAX_READ_BUFFER, MAX_WRITE_BUFFER); /设置输入 输出缓冲区 /设置通讯参数,即设置波特率,数据位,停止位,奇偶校验位dcb.DCBlength = sizeof(DCB); GetCommState(m_hIDComDev, dcb.BaudRate = nBaud; if (nByteSize 8) nByteSize = 8; dcb.ByteSize = n

11、ByteSize; if (nStopBits 2) nStopBits = 0; dcb.StopBits = nStopBits; if( nParity 4) nParity = 0; dcb.Parity = nParity / 对 dcb 配置进行修改 dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fRtsControl = RTS_CONTROL_DISA BLE; dcb.fParity = TRUE; dcb.XonLim = 100; dcb.XoffLim = 100;if (!SetCommState( m_hIDComDev, f

12、printf(stdout, “nerror at co nnect.n“); printf(“dwError = %d n“, dwError); return FALSE; /设置超时COMMTIMEOUTS CommTimeOuts; CommTimeOuts.ReadIntervalTimeout = 1; CommTime Outs.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 0; Comm TimeOuts.WriteTotalTimeoutMultiplier = 0; CommT

13、imeOuts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(m_hIDComDev, /* 来自 MSDN BOOL EscapeCommFunction( HANDLE hFile, / handle to communications device D WORD dwFunc / extended function to perform ); Parameters hFile Handle to the comm unications device. The CreateFile function returns this handle. dwFunc Specifies the code of t he extended function to perform. This parameter can be one of the following values: Value Meani ng C

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号