C语言串口通信-源代码

合集下载

51单片机的串口通信程序(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单片机串口通信程序。

串口精灵源代码

串口精灵源代码
senddata.SetSize(len/2);
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语言串口通信-源代码

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语言实现串口通信

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#上位机串口通信助手源代码详解

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语言编程-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写成没有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语言源代码三菱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; }。

[电子工程] 单片机C语言之串口通信协议(代码分享)

[电子工程]  单片机C语言之串口通信协议(代码分享)

现实生活中,我们总是要与人打交道,互通有无。

单片机也一样,需要跟各种设备交互。

例如汽车的显示仪表需要知道汽车的转速及电动机的运行参数,那么显示仪表就需要从汽车的底层控制器取得数据。

而这个数据的获得过程就是一个通信过程。

类似的例子还有控制器通常是单片机或者PLC与变频器的通信。

通信的双方需要遵守一套既定的规则也称为协议,这就好比我们人之间的对话,需要在双方都遵守一套语言语法规则才有可能达成对话。

通信协议又分为硬件层协议和软件层协议。

硬件层协议主要规范了物理上的连线,传输电平信号及传输的秩序等硬件性质的内容。

常用的硬件协议有串口,IIC,SPI,RS485,CAN和USB。

软件层协议则更侧重上层应用的规范,比如modbus协议。

好了,那这里我们就着重介绍51单片机的串口通信协议,以下简称串口。

串口的6个特征如下。

(1)、物理上的连线至少3根,分别是Tx数据发送线,Rx数据接收线,GND共用地线。

(2)、0与1的约定。

RS232电平,约定﹣5V至﹣25V之间的电压信号为1,﹢5V至﹢25V之间的电压信号为0 。

TTL电平,约定5V的电压信号为1,0V电压信号为0 。

CMOS电平,约定3.3V的电压信号为1,0V电压信号为0 。

其中,CMOS电平一般用于ARM芯片中。

(3)、发送秩序。

低位先发。

(4)、波特率。

收发双方共同约定的一个数据位(0或1)在数据传输线上维持的时间。

也可理解为每秒可以传输的位数。

常用的波特率有300bit/s, 600bit/s, 2400bit/s, 4800bit/s, 9600bit/s。

(5)、通信的起始信号。

发送方在没有发送数据时,应该将Tx置1 。

当需发送时,先将Tx置0,并且保持1位的时间。

接受方不断地侦测Rx,如果发现Rx常时间变高后,突然被拉低(置为0),则视为发送方将要发送数据,迅速启动自己的定时器,从而保证了收发双方定时器同步定时。

(6)、停止信号。

发送方发送完最后一个有效位时,必须再将Tx保持1位的时间,即为停止位。

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

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

