C语言串口通信-源代码
51单片机的串口通信程序(C语言)

51单片机的串口通信程序(C语言) 51单片机的串口通信程序(C语言)在嵌入式系统中,串口通信是一种常见的数据传输方式,也是单片机与外部设备进行通信的重要手段之一。
本文将介绍使用C语言编写51单片机的串口通信程序。
1. 硬件准备在开始编写串口通信程序之前,需要准备好相应的硬件设备。
首先,我们需要一块51单片机开发板,内置了串口通信功能。
另外,我们还需要连接一个与单片机通信的外部设备,例如计算机或其他单片机。
2. 引入头文件在C语言中,我们需要引入相应的头文件来使用串口通信相关的函数。
在51单片机中,我们需要引入reg51.h头文件,以便使用单片机的寄存器操作相关函数。
同时,我们还需要引入头文件来定义串口通信的相关寄存器。
3. 配置串口参数在使用串口通信之前,我们需要配置串口的参数,例如波特率、数据位、停止位等。
这些参数的配置需要根据实际需要进行调整。
在51单片机中,我们可以通过写入相应的寄存器来配置串口参数。
4. 初始化串口在配置完串口参数之后,我们需要初始化串口,以便开始进行数据的发送和接收。
初始化串口的过程包括打开串口、设置中断等。
5. 数据发送在串口通信中,数据的发送通常分为两种方式:阻塞发送和非阻塞发送。
阻塞发送是指程序在发送完数据之后才会继续执行下面的代码,而非阻塞发送是指程序在发送数据的同时可以继续执行其他代码。
6. 数据接收数据的接收与数据的发送类似,同样有阻塞接收和非阻塞接收两种方式。
在接收数据时,需要不断地检测是否有数据到达,并及时进行处理。
7. 中断处理在串口通信中,中断是一种常见的处理方式。
通过使用中断,可以及时地响应串口数据的到达或者发送完成等事件,提高程序的处理效率。
8. 串口通信实例下面是一个简单的串口通信实例,用于在51单片机与计算机之间进行数据的传输。
```c#include <reg51.h>#include <stdio.h>#define BAUDRATE 9600#define FOSC 11059200void UART_init(){TMOD = 0x20; // 设置定时器1为模式2SCON = 0x50; // 设置串口为模式1,允许接收TH1 = 256 - FOSC / 12 / 32 / BAUDRATE; // 计算波特率定时器重载值TR1 = 1; // 启动定时器1EA = 1; // 允许中断ES = 1; // 允许串口中断}void UART_send_byte(unsigned char byte){SBUF = byte;while (!TI); // 等待发送完成TI = 0; // 清除发送完成标志位}unsigned char UART_receive_byte(){while (!RI); // 等待接收完成RI = 0; // 清除接收完成标志位return SBUF;}void UART_send_string(char *s){while (*s){UART_send_byte(*s);s++;}}void main(){UART_init();UART_send_string("Hello, World!"); while (1){unsigned char data = UART_receive_byte();// 对接收到的数据进行处理}}```总结:通过以上步骤,我们可以编写出简单的51单片机串口通信程序。
串口精灵源代码

for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
lstr=str[i];
//功能:若是在0-F之间的字符,则转换为相应的十六进制字符,否则返回-1
char CSCommTestDlg::ConvertHexChar(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
写出详细的编程过程,姑且叫串口调试助手源程序V1.0或VC串口通讯源程序吧,
我相信,如果你用VC编程,那么有了这个代码,就可以轻而易举地完成串口编
程任务了。(也许本文过于详细,高手就不用看)
开始吧:
C语言串口通信-源代码

C语言串口通信-源代码#include<tdio.h>#include<do.h>#include<conio.h>#include<tring.h>#defineCOM2320某2f8#defineCOMINT0某0b#defineMa某BufLen500#definePort82590某20#defineEofInt0某20taticcharintvectnum;taticunignedcharmakb;taticunignedcharBuffer[Ma某BufLen];taticintCharInBuf,CircIn,CircOut;taticvoid(interruptfar某OldAyncInt)();taticvoidinterruptfarAyncInt(void);unignedcharData,unignedcharStop,unignedcharParity){ unignedcharHigh,Low;intf;intvectnum=IntVectNum;CharInBuf=0;CircIn=0;CircOut=0;diable();OldAyncInt=getvect(IntVectNum);etvect(IntVectNum,Ay ncInt);enable();makb=inp(Port8259+1);if(IntVectNum==0某0c)outp(Port8259+1,makb&0某ef);eleoutp(Port8259+1,makb&0某f7);}taticvoidinterruptfarAyncInt(void){diable();if(CharInBuf<Ma某BufLen)if(CircIn<Ma某BufLen-1)CircIn++;eleCircIn=0;if(CircIn==CircOut)CircOut++;eleCharInBuf++;enable();outp(Port8259,EofInt);}voidRetore(void)etvect(intvectnum,OldAyncInt);outp(Port8259+1,makb);}intGetCharInBuf(unignedchar某Char) {intFlag;Flag=-1;if(CharInBuf>0){(某Char)=Buffer[CircOut];if(CircOut<Ma某BufLen-1)CircOut++; eleCircOut=0;CharInBuf--;Flag=0;}returnFlag;}intSendChar(unignedcharChar)return0;}main(){inti,c;unignedcharInChar;Init_COM(COM232,COMINT,1200,8,1,0); while(1){if(kbhit()){if((InChar=getch())==27)break; elewhile(SendChar(InChar));}if(GetCharInBuf(&InChar)==0)printf("%c",InChar);}Retore();}接收程序:#include<do.h>#include<ftream.h>#include<conio.h>#include<tdio.h>#include<tdlib.h>#include<math.h>#defineR某D0//接收voidInt(){unignedcharkey,key2;if(peek(0某40,port某2)==0){e某it(1);}ele{};Getportaddr(port);//得到串口地址SerInit(SER_BAUD_9600,SER_PARITY_EVEN|SER_BITS_8|SER_STOP_1) ;//初始化串口,设置波特率等SerOpen();do{if(kbhit()){key2=getch();if(key2==27){break;}};key=getb();if(key!=0某ff){printf("%某\t",key);FILE某fp;fp=fopen("C:\\Receivedata.dat","ab");//可选择接收数据的存放文件路径和文件名if(fp==NULL)printf("Fileopenerror!");//fputc(key,fp);fwrite(&key,izeof(unignedchar),1,fp);fcloe(fp);}}while(key2!=27);SerCloe();//printf("%dcharhabeenreceived\n",incount); //printf("%dcharhabeenended\n",outcount);//printf("\num=%d\n",um);}{printf("PleaeinputthereceiveCOMnum:(1~4)\n"); Cloe_Serial(intport_bae){outp(port_bae+SER_MCR,0);outp(port_bae+SER_IER,0);outp(PIC_IMR,old_int_mak);if(port_bae==COM_1){_do_etvect(INT_SER_PORT_0,Old_Ir);}ele{_do_etvect(INT_SER_PORT_1,Old_Ir);}}/某-------------发送应用----------------某/ voidmain(intargc,char某argv[]){charch,pre;intdone=0;FILE某fp;argc=2;if(argc<2){printf("\nUage:diplayfilename.wav!!!");//e某it(1);}if((fp=fopen(argv[1],"r+b"))==NULL){printf("cannotopenthefile\n");//e某it(0);}feek(fp,0,SEEK_SET);Open_Serial(COM_1,SER_BAUD_9600,SER_PARITY_EVEN|SER_STOP_1);printf("preanykeytobeginending");getch();//Serial_Write('');//该语句可用于发送单个字符while(!done&&ch!=EOF)//发送文件开始{ch=fgetc(fp);SER_BITS_8|//if(ch==EOF)Serial_Write(27);Serial_Write(ch);delay(30);if( kbhit()){pre=getch();if(pre==27){Serial_Write(27);done=1;}}}Cloe _Serial(COM_1);fcloe(fp);}下面介绍最重要的MFC:CWnd:窗口,它是大多数“看得见的东西”的父类(Window里几乎所有看得见的东西都是一个窗口,大窗口里有许多小窗口),比如视图CView、框架窗口CFrameWnd、工具条CToolBar、对话框CDialog、按钮CButton,etc;一个例外是菜单(CMenu)不是从窗口派生的。
C语言实现串口通信

C语言实现串口通信在使用系统调用函数进行串口通信之前,需要打开串口设备并设置相关参数。
打开串口设备可以使用open(函数,设置串口参数可以使用termios结构体和tcsetattr(函数。
以下是一个简单的串口通信接收数据的示例代码:```c#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <termios.h>int mainint fd; // 串口设备文件描述符char buff[255]; // 存储接收到的数据int len; // 接收到的数据长度//打开串口设备fd = open("/dev/ttyS0", O_RDONLY);if (fd < 0)perror("Failed to open serial port");return -1;}//设置串口参数struct termios options;tcgetattr(fd, &options);cfsetspeed(&options, B1200); // 设置波特率为1200 tcsetattr(fd, TCSANOW, &options);//接收数据while (1)len = read(fd, buff, sizeof(buff)); // 从串口读取数据if (len > 0)buff[len] = '\0'; // 将接收到的数据转为字符串printf("Received data: %s\n", buff);}}//关闭串口设备close(fd);return 0;```这段代码首先通过open(函数打开串口设备文件"/dev/ttyS0",然后使用tcgetattr(函数获取当前设置的串口参数,接着使用cfsetspeed(函数设置波特率为1200,最后使用tcsetattr(函数将设置好的串口参数写回。
c#上位机串口通信助手源代码详解

c#上位机串口通信助手源代码实例详解
一、功能
1软件打开时,自动检测有效COM端口
2软件打开时,自动复原到上次关闭时的状态
3不必关闭串口,即可直接进行更改初始化设置内容(串口号、波特率、数据位、停止位、校验位),可按更改后的信息自动将串口重新打开
4
5
6
7
8
扰
9
10
二、
三、
1
用
端口号:
数据位:
校验位:
发送(byte):tbSendCount接收(byte):tbReceivedCount
清空计数按钮:btnClearCount按16进制显示:cb16Display
接收区清空内容按钮:btnClearReceived保存数据按钮:btnSaveFile
接收数据框:tbReceivedData发送数据框:tbSendData
自动发送:cbAutomaticSend间隔时间:tbSpaceTime
按16进制发送:cb16Send发送区清空内容按钮:btnClearSend
2创建一个方法类
按Ctrl+shift+A快捷键创建一个类,名字叫Methods,代码为:
tbSendData.Text=tbSendDataStr;
}
}
//发送文本框键盘按键检测
privatevoidtbSendData_KeyPress(objectsender,KeyPressEventArgse) {
if(cb16Send.Checked)
{
//正则匹配
stringpattern="[0-9a-fA-F]|\b";//\b:退格键
if(m.Success)。
STM8的C语言编程-UART应用

STM8的C语言编程(8)-- UART应用串口通讯也是单片机应用中经常要用到,今天的实验就是利用STM8的UART资源,来进行串口通讯的实验。
实验程序的功能是以中断方式接收串口数据,然后将接收到的数据以查询方式发送到串口。
程序代码如下,首先要对STM8的UART进行初始化,初始化时要注意的是波特率寄存器的设置,当求出一个波特率的分频系数(一个16位的数)后,要将高4位和低4位写到BRR2中,而将中间的8位写到BRR1中,并且必须是先写BRR2,再写BRR1。
同样也是利用ST的开发工具,生成一个C语言的框架,然后修改其中的main.c,同时由于需要用到中断服务,因此还要修改stm8_interrupt_vector.c。
修改后,编译连接,然后下载到开发板上,再做一根与PC机相连的线,把开发板的串口与PC机的串口连接起来,注意,2、3脚要交叉。
在PC机上运行超级终端,设置波特率为9600,然后每按下一个按键,屏幕上就显示对应的字符。
修改后的main.c和stm8_interrupt_vector.c如下:// 程序描述:初始化UART,以中断方式接收字符,以查询方式发送// UART通讯参数:9600bps,8位数据,1位停止位,无校验#include "STM8S207C_S.h"// 函数功能:初始化UART// 输入参数:无// 输出参数:无// 返回值:无// 备注:无void UART3_Init(void){LINUART_CR2 = 0; // 禁止UART发送和接收LINUART_CR1 = 0; // b5 = 0,允许UART// b2 = 0,禁止校验LINUART_CR3 = 0; // b5,b4 = 00,1个停止位// 设置波特率,必须注意以下几点://(1) 必须先写BRR2//(2) BRR1存放的是分频系数的第11位到第4位,//(3) BRR2存放的是分频系数的第15位到第12位,和第3位到第0位// 例如对于波特率位9600时,分频系数=2000000/9600=208// 对应的十六进制数为00D0,BBR1=0D,BBR2=00LINUART_BRR2 = 0;LINUART_BRR1 = 0x0d; // 实际的波特率分频系数为00D0(208) // 对应的波特率为2000000/208=9600 LINUART_CR2 = 0x2C; // b3 = 1,允许发送// b2 = 1,允许接收// b5 = 1,允许产生接收中断}// 函数功能:从UART3发送一个字符// 输入参数:ch -- 要发送的字符// 输出参数:无// 返回值:无// 备注:无void UART3_SendChar(unsigned char ch){while((LINUART_SR & 0x80) == 0x00); // 若发送寄存器不空,则等待 LINUART_DR = ch; // 将要发送的字符送到数据寄存器}main(){// 首先初始化UART3UART3_Init();_asm("rim"); // 允许CPU全局中断while(1) // 进入无限循环{}}// 函数功能:UART3的接收中断服务程序// 输入参数:无// 输出参数:无// 返回值:无@far @interrupt void UART3_Recv_IRQHandler (void){unsigned char ch;ch = LINUART_DR; // 读入接收到的字符 UART3_SendChar(ch); // 将字符发送出去}/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices* Copyright (c) 2007 STMicroelectronics*/typedef void @far (*interrupt_handler_t)(void);struct interrupt_vector {unsigned char interrupt_instruction;interrupt_handler_t interrupt_handler;};@far @interrupt void NonHandledInterrupt (void){/* in order to detect unexpected events during development,it is recommended to set a breakpoint on the following instruction */return;}extern void _stext(); /* startup routine */extern @far @interrupt void UART3_Recv_IRQHandler();struct interrupt_vector const _vectab[] ={0x82, (interrupt_handler_t)_stext}, /* reset */{0x82, NonHandledInterrupt}, /* trap */{0x82, NonHandledInterrupt}, /* irq0 */{0x82, NonHandledInterrupt}, /* irq1 */{0x82, NonHandledInterrupt}, /* irq2 */{0x82, NonHandledInterrupt}, /* irq3 */{0x82, NonHandledInterrupt}, /* irq4 */{0x82, NonHandledInterrupt}, /* irq5 */{0x82, NonHandledInterrupt}, /* irq6 */{0x82, NonHandledInterrupt}, /* irq7 */{0x82, NonHandledInterrupt}, /* irq8 */{0x82, NonHandledInterrupt}, /* irq9 */{0x82, NonHandledInterrupt}, /* irq10 */{0x82, NonHandledInterrupt}, /* irq11 */{0x82, NonHandledInterrupt}, /* irq12 */{0x82, NonHandledInterrupt}, /* irq13 */{0x82, NonHandledInterrupt}, /* irq14 */{0x82, NonHandledInterrupt}, /* irq15 */{0x82, NonHandledInterrupt}, /* irq16 */{0x82, NonHandledInterrupt}, /* irq17 */{0x82, NonHandledInterrupt}, /* irq18 */{0x82, NonHandledInterrupt}, /* irq19 */{0x82, NonHandledInterrupt}, /* irq20 */{0x82, UART3_Recv_IRQHandler}, /* irq21 */{0x82, NonHandledInterrupt}, /* irq22 */{0x82, NonHandledInterrupt}, /* irq23 */{0x82, NonHandledInterrupt}, /* irq24 */{0x82, NonHandledInterrupt}, /* irq25 */{0x82, NonHandledInterrupt}, /* irq26 */{0x82, NonHandledInterrupt}, /* irq27 */{0x82, NonHandledInterrupt}, /* irq28 */{0x82, NonHandledInterrupt}, /* irq29 */};2010-8-6程序备份/* MAIN.C file** Copyright (c) 2002-2005 STMicroelectronics*/#include "STM8S103f3p.h"/////////////////////////////////////////void Init_UART1(void){UART1_CR1=0x00;UART1_CR2=0x00;UART1_CR3=0x00;// 设置波特率,必须注意以下几点:// (1) 必须先写BRR2// (2) BRR1存放的是分频系数的第11位到第4位,// (3) BRR2存放的是分频系数的第15位到第12位,和第3位// 到第0位// 例如对于波特率位9600时,分频系数=2000000/9600=208// 对应的十六进制数为00D0,BBR1=0D,BBR2=00UART1_BRR2=0x00;UART1_BRR1=0x0d;UART1_CR2=0x2c;//允许接收,发送,开接收中断}///////////////////////////////////////////void UART1_sendchar(unsigned char c){while((UART1_SR&0x80)==0x00);UART1_DR=c;}////////////////IO初始化////////////////////void init_gpio(void){//将pb5设置成推挽输出PB_DDR = 0x20; //数据方向PB_CR1 = 0x20; // 上拉、悬空PB_CR2 = 0x00;}/////////////////////////////////////////////main(){unsigned char i=0;init_gpio();Init_UART1();_asm("rim");//开中断,sim为关中断while (1);}//将收到的数据再发送出去@far @interrupt void UART1_Recv_IRQHandler (void) {unsigned char ch;ch=UART1_DR;UART1_sendchar(ch);PB_ODR^=0x20;return;}/*/////////////////////////////////////串口发送程序///////////////////////////////////////#include "STM8S103f3p.h"void delay(unsigned int ms){unsigned char i;while(ms != 0){for(i=0;i<250;i++){}for(i=0;i<75;i++){}ms--;}}/////////////uart初始化///////////////////void init_uart1(void){UART1_CR1=0x00;UART1_CR2=0x00;UART1_CR3=0x00;// 设置波特率,必须注意以下几点:// (1) 必须先写BRR2// (2) BRR1存放的是分频系数的第11位到第4位,// (3) BRR2存放的是分频系数的第15位到第12位,和第3位// 到第0位// 例如对于波特率位9600时,分频系数=2000000/9600=208// 对应的十六进制数为00D0,BBR1=0D,BBR2=00UART1_BRR2=0x00;UART1_BRR1=0x0d;UART1_CR2=0x2c; //允许接收,发送,开接收中断}//////////////uart发送程序//////////////////////void uart1_sendchar(unsigned char c){while((UART1_SR & 0x80)==0x00);UART1_DR=c;}//////////////初始化A/D模块/////////////////////void init_ad(void){ADC_CR2 = 0x00; // A/D结果数据左对齐ADC_CR1 = 0x00; // ADC时钟=主时钟/2=1MHZ// ADC转换模式=单次// 禁止ADC转换ADC_CSR = 0x03; // 选择通道3ADC_TDRL = 0x20;}///////////////读AD值/////////////////////unsigned char read_ad(void){unsigned char i=0;ADC_CR1 = 0x01; // CR1寄存器的最低位置1,使能ADC转换for(i=0;i<100;i++);// 延时一段时间,至少7uS,保证ADC模块的上电完成ADC_CR1 = ADC_CR1 | 0x01;// 再次将CR1寄存器的最低位置1// 使能ADC转换while((ADC_CSR&0x80)==0); // 等待ADC结束i = ADC_DRH; // 读出ADC结果的高8位return(i);}////////////////IO初始化////////////////////void init_gpio(void){//将pb5设置成推挽输出PB_DDR = 0x20; //数据方向PB_CR1 = 0x20; // 上拉、悬空PB_CR2 = 0x00;}/////////////////////////////////////////////main(){unsigned char i=0;init_uart1();init_ad();init_gpio();while (1){delay(1000);i=read_ad();uart1_sendchar(i);PB_ODR^=0x20;}}/////////////////////////////////////////////////*/友情提示:范文可能无法思考和涵盖全面,供参考!最好找专业人士起草或审核后使用,感谢您的下载!。
C语言串口通信助手代码

该程序全部由C写成没有C++ 更没用MFC完全是自娱自乐给需要的人一个参考#include "stdafx.h"#include <windowsx.h>#include "resource.h"#include "MainDlg.h"#include <windows.h>#include <stdio.h>#include <stdlib.h>HANDLE hComm;//用于获取串口打开函数的返回值(句柄或错误值)OVERLAPPED m_ov;COMSTAT comstat;DWORD m_dwCommEvents;TCHAR cRecs[200],cSends[100]; //接收字符串发送字符串char j=0,*cCom; //接收用统计数据大小变量端口选择BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){switch(uMsg){HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog); HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand); HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);}return FALSE;}/*系统初始化函数*/BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam){HWND hwndCombo1=GetDlgItem(hwnd,IDC_COMBO1);ComboBox_InsertString(hwndCombo1,-1,TEXT("COM1"));ComboBox_InsertString(hwndCombo1,-1,TEXT("COM2"));ComboBox_InsertString(hwndCombo1,-1,TEXT("COM3"));ComboBox_InsertString(hwndCombo1,-1,TEXT("COM4"));ComboBox_InsertString(hwndCombo1,-1,TEXT("COM5"));ComboBox_SetCurSel(hwndCombo1,0);void CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime);SetTimer(hwnd,1,1000,TimerProc);return TRUE;}/*监视串口错误时使用的函数*/bool ProcessErrorMessage(char* ErrorText){char *Temp = new char[200];LPVOID lpMsgBuf;FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language(LPTSTR) &lpMsgBuf,NULL);sprintf(Temp, "WARNING: %s Failed with the following error:\n%s\nPort: %d\n", (char*)ErrorText, lpMsgBuf, "com2"); MessageBox(NULL, Temp, "Application Error", MB_ICONSTOP); LocalFree(lpMsgBuf);delete[] Temp;return true;}bool openport(char *portname)//打开串口{hComm = CreateFile(portname, //串口号“com1”“com2” 调用方法:bool open; open=openport("com2");GENERIC_READ | GENERIC_WRITE, //允许读写0, //通讯设备必须以独占方式打开0, //无安全属性OPEN_EXISTING, //通讯设备已存在FILE_FLAG_OVERLAPPED, //异步I/O0); //通讯设备不能用模板打开if (hComm == INVALID_HANDLE_VALUE) //如果被占用或是没有打开时返回的是这个错误代码CloseHandle(hComm);return FALSE;}elsereturn true;}bool setupdcb(int rate_arg)//设置port的属性{DCB dcb;int rate= rate_arg;memset(&dcb,0,sizeof(dcb));if(!GetCommState(hComm,&dcb))//获取当前DCB配置return FALSE;// set DCB to configure the serial portdcb.DCBlength = sizeof(dcb);dcb.BaudRate = rate;dcb.Parity = NOPARITY; //奇偶校验值0~4分别对应无校验、奇校验、偶校验、校验置位、校验清零dcb.fParity = 0; //为1的话激活奇偶校验检查dcb.StopBits = ONESTOPBIT;//停止位个数,0~2分别对应1位、1.5位、2位停止位dcb.ByteSize = 8; //数据位数dcb.fOutxCtsFlow = 0;dcb.fOutxDsrFlow = 0;dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fDsrSensitivity = 0;dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fOutX = 0;dcb.fInX = 0;dcb.fErrorChar = 0;dcb.fBinary = 1;dcb.fNull = 0;dcb.fAbortOnError = 0;dcb.wReserved = 0;dcb.XonLim = 2;dcb.XoffLim = 4;dcb.XonChar = 0x13;dcb.XoffChar = 0x19;dcb.EvtChar = 0;// set DCBif(!SetCommState(hComm,&dcb))return false;elsereturn true;}/*串口读取相关时间设置*/bool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,DWORD ReadTotalconstant,DWORD WriteTotalMultiplier,DWORD WriteTotalconstant){COMMTIMEOUTS timeouts;timeouts.ReadIntervalTimeout=ReadInterval; //读取两个字节间隔最大值mS如超过立即返回不再读取timeouts.ReadTotalTimeoutConstant=ReadTotalconstant; //如果同下面一个都为0 则无论是否读到数据都返回// 可以毫秒为单位指定一个乘数,该乘数用来计算读操作的总限时时间timeouts.ReadTotalTimeoutMultiplier=ReadTotalMultiplier; // 以毫秒为单位指定一个常数,用于计算读操作的总限时时间0表示不限时timeouts.WriteTotalTimeoutConstant=WriteTotalconstant;// 写操作延时同上timeouts.WriteTotalTimeoutMultiplier=WriteTotalMultiplier;if(!SetCommTimeouts(hComm, &timeouts))return false;elsereturn true;}int Clearn() //清除buff中的内容并返回buff中现有数据量的大小并读取错误原因{DWORD dwError = 0;DWORD BytesRead = 0;ClearCommError(hComm, &dwError, &comstat);return comstat.cbInQue; //返回buff中数据量}/*串口数据接收读取函数*/void ReceiveChar(){BOOL bRead = TRUE;BOOL bResult = TRUE;DWORD dwError = 0;DWORD BytesRead = 0;char i=0,n;char RXBuff;j=0;while (i-n){n=i;Sleep(10);bResult = ClearCommError(hComm, &dwError, &comstat); i=(char)comstat.cbInQue;for (;i>0;i--){if (bRead)bResult = ReadFile(hComm, // Handle to COMM port &RXBuff, // RX Buffer Pointer1, // Read one byte&BytesRead, // Stores number of bytes read&m_ov); // pointer to the m_ov structure// printf("%c",RXBuff);cRecs[j++]=(char)RXBuff;if (!bResult){switch (dwError = GetLastError()){case ERROR_IO_PENDING:{bRead = FALSE;break;}default: break;}elsebRead = TRUE; // close if (bRead)if (!bRead){bRead = TRUE;bResult = GetOverlappedResult(hComm, // Handle to COMM port&m_ov, // Overlapped structure&BytesRead, // Stores number of bytes readTRUE); // Wait flag}}}bool WriteChar(char* m_szWriteBuffer,DWORD m_nToSend) //写字符的函数{BOOL bWrite = TRUE;BOOL bResult = TRUE;DWORD BytesSent = 0;HANDLE m_hWriteEvent;ResetEvent(m_hWriteEvent);if (bWrite){m_ov.Offset = 0;m_ov.OffsetHigh = 0;// Clear bufferbResult = WriteFile(hComm, // Handle to COMM Portm_szWriteBuffer, // Pointer to message buffer in calling finction m_nToSend, // Length of message to send&BytesSent, // Where to store the number of bytes sent&m_ov ); // Overlapped structureif (!bResult){DWORD dwError = GetLastError();switch (dwError){case ERROR_IO_PENDING:{// continue to GetOverlappedResults()BytesSent = 0;bWrite = FALSE;break;}default:// all other error codesbreak;}}} // end if(bWrite)if (!bWrite){bWrite = TRUE;bResult = GetOverlappedResult(hComm, // Handle to COMM port &m_ov, // Overlapped structure&BytesSent, // Stores number of bytes sentTRUE); // Wait flag// deal with the error codeif (!bResult){printf("GetOverlappedResults() in WriteFile()");}} // end if (!bWrite)// Verify that the data size send equals what we tried to send if (BytesSent != m_nToSend){printf("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)m_szWriteBuffer));}return true;}/*window时间函数回调*/void CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime){SYSTEMTIME time; //定义机构体变量timeGetLocalTime(&time); //取系统时间以指针方式TCHAR strTime[256]; //程序只有一个作用wsprintf(strTime,"%04d-%02d-%02d %02d:%02d:%02d",time.wYear, //就是读取系统时间time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond);//然后写进strTimeSetDlgItemText(hwnd,IDC_TIME,strTime); //这个字符串}void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify){switch(id){case IDC_SEND:{GetDlgItemText (hwnd,IDC_EDIT2,cSends,sizeof(cSends)); unsigned n=sizeof(cSends); //n是通知串口将发送字节的长度char send[100];wsprintf (send,"%s",cSends);WriteChar(send,n-1);SetCommMask(hComm, EV_RXCHAR); //监视串口是否接收有数据ReceiveChar(); //读取串口sbuff中数据cRecs[j]='\0'; //将cRecs转为字符串SetDlgItemText(hwnd,IDC_EDIT1,cRecs);} break;/*case IDC_RECEIVE: //暂时未用采用直接显示的方式{}break;*/case IDC_CHECK:{int ctr;HWND hwndCombo1=GetDlgItem(hwnd,IDC_COMBO1);ctr = ComboBox_GetCurSel(hwndCombo1);switch (ctr){case 0:cCom="com1";break;case 1:cCom="com2";break;case 2:cCom="com3";break;case 3:cCom="com4";break;case 4:cCom="com5";break;default: cCom="com1";break;}if (openport(cCom)){ SetDlgItemText(hwnd,IDC_EDIT3,"OK !");MessageBox(hwnd,"串口打开成功!","",0); }elseSetDlgItemText(hwnd,IDC_EDIT3,"FAIL");if(setupdcb(9600)&&setuptimeout(1024,0,0,20,1000)) //初始化串口属性波特率9600SetDlgItemText(hwnd,IDC_EDIT4,"串口属性设置成功");elseSetDlgItemText(hwnd,IDC_EDIT4,"串口初始化失败!"); PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);}default:break;}}void Main_OnClose(HWND hwnd) {EndDialog(hwnd, 0);}。
三菱PLC编程口通信C语言源代码三菱plc

三菱PLC编程口通信C语言源代码 - 三菱plcfxnew.h#define DELAY_TIMES 30000L#define TRUE 1#define FALSE 0#define TRUE 1#define FALSE 0#define FORCE_ON 0x37#define FORCE_OFF 0x38void init_plc(void);int check_plc(void);int _read_data_register(unsigned int uAddress,unsigned int number);int _read_mdata_register(unsigned int uAddress,unsigned int number);int _write_data_register(unsigned int uAddress,unsigned int number);int _force_m_contact(unsigned int uAddress,unsigned char ucOn_off);int read_data_register(unsigned int uAddress,unsigned intnumber);int read_mdata_register(unsigned int uAddress,unsigned int number);int write_data_register(unsigned int uAddress,unsigned int number);int force_m_contact(unsigned int uAddress,unsigned char ucOn_off);int _read_m_register(unsigned int uAddress,unsigned int number);int read_m_register(unsigned int uAddress,unsigned int number);int TESTING=0;unsigned int uRead_value[25];unsigned int uWrite_value[25];unsigned int COMM_PORT=1;unsigned int STATS_PORT=0x2fd;unsigned int DATA_PORT=0x2f8;void init_plc(void){ _AX=0xfa;_DX=COMM_PORT;geninterrupt(0x14);while((inportb(STATS_PORT)1)!=0)inportb(DATA_PORT);}int check_plc(void){ long lTmp;if(TESTING==1)return TRUE;init_plc();for(lTmp=0L;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)0x20)!=0) break;}if(lTmp=DELAY_TIMES)return(FALSE);outportb(DATA_PORT,5);disable();for(lTmp=0L;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)1)!=0) break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}if((lTmp=inportb(DATA_PORT))==6){ enable();return(TRUE);}else{ enable();return(FALSE);}}int read_data_register(unsigned int uAddress,unsigned int number){int i;for(i=0;i3;i++)if(_read_data_register(uAddress,number)==TRUE)return TRUE;return FALSE;}int _read_data_register(unsigned int uAddress,unsigned int number){ unsigned char uSend[]={2,0x30,0x30,0x30,0x30,0x30,0x30,0x32,3,0x30,0x30}; unsigned char uReceive[104];unsigned int uTmp;unsigned int uSum;unsigned int num;long lTmp;int i,j;if(TESTING==1){ for(i=0;inumber;i++)uRead_value[i]=0;return TRUE;}init_plc();num=number*2;if((num/16)=10)uSend[6]=(unsigned char)(num/16+0x41-10);elseuSend[6]=(unsigned char)(num/16+0x30);if((num%16)=10)uSend[7]=(unsigned char)((num%16)+0x41-10); elseuSend[7]=(unsigned char)((num%16)+0x30);uAddress=uAddress*2+0x1000;uTmp=uAddress 0x000f;uSend[5]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);uTmp=(uAddress4) 0x000f;uSend[4]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress8) 0x000f;uSend[3]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress12)0x000f;uSend[2]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uSum=0;for(i=1;i9;i++)uSum=uSum+(unsigned char)uSend[i];uTmp=uSum0x000f;uSend[10]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uSum4)0x000f;uSend[9]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);for(i=0;i11;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++){ if((inportb(STATS_PORT)0x20)!=0)break;}if(lTmp=DELAY_TIMES){return(FALSE);}outportb(DATA_PORT,uSend[i]); }disable();for(lTmp=0;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)1)!=0) break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}uReceive[0]=inportb(DATA_PORT);if(uReceive[0]!=2){enable();return(FALSE);}for(i=1;inumber*4+4;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++){ if((inportb(STATS_PORT)1)!=0)break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}uReceive[i]=inportb(DATA_PORT);}enable();uSum=0;for(i=1;inumber*4+2;i++)uSum=uSum+(unsigned int)uReceive[i];uTmp=uSum0xf;uTmp=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);if((unsigned char)uTmp!=uReceive[number*4+3])return(FALSE); uTmp=(uSum4)0xf;uTmp=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);if((unsigned char)uTmp!=uReceive[number*4+2])return(FALSE); for(j=0;jnumber;j++){for(i=j*4+1;ij*4+5;i++)uReceive[i]=(uReceive[i]0x39)?uReceive[i]-0x41+0xa:uReceive [i]-0x30;uRead_value[j]=(((((uReceive[j*4+3]4)+uReceive[j*4+4])4)+uR eceive[j*4+1])4)+uReceive[j*4+2];}return TRUE;}int read_mdata_register(unsigned int uAddress,unsigned int number){int i;for(i=0;i3;i++)if(_read_mdata_register(uAddress,number)==TRUE)return TRUE;return FALSE;}int _read_mdata_register(unsigned int uAddress,unsigned int number){ unsigned char uSend[]={2,0x30,0x30,0x30,0x30,0x30,0x30,0x32,3,0x30,0x30};unsigned char uReceive[104];unsigned int uTmp;unsigned int uSum;unsigned int num;long lTmp;int i,j;if(TESTING==1){ for(i=0;inumber;i++)uRead_value[i]=0; return TRUE;}init_plc();num=number*2;if((num/16)=10)uSend[6]=(unsigned char)(num/16+0x41-10); elseuSend[6]=(unsigned char)(num/16+0x30);if((num%16)=10)uSend[7]=(unsigned char)((num%16)+0x41-10); elseuSend[7]=(unsigned char)((num%16)+0x30);/*uAddress=uAddress*2+0x1000;*/uTmp=uAddress 0x000f;uSend[5]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress4) 0x000f;uSend[4]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress8) 0x000f;uSend[3]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress12)0x000f;uSend[2]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uSum=0;for(i=1;i9;i++)uSum=uSum+(unsigned char)uSend[i];uTmp=uSum0x000f;uSend[10]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uSum4)0x000f;uSend[9]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); for(i=0;i11;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++){ if((inportb(STATS_PORT)0x20)!=0)break;}if(lTmp=DELAY_TIMES){return(FALSE);}outportb(DATA_PORT,uSend[i]); }disable();for(lTmp=0;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)1)!=0) break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}uReceive[0]=inportb(DATA_PORT);if(uReceive[0]!=2){enable();return(FALSE);}for(i=1;inumber*4+4;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++){ if((inportb(STATS_PORT)1)!=0)break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}uReceive[i]=inportb(DATA_PORT);}enable();uSum=0;for(i=1;inumber*4+2;i++)uSum=uSum+(unsigned int)uReceive[i];uTmp=uSum0xf;uTmp=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);if((unsigned char)uTmp!=uReceive[number*4+3])return(FALSE); uTmp=(uSum4)0xf;uTmp=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);if((unsigned char)uTmp!=uReceive[number*4+2])return(FALSE); for(j=0;jnumber;j++){for(i=j*4+1;ij*4+5;i++)uReceive[i]=(uReceive[i]0x39)?uReceive[i]-0x41+0xa:uReceive [i]-0x30;uRead_value[j]=(((((uReceive[j*4+3]4)+uReceive[j*4+4])4)+uR eceive[j*4+1])4)+uReceive[j*4+2];}return TRUE;}int write_data_register(unsigned int uAddress,unsigned int number){int i;for(i=0;i3;i++)if(_write_data_register(uAddress,number)==TRUE)return TRUE;return FALSE;}int _write_data_register(unsigned int uAddress,unsigned int number){ unsigned char uSend[111];unsigned int uTmp,uSum,num;long lTmp;int i;if(TESTING==1)return TRUE;init_plc();uSend[0]=2;uSend[1]=0x31;uSend[number*4+8]=3;num=(number*2)/16;if(num=10)uSend[6]=num+0x41-10;else uSend[6]=num+0x30;num=(number*2)%16;if(num=10)uSend[7]=num+0x41-10;else uSend[7]=num+0x30;uAddress=0x1000+2*uAddress;uTmp=uAddress0x000f;uSend[5]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress4)0x000f;uSend[4]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress8)0x000f;uSend[3]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress12)0x000f;uSend[2]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);for(i=0;inumber;i++){uTmp=uWrite_value[i]0x000f;uSend[i*4+9]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);uTmp=(uWrite_value[i]4)0x000f;uSend[i*4+8]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);uTmp=(uWrite_value[i]8)0x000f;uSend[i*4+11]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uWrite_value[i]12)0x000f;uSend[i*4+10]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);}uSum=0;for(i=1;i9+number*4;i++)uSum+=uSend[i];uTmp=uSum0x000f;uSend[number*4+10]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uSum4)0x000f;uSend[number*4+9]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); for(i=0;i11+number*4;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++){ if((inportb(STATS_PORT)0x20)!=0)break;}if(lTmp=DELAY_TIMES){/*enable();*/return(FALSE);}outportb(DATA_PORT,uSend[i]);}disable();for(lTmp=0L;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)1)!=0) break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}if(inportb(DATA_PORT)!=6){enable();return(FALSE);}else{enable();return(TRUE);}}int force_m_contact(unsigned uAddress,unsigned char ucOn_off) {int i;for(i=0;i3;i++)if(_force_m_contact(uAddress,ucOn_off)==TRUE)return TRUE;return FALSE;}int _force_m_contact(unsigned uAddress,unsigned char ucOn_off) { unsigned uSend[]={2,0x37,0x30,0x30,0x30,0x30,3,0x30,0x30}; unsigned uTmp,uSum,i;long lTmp;if(TESTING==1)return TRUE;init_plc();uAddress=uAddress+0x800;uSend[1]=ucOn_off;uTmp=uAddress0x000f;uSend[3]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress4)0x000f;uSend[2]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress8)0x000f;uSend[5]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress12)0x000f;uSend[4]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uSum=0;for(i=1;i7;i++)uSum+=uSend[i];uTmp=uSum0x000f;uSend[8]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uSum4)0x000f;uSend[7]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); for(i=0;i9;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++){ if((inportb(STATS_PORT)0x20)!=0)break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}outportb(DATA_PORT,uSend[i]);}disable();for(lTmp=0L;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)1)!=0) break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}if(inportb(DATA_PORT)!=6){enable();return(FALSE);}else{ enable();return(TRUE);}}int read_m_register(unsigned int uAddress,unsigned int number) {int i;for(i=0;i3;i++)if(_read_m_register(uAddress,number)==TRUE)return TRUE;return FALSE;}int _read_m_register(unsigned int uAddress,unsigned int number){ unsigned char uSend[]={2,0x30,0x30,0x30,0x30,0x30,0x30,0x32,3,0x30,0x30}; unsigned char uReceive[54];unsigned int uTmp;unsigned int uSum;unsigned int num;long lTmp;int i,j;if(TESTING==1){ for(i=0;inumber;i++)uRead_value[i]=0;return TRUE;}init_plc();num=number;if((num/16)=10)uSend[6]=(unsigned char)(num/16+0x41-10);elseuSend[6]=(unsigned char)(num/16+0x30);if((num%16)=10)uSend[7]=(unsigned char)((num%16)+0x41-10); elseuSend[7]=(unsigned char)((num%16)+0x30);uAddress=uAddress/8+0x100;uTmp=uAddress 0x000f;uSend[5]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress4) 0x000f;uSend[4]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress8) 0x000f;uSend[3]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uAddress12)0x000f;uSend[2]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);uSum=0;for(i=1;i9;i++)uSum=uSum+(unsigned char)uSend[i];uTmp=uSum0x000f;uSend[10]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa); uTmp=(uSum4)0x000f;uSend[9]=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);for(i=0;i11;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++){ if((inportb(STATS_PORT)0x20)!=0)break;}if(lTmp=DELAY_TIMES){/*enable();*/return(FALSE);}outportb(DATA_PORT,uSend[i]);}disable();for(lTmp=0;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)1)!=0) break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}uReceive[0]=inportb(DATA_PORT);if(uReceive[0]!=2){enable();return(FALSE);}for(i=1;inumber*2+4;i++){ for(lTmp=0L;lTmpDELAY_TIMES;lTmp++) { if((inportb(STATS_PORT)1)!=0) break;}if(lTmp=DELAY_TIMES){enable();return(FALSE);}uReceive[i]=inportb(DATA_PORT);}enable();uSum=0;for(i=1;inumber*2+2;i++)uSum=uSum+(unsigned int)uReceive[i];uTmp=uSum0xf;uTmp=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);if((unsigned char)uTmp!=uReceive[number*2+3])return(FALSE); uTmp=(uSum4)0xf;uTmp=(uTmp10)?(uTmp+0x30):(uTmp+0x41-0xa);if((unsigned char)uTmp!=uReceive[number*2+2])return(FALSE); for(j=0;jnumber;j++){for(i=j*2+1;ij*2+3;i++)uReceive[i]=(uReceive[i]0x39)?uReceive[i]-0x41+0xa:uReceive [i]-0x30;uRead_value[j]=((uReceive[j*2+1])4)+uReceive[j*2+2];}return TRUE; }。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <stdio.h>#include <dos.h>#include <conio.h>#include <string.h>#define COM232 0x2f8#define COMINT 0x0b#define MaxBufLen 500#define Port8259 0x20#define EofInt 0x20static int comportaddr;static char intvectnum;static unsigned char maskb;static unsigned char Buffer[MaxBufLen];static int CharsInBuf,CircIn,CircOut;static void (interrupt far *OldAsyncInt)();static void interrupt far AsyncInt(void);void Init_COM(int ComPortAddr, unsigned char IntVectNum, int Baud, unsigned char Data, unsigned char Stop, unsigned char Parity) {unsigned char High,Low;int f;comportaddr=ComPortAddr;intvectnum=IntVectNum;CharsInBuf=0;CircIn=0;CircOut=0;f=(Baud/100);f=1152/f; High=f/256;Low=f-High*256;outp(ComPortAddr+3,0x80);outp(ComPortAddr,Low);outp(ComPortAddr+1,High);Data=(Data-5)|((Stop-1)*4);if(Parity==2) Data=Data|0x18;else if(Parity==1) Data=Data|0x8;outp(ComPortAddr+3,Data);outp(ComPortAddr+4,0x0a);outp(ComPortAddr+1,0x01);disable();OldAsyncInt=getvect( IntVectNum );setvect( IntVectNum, AsyncInt );enable();maskb=inp(Port8259+1);if(IntVectNum==0x0c)outp(Port8259+1,maskb&0xef); else outp(Port8259+1,maskb&0xf7);}static void interrupt far AsyncInt(void){disable();if(CharsInBuf<MaxBufLen)Buffer[CircIn]=inp(comportaddr);if(CircIn<MaxBufLen-1) CircIn++;else CircIn=0;if(CircIn==CircOut) CircOut++;else CharsInBuf++;enable();outp(Port8259,EofInt);}void Restore(void){setvect(intvectnum,OldAsyncInt);outp(Port8259+1,maskb);}int GetCharInBuf(unsigned char *Char){int Flag;Flag=-1;if(CharsInBuf>0){(*Char)=Buffer[CircOut];if(CircOut<MaxBufLen-1)CircOut++;else CircOut=0;CharsInBuf--;Flag=0;}return Flag;}int SendChar(unsigned char Char){if((inp(comportaddr+5)&0x20)==0) return -1; outp(comportaddr,Char);return 0;}main(){int i,c;unsigned char InChar;Init_COM(COM232,COMINT,1200,8,1,0);while(1){if(kbhit()){if((InChar=getch())==27)break;else while(SendChar(InChar));}if(GetCharInBuf(&InChar)==0)printf("%c",InChar);}Restore();}接收程序:#include <dos.h>#include <fstream.h>#include <conio.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#define RXD 0 //接收#define TXD 0 //发送#define LSB 0 //波特率调节低8位#define MSB 1 //波特率调节高8位#define IER 1 // 中断起用寄存器#define IIR 2 //中断标识寄存器#define LCR 3 //线路控制寄存器#define MCR 4 //调制解调器控制寄存器#define LSR 5 //线路状态寄存器#define MSR 6 //调制解调器状态寄存器#define IERV 1#define OUT2 0x08#define ERTS 2#define EDTR 1#define EMPTY 0X20#define READY 0X30#define ICREG 0X20#define IMASKREG 0X21#define EOI 0X20#define WAITCOUNT 5000#define BUFFLEN 2048 //用于存储字符的数组的界#define ALTE 0X12#define ALTQ 0X10#define SER_BAUD_1200 96#define SER_BAUD_2400 48#define SER_BAUD_9600 0x0C#define SER_BAUD_19200 6#define SER_STOP_1 0 /*/ 1 stop bit per character*/#define SER_STOP_2 4 /*/ 2 stop bits per character*/#define SER_BITS_5 0 /*/ send 5 bit characters*/#define SER_BITS_6 1 /*/ send 6 bit characters*/#define SER_BITS_7 2 /*/ send 7 bit characters*/#define SER_BITS_8 3 /*/ send 8 bit characters*/#define SER_PARITY_NONE 0 /*/ no parity*/#define SER_PARITY_ODD 8 /*/ odd parity*/#define SER_PARITY_EVEN 24 /*/ even parity*/int port;int ComNum;unsigned portaddr;unsigned portf;unsigned int baudtable[]={0x180,0xc0,0x60,0x30,0x18,0x0c,0x06}; unsigned char paritytable[]={0x08,0x18,0x00,0x28,0x38}; unsigned char buffer[BUFFLEN];//recv bufint buffin=0;int buffout=0;int incount=0;int outcount=0;void interrupt(*vect_com)(...);void putb(unsigned char ch)//write a char to the recvbuf 将中断得到的数据写到缓冲区{int temp;temp=buffin;if(++buffin==BUFFLEN)buffin=0;if(buffin!=buffout){buffer[buffin]=ch;// printf("bufferin[%d]=%c",buffer[buffin]);// getch();}elsebuffin=temp;};unsigned char getb()//read a char from the recvbuf{if(buffout!=buffin){if(++buffout==BUFFLEN)buffout=0;//printf("bufferout[%d]=%c",buffout,buffer[buffout]);return(buffer[buffout]);}elsereturn(0xff);};/*unsigned char sender( unsigned char ch){outportb(portaddr2+TXD,ch);printf("\nsender outportdata=%c\n",ch);outcount++;return(1);};*/void interrupt receiver(...){unsigned char ch;ch=inportb(portaddr+RXD);putb(ch);incount++; //记录接收了多少个数据。
outportb(ICREG,EOI);};void SerInit(int baud,int configuration){disable();outportb(portaddr+LCR,0x80);outportb(portaddr+LSB,baud);outportb(portaddr+MSB,0x00);outportb(portaddr+LCR,configuration);enable();};void SerOpen(void){vect_com=getvect(portf+8);disable();inportb(portaddr+RXD);inportb(portaddr+MSR);inportb(portaddr+LSR);inportb(portaddr+IIR);outportb(portaddr+IER,IERV);outportb(portaddr+MCR,OUT2|ERTS|EDTR);outportb(IMASKREG,inportb(IMASKREG)&(~(1<<portf))); setvect(portf+8,receiver);enable();};void SerClose(void){disable();outportb(portaddr+IER,0);outportb(portaddr+MCR,0);outportb(IMASKREG,inportb(IMASKREG)|(1<<portf)); enable();setvect(portf+8,vect_com);};void Getportaddr(int port){switch (port){case 0: portaddr=0x3F8; break;case 1: portaddr=0x2F8; break;case 2: portaddr=0x3E8; break;case 3: portaddr=0x2E8; break;}portf=(port==0)?4:3;};void Int(){unsigned char key,key2;port=ComNum-1;if(peek(0x40,port*2)==0){printf("have no special com .\n");exit(1);}else{printf("The used port is :COM%d\n",ComNum);};Getportaddr(port); // 得到串口地址SerInit(SER_BAUD_9600,SER_PARITY_EVEN| SER_BITS_8 | SER_STOP_1); //初始化串口,设置波特率等SerOpen();do{if(kbhit()){key2=getch();if(key2==27){ break;}};key=getb();if(key!=0xff){printf("%x\t",key);FILE *fp;fp=fopen("C:\\Receivedata.dat","ab"); //可选择接收数据的存放文件路径和文件名if(fp==NULL) printf("File open error!");// fputc(key,fp);fwrite(&key,sizeof(unsigned char),1,fp);fclose(fp);}}while (key2!=27);SerClose();// printf("%d char has been received\n",incount);// printf("%d char has been sended\n",outcount);// printf("\nsum=%d \n",sum);}void main()//com1 receive{printf("Please input the receive COMnum:(1~4)\n");cin>>ComNum;printf("the current com set is:9600,even, data bits 8, stop 1,"); Int();printf("\n\nOVER\n");exit(0);};---------------发送文件程序#include <dos.h>#include <bios.h>#include <stdio.h>#include <math.h>#include <conio.h>#include <graphics.h>#ifdef __cplusplus#define __CPPARGS ...#else#define __CPPARGS#endif#define SER_RBF 0#define SER_THR 0#define SER_IER 1#define SER_IIR 2#define SER_LCR 3#define SER_MCR 4#define SER_LSR 5#define SER_MSR 6#define SER_DLL 0#define SER_DLH 1#define SER_BAUD_1200 96#define SER_BAUD_2400 48#define SER_BAUD_9600 12#define SER_BAUD_19200 6#define SER_GP02 8#define COM_1 0x3F8#define COM_2 0x2F8 /*/ base port address of port 1*/#define COM_3 0x3E8#define COM_4 0x2E8#define SER_STOP_1 0 /*/ 1 stop bit per character*/#define SER_STOP_2 4 /*/ 2 stop bits per character*/#define SER_BITS_5 0 /*/ send 5 bit characters*/#define SER_BITS_6 1 /*/ send 6 bit characters*/#define SER_BITS_7 2 /*/ send 7 bit characters*/#define SER_BITS_8 3 /*/ send 8 bit characters*/#define SER_PARITY_NONE 0 /*/ no parity*/#define SER_PARITY_ODD 8 /*/ odd parity*/#define SER_PARITY_EVEN 24 /*/ even parity*/#define SER_DIV_LATCH_ON 128 /*/ used to turn reg 0,1 into divisor latch*/#define PIC_IMR 0x21 /*/ pic's interrupt mask reg.*/#define PIC_ICR 0x20 /*/ pic's interupt control reg.*/#define INT_SER_PORT_0 0x0C /*/ port 0 interrupt com 1 & 3*/#define INT_SER_PORT_1 0x0B /*/ port 0 interrupt com 2 & 4*/#define SERIAL_BUFF_SIZE 128 /*/ current size of circulating receive buffer*/ void interrupt far (*Old_Isr)(__CPPARGS); /*/ holds old com port interrupt handler*/ char ser_buffer[SERIAL_BUFF_SIZE]; /*/ the receive buffer*/int ser_end = -1,ser_start=-1; /*/ indexes into receive buffer*/int ser_ch, char_ready=0; /*/ current character and ready flag*/int old_int_mask; /*/ the old interrupt mask on the PIC*/int open_port; /*/ the currently open port*/int serial_lock = 0; /*/ serial ISR semaphore so the buffer*//*/ isn't altered will it is being written*//*/ to by the ISR*//*-------------写串口-----------------*/void interrupt far Serial_Isr(__CPPARGS){serial_lock = 1;ser_ch = inp(open_port + SER_RBF);if (++ser_end > SERIAL_BUFF_SIZE-1)ser_end = 0;ser_buffer[ser_end] = ser_ch;++char_ready;outp(PIC_ICR,0x20);serial_lock = 0;}int Ready_Serial(){return(char_ready);}/*--------------读串口--------------*/int Serial_Read(){int ch;while(serial_lock){}if (ser_end != ser_start){if (++ser_start > SERIAL_BUFF_SIZE-1)ser_start = 0;ch = ser_buffer[ser_start];printf("%x",ch);if (char_ready > 0)--char_ready;return(ch);}elsereturn(0);}/*--------------写串口-----------------*/Serial_Write(char ch){while(!(inp(open_port + SER_LSR) & 0x20)){}asm clioutp(open_port + SER_THR, ch);asm sti}/*-----------初始化串口---------------*/Open_Serial(int port_base, int baud, int configuration){open_port = port_base;disable();outp(port_base + SER_LCR, SER_DIV_LATCH_ON);outp(port_base + SER_DLL, baud);outp(port_base + SER_DLH, 0);outp(port_base + SER_LCR, configuration);outp(port_base + SER_MCR, SER_GP02);outp(port_base + SER_IER, 1);if (port_base == COM_1 || port_base==COM_3){Old_Isr = _dos_getvect(INT_SER_PORT_0);_dos_setvect(INT_SER_PORT_0, Serial_Isr);printf("\nOpening Communications Channel Com Port #1/3...\n");}else{Old_Isr = _dos_getvect(INT_SER_PORT_1);_dos_setvect(INT_SER_PORT_1, Serial_Isr);printf("\nOpening Communications Channel Com Port #2/4...\n");}old_int_mask = inp(PIC_IMR);outp(PIC_IMR, (port_base==COM_1) ? (old_int_mask & 0xEF) : (old_int_mask & 0xF7 )); enable();}/*-------------关闭串口--------------*/Close_Serial(int port_base){outp(port_base + SER_MCR, 0);outp(port_base + SER_IER, 0);outp(PIC_IMR, old_int_mask );if (port_base == COM_1){_dos_setvect(INT_SER_PORT_0, Old_Isr);printf("\nClosing Communications Channel Com Port #1.\n");}else{_dos_setvect(INT_SER_PORT_1, Old_Isr);printf("\nClosing Communications Channel Com Port #2.\n");}}/*-------------发送应用----------------*/void main(int argc,char *argv[]){char ch,press;int done=0;FILE *fp;argc=2;//argv[1]="c:\\comfile.c";if(argc<2){printf("\nUsage:display filename.wav!!!");// exit(1);}if((fp=fopen(argv[1],"r+b"))==NULL){printf("cannot open the file\n");// exit(0);}fseek(fp, 0, SEEK_SET);Open_Serial(COM_1,SER_BAUD_9600,SER_PARITY_EVEN| SER_BITS_8 | SER_STOP_1);printf("com:1;bps:9600;parity:even;bits:8;stop bit:1");printf("press any key to begin sending");getch();//Serial_Write(''); //该语句可用于发送单个字符while(!done&&ch != EOF) //发送文件开始{ch = fgetc(fp);//if(ch==EOF) Serial_Write(27);Serial_Write(ch);delay(30);if (kbhit()){press=getch();if (press==27){Serial_Write(27);done=1;}}}Close_Serial(COM_1);fclose(fp);}下面介绍最重要的MFC:CWnd:窗口,它是大多数“看得见的东西”的父类(Windows里几乎所有看得见的东西都是一个窗口,大窗口里有许多小窗口),比如视图CView、框架窗口CFrameWnd、工具条CToolBar、对话框CDialog、按钮CButton,etc;一个例外是菜单(CMenu)不是从窗口派生的。