#include <reg52.h>#include<intrins.h>#include <stdio.h>#include <math.h>#define uchar unsigned char#define uint unsigned intsbit Key1 = P2^3;sbit Key2 = P2^2;sbit Key3 = P2^1;sbit Key4 = P2^0;sbit BELL = P3^6;sbit CONNECT = P3^7;unsigned int Key1_flag = 0;unsigned int Key2_flag = 0;unsigned int Key3_flag = 0;unsigned int Key4_flag = 0;unsigned char b;unsigned char code Num[21]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00, 0x10,0x89};unsigned char code Disdigit[4] = {0x7F,0xBF,0xDF,0xEF};unsigned char Disbuf[4];void delayms(uint t){uint i;while(t--){/* 对于11.0592M时钟,约延时1ms */for (i=0;i<125;i++){}}}//-----------------------------------------------------void SendData(uchar Dat){uchar i=0;SBUF = Dat;while (1){if(TI){TI=0;break;}}}void ScanKey(){if(Key1 == 0){delayms(100); if(Key1 == 0){Key1_flag = 1; Key2_flag = 0; Key3_flag = 0;Key4_flag = 0;Key1 = 1;}else;}if(Key2 == 0){delayms(100);if(Key2 == 0){Key2_flag = 1; Key1_flag = 0; Key3_flag = 0;Key4_flag = 0;Key2 = 1;}else;}if(Key3 == 0){delayms(50);if(Key3 == 0){Key3_flag = 1; Key1_flag = 0; Key2_flag = 0;Key4_flag = 0;Key3 = 1;}else;}if(Key4 == 0){delayms(50);if(Key4 == 0){Key4_flag = 1;Key1_flag = 0;Key2_flag = 0;Key3_flag = 0;Key4 = 1;}else;}else;}void KeyProc(){if(Key1_flag){TR1 = 1;SendData(0x55);Key1_flag = 0; }else if(Key2_flag){TR1 = 1;SendData(0x11); Key2_flag = 0;}else if(Key3_flag) {P1=0xff;BELL = 0;CONNECT = 1;Key3_flag = 0;}else if(Key4_flag){CONNECT = 0;BELL = 1;Key4_flag = 0;}else;}void Initdisplay(void){Disbuf[0] = 1;Disbuf[1] = 2;Disbuf[2] = 3;Disbuf[3] = 4;}void Display() //显示{unsigned int i = 0;unsigned int temp,count;temp = Disdigit[count]; P2 =temp;temp = Disbuf[count];temp = Num[temp];P0 =temp;count++;if (count==4)count=0;}void time0() interrupt 1 using 2 {Display();TH0 = (65535 - 2000)/256;TL0 = (65535 - 2000)%256;}void main(){Initdisplay();TMOD = 0x21;TH0 = (65535 - 2000)/256;TL0 = (65535 - 2000)%256;TR0 = 1;ET0 = 1;TH1 = 0xFD; //11.0592MTL1 = 0xFD;PCON&=0x80;TR1 = 1;ET1 = 1;SCON = 0x40; //串口方式REN = 1;PT1 = 0;PT0 = 1;EA = 1;while(1){ScanKey();KeyProc();if(RI){Disbuf[0] = 0;Disbuf[1] = 20;Disbuf[2] = SBUF>>4;Disbuf[3] = SBUF&0x0f;RI = 0;}else;}}51单片机串口通信C语言程序2**************************************************************; 平凡单片机工作室;ckss.asm;功能:反复向主机送AA和55两个数;主机使用一个串口调试软件设置19200,n,8,1***************************************************************/#include "reg51.h"#define uchar unsigned char#define uint unsigned int//延时程序//////////////////由Delay参数确定延迟时间*/void mDelay(unsigned int Delay){ unsigned int i;for(;Delay>0;Delay--){ for(i=0;i<124;i++){;}}}//////////////////// 主程序////////////////////void main(){ uchar OutDat; //定义输出变量TMOD=0x20; //TMOD=0TH1=0xf3; //12MHZ ,BPS:4800,N,8,1TL1=0xf3;PCON=0x80; //方式一TR1=1; //?????????????????????????????SCON=0x40; //串口通信控制寄存器模式一OutDat=0xaa; //向串口发送固定数据值for(;;) //循环程序{SBUF=OutDat;//发送数据for(;;){ if(TI) //发送中断位当发送停止位时置1,表示发送完成break;}mDelay(500);TI=0; //清零中断位OutDat=~OutDat; //显示内容按位取反}}。

c语言串口编程实例

c语言串口编程实例

c语言串口编程实例摘要:1.串口编程基础2.C 语言串口编程步骤3.C 语言串口编程实例4.实例详解5.总结正文:一、串口编程基础串口编程是指通过计算机串行接口进行数据通信的编程方式。

串口(Serial Port)是一种计算机硬件接口,可以通过串行通信传输数据。

与并行通信相比,串行通信只需一条数据线,传输速度较慢,但具有线路简单、成本低的优点。

因此,串口编程在电子设备、计算机外设、通信设备等领域有广泛的应用。

二、C 语言串口编程步骤1.包含头文件:在使用C 语言进行串口编程时,首先需要包含头文件`<reg52.h>`或`<intrins.h>`。

2.配置串口:配置串口包括设置波特率、数据位、停止位、奇偶校验等参数。

3.初始化串口:初始化串口主要是初始化串口硬件,如配置UART(通用异步收发器)等。

4.打开串口:打开串口是指使能串口通信功能,以便数据传输。

5.读写串口:通过`in`和`out`语句实现数据的输入输出。

6.关闭串口:在数据传输完成后,需要关闭串口以节省资源。

7.串口通信:通过循环寄存器、缓存寄存器或FIFO(先进先出)等方法实现数据的收发。

三、C 语言串口编程实例以下是一个简单的C 语言串口编程实例,该实例通过串口发送数据“Hello, World!”:```c#include <reg52.h>#include <intrins.h>sbit UART_TXD = P3^1; // 配置UART TXD 引脚void init_uart(); // 初始化UART 函数void send_data(unsigned char dat); // 发送数据函数void main(){init_uart(); // 初始化UARTsend_data("H"); // 发送字符"H"send_data("e"); // 发送字符"e"send_data("l"); // 发送字符"l"send_data("l"); // 发送字符"o"send_data(" "); // 发送空格send_data("W"); // 发送字符"W"send_data("o"); // 发送字符"r"send_data("r"); // 发送字符"l"send_data("d"); // 发送字符"d"while(1); // 循环等待}void init_uart() // 初始化UART 函数{TMOD = 0x20; // 设置定时器1 为工作状态TH1 = 0xfd; // 设置定时器1 的计数值TL1 = 0xfd; // 设置定时器1 的计数值TR1 = 1; // 使能定时器1SCON = 0x40; // 设置串口工作状态ES = 0; // 开总中断EA = 1; // 开总中断允许}void send_data(unsigned char dat) // 发送数据函数{SBUF = dat; // 将数据存入缓存寄存器while(!TI); // 等待发送缓存清空TI = 0; // 清空发送缓存}```四、实例详解1.配置串口:通过设置UART TXD 引脚为P3.1,确定波特率、数据位、停止位和奇偶校验等参数。

串口通信源代码

串口通信源代码
// txtest.cpp : 定义控制台应用程序的入口点。
//
#include "tdafx.h"
#include <iostream>
// #include <cv.h>
// #include <highgui.h>
/*#include <cstring>*/
#include<windows.h>
//myDCB.fBinary = TRUE;
//myDCB.fParity = FALSE;
myDCB.ByteSize = 8;
myDCB.Parity = NOPARITY;
myDCB.StopBits = ONESTOPBIT;
SetCommState(hCom, &myDCB);
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(hCom,&TimeOuts); //设置超时
DCB myDCB;
GetCommState(hCom, &myDCB);
myDCB.BaudRate = 9600;
ReceiveData(hCom, rdata);
cout << (BYTE)(rdata[0]) <<"%%%%%%%"<< (BYTE)(rdata[1]) << endl;
cout << "收到数据!" << endl;
if(i>=250 ) break;

C语言串口通信-源代码

C语言串口通信-源代码

#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++; //记录接收了多少个数据。

C语言 做串口通讯程序

C语言    做串口通讯程序

一、引言:现在在工业现场很少有人再用C语言做串口通讯程序了,但是基于DOS环境的程序还是有它的优势的。

DOS系统的单任务环境是系统运行更加稳定、可靠;在一些追求很高的可靠性的系统中还是有一定的价值的。

本文通过C语言控制PLC实现简单的物料传送为例子。

二、硬件介绍:1、CPM1A采用RS232串口通讯与上位机连接,在PLC的DM区中可以设定串口参数,本文采用默认值:串口通信格式:1位---起始位、9600---波特率、7位---数据位、2位---停止位、偶校验2、C语言中用于串口读写的函数:bioscom,在bios.h头文件中。

Bioscom用法:bioscom(int cmd,char byte,int port)Cmd的值:0 设置通信参数为btye值1 发送一个字符到串口2 从串口接收一个字符3 返回串口端口的状态byte的值:0x02 7数据位0x03 8位数据位0x00 1个停止位0x04 2个停止位0x00 无奇偶校验0x08奇校验0x18偶校验0x80 1200波特率0xA0 2400波特率0xC0 4800波特率0xE0 9600波特率注意:在对串口初始化时,上述参数值相或附给byte。

Port的值:0 端口11 端口2三、完整源代码:#include /* 此头函数请不要删除*/#include#include#define F1 0x3B /*启动*/#define F2 0x3C /*停止*/#define F3 0x3D /*混料*/#define F4 0x3E /*出料*/#define F5 0x3F /*退出*/#define PORT 0 /*定义端口号*/#define SETTINGS (0x02|0x04|0x18|0xE0) /*设定参数*//* 定义发送字符函数send */void sendPort(int port,char cc){union{char ch[2];int status;}port_status;/*发送一个字符*/port_status.status=bioscom(1,cc,port);printf("%c",cc); /*判断发送是否正确*/if(port_status.ch[1]&128){printf("Send data error detected in serial port"); printf("\r");}}/* Check-Status 检查端口*/int check_status(int port){int status;status=bioscom(3,0,port);if(status & 0x100)return 0;else return 1;}/*发送命令函数*/void SendOder(char od[]){int i=0;for(i=0;i<=16;i++)sendPort(PORT,od[i]);}/*应用程序主体*/main(){int key=1,i,m=1;int check=1;char a[20]="@00WR0000000144*"; /*启动00*/ char b[20]="@00WR0000000441*"; /*混料02*/ char c[20]="@00WR000000084D*"; /*出料03*/ char d[20]="@00WR0000000247*"; /*停止01*/ a[16]='\r';b[16]='\r';c[16]='\r';d[16]='\r';printf("启动:F1 \t混料:F2 \t出料:F3");printf(" \t停止:F4 \t退出:F5\n");bioscom(0,SETTINGS,PORT); /*初始化串口*/ while(check!=0) /*检查端口状态*/{m+=1;check=check_status(PORT);if(m>=1000)break;}while (1){while (bioskey(1)==0);key=bioskey(0);key=key&0xFF?0:key>>8;if(key==F1) /*启动命令*/{for(i=0;i<=16;i++){sendPort(PORT,a[i]);m=1;while(check!=0){m+=1;check=check_status(PORT); /*检查端口状态*/ if(m>=3000)break;}}}else if(key==F4) /*停止命令*/{for(i=0;i<=16;i++){sendPort(PORT,d[i]);m=1;while(check!=0){m+=1;check=check_status(PORT);if(m>=3000)break;}}}else if(key==F2) /*混料命令*/{for(i=0;i<=16;i++){sendPort(PORT,b[i]);m=1;while(check!=0){m+=1;check=check_status(PORT);if(m>=3000)break;}}}else if(key==F3) /*出料命令*/{for(i=0;i<=16;i++){sendPort(PORT,c[i]);m=1;while(check!=0){m+=1;check=check_status(PORT);if(m>=3000)break;}}}else if(key==F5)exit(1); /*退出命令*/}getch(); /* 此语句请不要删除*/}四、结束语RS232串口通讯在小型的控制系统同中还是应用广泛的,本文采用C语言控制,主要是给用户提供一个新的控制方式做选择。

VC实现串口通信项目源码

VC实现串口通信项目源码

VC实现串口通信项目源码以下是一个简单的串口通信项目的VC实现源码,包括了串口初始化、发送数据、接收数据等基本功能。

```#include <Windows.h>#include <stdio.h>#define BUFFER_SIZE 1024HANDLE hSerial;//初始化串口参数BOOL InitSerialPort//打开串口hSerial = CreateFile("\\\\.\\COM1", GENERIC_READ ,GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hSerial == INVALID_HANDLE_VALUE)printf("Failed to open serial port\n");return FALSE;}//配置串口参数DCB dcbSerialParams = { 0 };dcbSerialParams.DCBlength = sizeof(dcbSerialParams); printf("Failed to get serial port state\n"); CloseHandle(hSerial);return FALSE;}dcbSerialParams.BaudRate = CBR_9600; // 波特率为9600 dcbSerialParams.ByteSize = 8; // 8位数据位dcbSerialParams.Parity = NOPARITY; // 无奇偶校验dcbSerialParams.StopBits = ONESTOPBIT;// 1位停止位printf("Failed to set serial port state\n"); CloseHandle(hSerial);return FALSE;}//设置超时操作CloseHandle(hSerial);return FALSE;}return TRUE;//发送数据BOOL SendData(const char* data)DWORD bytesWritten;if (!WriteFile(hSerial, data, strlen(data), &bytesWritten, NULL))printf("Failed to send data\n");CloseHandle(hSerial);return FALSE;}return TRUE;//接收数据BOOL ReceiveData(char* buffer, DWORD size)DWORD bytesRead;if (!ReadFile(hSerial, buffer, size, &bytesRead, NULL))printf("Failed to receive data\n");CloseHandle(hSerial);return FALSE;}return TRUE;if (!InitSerialPort()return 1;}char sendBuffer[BUFFER_SIZE];char receiveBuffer[BUFFER_SIZE];//发送数据printf("Enter data to send: ");gets_s(sendBuffer, BUFFER_SIZE);if (!SendData(sendBuffer))return 1;}//接收数据printf("Receiving data...\n");if (!ReceiveData(receiveBuffer, BUFFER_SIZE)) return 1;}printf("Received data: %s\n", receiveBuffer); CloseHandle(hSerial);```这个项目使用了Windows的串口通信API函数来实现串口的初始化、发送数据和接收数据操作。

c语言怎么写串口通信编程

c语言怎么写串口通信编程

c语言怎么写串口通信编程串口通信是一种广泛应用于嵌入式系统和电子设备之间的通信方式。

无论是嵌入式开发还是电子设备控制,串口通信都是常见的需求。

在C语言中,实现串口通信需要通过操作串口的硬件寄存器和使用相应的通信协议来实现数据的发送和接收。

本文将一步一步介绍如何使用C语言编写串口通信程序。

第一步:打开串口要开始串口通信,首先需要打开串口。

在C语言中,可以使用文件操作函数来打开串口设备。

通常,串口设备被命名为/dev/ttyS0,/dev/ttyS1等,具体名称取决于系统。

下面是一个打开串口设备的示例代码:cinclude <stdio.h>include <fcntl.h>include <termios.h>int open_serial_port(const char *port) {int fd = open(port, O_RDWR O_NOCTTYO_NDELAY);if (fd == -1) {perror("open_serial_port");return -1;}设置串口属性struct termios options;tcgetattr(fd, &options);cfmakeraw(&options);cfsetspeed(&options, B9600);tcsetattr(fd, TCSANOW, &options);return fd;}int main() {const char *port = "/dev/ttyS0";int fd = open_serial_port(port);if (fd == -1) {打开串口失败,处理错误return -1;}串口已打开,可以进行数据的读写操作return 0;}在上面的代码中,open_serial_port函数用于打开指定的串口设备并进行一些必要的设置。

C语言串口通信源代码

C语言串口通信源代码

C语言串口通信源代码#include#include#include#include#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)< bdsfid="112" p=""></maxbuflen)<>Buffer[CircIn]=inp(comportaddr);if(CircIn<="" p="">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++;< bdsfid="135" p=""></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#include#include#include#include#include #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++; //记录接收了多少个数据。

C51单片机串口通讯通用模块代码(可用于操作系统串口通讯)

C51单片机串口通讯通用模块代码(可用于操作系统串口通讯)

C51单片机串口通讯通用模块代码(可用于操作系统串口通讯)#include#include "UART1.h"#include "commdriver.h"//当前适用于C51//可以根据具体CPU型号,修改宏定义和串口初始化代码就可以#define CPU_XTAL 22118400 //CPU频率#define FUNCTION_NULL 0 //没有定义函数#define COM_TI TI //发送中断寄存器#define COM_TI0() {TI = 0;} //发送中断寄存器清零#define COM_TI1() {TI = 1;} //发送中断寄存器置一( 强行置一触发发送中断)#define COM_TI_SBUF(dat) {SBUF = dat;} //发送数据到硬件缓冲区#define COM_TI_End() {return;} //发送结束处理函数#define COM_RI RI //接收中断寄存器#define COM_RI0() {RI = 0;} //接收中断寄存器清零#define COM_RI1() {RI = 1;} //接收中断寄存器置一#define COM_RI_SBUF(dat) {dat = SBUF;} //提取接收硬件缓冲区数据#define COM_TI_FLAG COM_TI //发送中断标志寄存器(非中断发送方式使用)#define COM_TI_FLAG0() COM_TI0() //发送中断标志寄存器清零(非中断发送方式使用)void (*COM1RevEvent)(unsigned char dat); //数据接收事件#define LenTxBuf 30 //发送缓冲区长度#define LenRxBuf 1 //接收缓冲区长度volatile unsigned char TxBuf1[LenTxBuf],RxBuf1[LenRxBuf]; //收发缓冲区实体volatile unsigned char *inTxBuf1,*outTxBuf1,*inRxBuf1,*outRxBuf1; //收发缓冲区读写指针volatile unsigned char TIflag1=1;//Note:It must be 1. //发送缓冲区为空标志//*********************#define _TxBuf TxBuf1#define _RxBuf RxBuf1#define _inTxBuf inTxBuf1#define _outTxBuf outTxBuf1#define _inRxBuf inRxBuf1#define _outRxBuf outRxBuf1#define _TIflag TIflag1/*************函数声明****************/#define _COMRevEvent COM1RevEvent //串口接收事件#define _USART_IRQ void USART1_IRQHandler(void) interrupt 4 //串口中断服务程序#define _COM_Buffer_Init void COM1_Buffer_Init(void) //串口缓冲区初始化#define _COM_Open void COM1_Open(unsigned int baudrate,void (*revevent)(unsigned char dat)) // 串口初始化#define _COM_Close void COM1_Close(void) //关闭串口#define _COM_GetOneByte unsigned charCOM1_GetOneByte(unsigned char *ch) //获取一个字节#define _COM_GetPChar unsigned char COM1_GetPChar(unsigned char *ch,unsigned char len) //获取指定长度字节数组#define _COM_RxByte unsigned char COM1_RxByte(void) //获取接收字节个数//********缓冲区中断方式发送(安全性高)#define _COM_SendOneByte unsigned char COM1_SendOneByte(unsigned char one_byte) // 发送一个字节#define _COM_SendPChar void COM1_SendPChar(unsigned char *P,unsigned char Len) //发送定长字节数组#define _COM_SendString void COM1_SendString(unsigned char *P) //发送字符串//********非缓冲区中断方式发送#define _COM_PrintOneByte void COM1_PrintOneByte(unsigned char c) // 发送一个字节#define _COM_PrintPChar void COM1_PrintPChar(unsigned char *buf,unsigned int len) //发送定长字节数组#define _COM_PrintString void COM1_PrintString(unsigned char *P) //发送字符串//*************内部引用模型函数(外部不关心,移植时修改后面函数名,要与上面对应的外部声明的函数名一致)*******************#define COM_GetOneByte_(ptr) COM1_GetOneByte(ptr) //与上面获取一个字节的函数对应#define COM_SendOneByte_(dat) COM1_SendOneByte(dat) //与上面发送一个字节的函数对应#define COM_PrintOneByte_(dat) COM1_PrintOneByte(dat) //与上面发送一个字节的函数对应//*********************函数模型定义区(不需要修改)*****************/****************************函数模型:void USART_IRQHandler(void)函数功能:串口中断服务程序入口参数:无返回值:无修改者:修改时间:修改内容简要:***************************/_USART_IRQ //串口1中断{volatile unsigned char *t;if(COM_RI){COM_RI0(); //清接收标记if(_COMRevEvent != FUNCTION_NULL) //自定义接收事件{COM_RI_SBUF(*_inRxBuf);_COMRevEvent(*_inRxBuf);}else{t = _inRxBuf;t++;if(t == (_RxBuf + LenRxBuf)) t = _RxBuf;if(t != _outRxBuf) //RxBuf No Full{COM_RI_SBUF(*_inRxBuf);_inRxBuf = t;}}}if(COM_TI){COM_TI0();if(_inTxBuf == _outTxBuf) {_TIflag = 1;COM_TI_End();};//TxBuf1 EmptyCOM_TI_SBUF(*_outTxBuf); _outTxBuf++;if(_outTxBuf == (_TxBuf+LenTxBuf)) _outTxBuf = _TxBuf;}}/****************************函数模型:void COM1_Buffer_Init(void)函数功能:串口1缓冲区初始化入口参数:无返回值:无修改者:修改时间:修改内容简要:***************************/_COM_Buffer_Init{_inTxBuf = _TxBuf;_outTxBuf = _TxBuf;_inRxBuf = _RxBuf;_outRxBuf = _RxBuf;}/****************************函数模型:void COM_Open(unsigned int baudrate,void (*revevent)(unsigned char dat))函数功能:系统串口初始化入口参数: unsigned int baudrate:串口波特率返回值:无修改者:修改时间:修改内容简要:***************************/_COM_Open{//定时器1做波特率发生器TI = 0; /* clear transmit interrupt */TR1 = 0; /* stop timer 1 */ET1 = 0; /* disable timer 1 interrupt */PCON |= 0x80; /* 0x80=SMOD: set serial baudrate doubler */ TMOD &= ~0xF0; /* clear timer 1 mode bits */TMOD |= 0x20; /* put timer 1 into MODE 2 */TH1 = (unsigned char) (256 - (CPU_XTAL / (16L * 12L * baudrate)));TR1 = 1; /* start timer 1 *///设置串口1模式SM0 = 0; SM1 = 1; /* serial port MODE 1 */SM2 = 0;REN = 1; /* enable serial receiver */RI = 0; /* clear receiver interrupt */TI = 0; /* clear transmit interrupt */ES = 1; /* enable serial interrupts */PS = 1; /* set serial interrupts to low priority */_COMRevEvent = revevent;}/****************************函数模型:void COM1_Close(void)函数功能:关闭系统串口入口参数:无返回值:无修改者:修改时间:修改内容简要:***************************/_COM_Close{}/****************************函数模型:unsigned char COM1_GetOneByte(unsigned char *ch)函数功能:获取一个字节入口参数:unsigned char *ch:接收字节指针返回值: unsigned char *ch:接收字节指针unsigned char :获取状态;0:成功,1:失败(缓冲区为空)修改者:修改时间:修改内容简要:***************************/_COM_GetOneByte{if(_inRxBuf == _outRxBuf) {return 0;}; //RxBuf Empty*ch = *_outRxBuf; _outRxBuf++;if(_outRxBuf==_RxBuf+LenRxBuf) _outRxBuf = _RxBuf;return 1;}/****************************函数模型:unsigned char COM_GetPChar(unsigned char *ch,unsigned char len)函数功能:获取指定长度字节数组入口参数: unsigned char *ch:接收字节数组指针unsigned char len:接收字节个数返回值: unsigned char *ch:接收字节数组指针unsigned char:实际接收字节个数修改者:修改时间:修改内容简要:***************************/_COM_GetPChar{unsigned char i = 0;do{len--;if(!COM_GetOneByte_(ch)) break;i++;ch++;}while(len);return i;}/****************************函数模型:unsigned char COM_RxByte(void)函数功能:获取接收缓冲区有效字节个数入口参数:无返回值: unsigned char:接收缓冲区有效字节个数修改者:修改时间:修改内容简要:***************************/_COM_RxByte{if(_inRxBuf>=_outRxBuf) return (_inRxBuf-_outRxBuf);else return LenRxBuf-(_outRxBuf-_inRxBuf);}/****************************函数模型:unsigned char COM_SendOneByte(unsigned char one_byte)函数功能:发送一个字节入口参数: unsigned char one_byte:发送的字节返回值:unsigned char:发送状态,0:成功,1:失败(缓冲区满)修改者:修改时间:修改内容简要:***************************/_COM_SendOneByte{volatile unsigned char *t;if(_TIflag){_TIflag = 0;COM_TI1();}t = _inTxBuf;t++;if(t == _TxBuf + LenTxBuf) t = _TxBuf;if(t == _outTxBuf) {return 1;};//TxBuf Full*_inTxBuf = one_byte;_inTxBuf = t;return 0;}/****************************函数模型:void COM_SendPChar(unsigned char *P,unsigned char Len)函数功能:发送定长字节数组入口参数: unsigned char *P:字节数组指针unsigned char Len:发送长度返回值:无修改者:修改时间:修改内容简要:***************************/_COM_SendPChar{while(Len){//while(COM1_SendOneByte(*P)); //发送失败,继续发送,知道发送成功COM_SendOneByte_(*P);P++;Len--;}}/****************************函数模型:void COM_SendString(unsigned char *P)函数功能:发送字符串入口参数: unsigned char *P:字符串指针返回值:无修改者:修改时间:修改内容简要:***************************/_COM_SendString{while(*P){COM_SendOneByte_(*P);P++;}}//**************** 非缓冲区中断方式发送/****************************函数模型:void COM_PrintOneByte(unsigned char c)函数功能:发送一个字节入口参数: unsigned char one_byte:发送的字节返回值:无修改者:修改时间:修改内容简要:***************************/_COM_PrintOneByte{COM_TI_FLAG0();COM_TI_SBUF(c); //发送数据while(!COM_TI_FLAG) ; //等待发送结束}/****************************函数模型:void COM_PrintPChar(unsigned char *buf,unsigned int len)函数功能:发送定长字节数组入口参数: unsigned char *P:字节数组指针unsigned char Len:发送长度返回值:无修改者:修改时间:修改内容简要:***************************/_COM_PrintPChar{while(len){COM_PrintOneByte_(*buf);buf ++;len--;}}/****************************函数模型:void COM_PrintString(unsigned char *P) 函数功能:发送字符串入口参数: unsigned char *P:字符串指针返回值:无修改者:修改时间:修改内容简要:***************************/_COM_PrintString{while(*P){COM_PrintOneByte_(*P);P++;}}。

c语言通信问题的代码

c语言通信问题的代码

c语言通信问题的代码以下是一个简单的C语言通信问题的示例代码: c.#include <stdio.h>。

#include <stdlib.h>。

#include <string.h>。

#define MAX_MESSAGE_SIZE 100。

// 定义一个结构体来表示消息。

typedef struct {。

int sender_id;int receiver_id;char message[MAX_MESSAGE_SIZE];} Message;// 模拟发送消息的函数。

void sendMessage(Message msg) {。

printf("发送者ID,%d\n", msg->sender_id); printf("接收者ID,%d\n", msg->receiver_id); printf("消息内容,%s\n", msg->message);printf("消息发送成功!\n");}。

// 模拟接收消息的函数。

void receiveMessage(Message msg) {。

printf("接收者ID,%d\n", msg->receiver_id);printf("发送者ID,%d\n", msg->sender_id);printf("消息内容,%s\n", msg->message);printf("消息接收成功!\n");}。

int main() {。

Message msg1;msg1.sender_id = 1;msg1.receiver_id = 2;strcpy(msg1.message, "你好,这是一条测试消息!"); Message msg2;msg2.sender_id = 2;msg2.receiver_id = 1;strcpy(msg2.message, "你好,我收到了你的消息!"); sendMessage(&msg1);receiveMessage(&msg2);return 0;}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 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)不是从窗口派生的。

相关文档
最新文档