串口通信协议源代码
篇一:串口通信源代码
#include
#define uchar unsigned char
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; uchar a,b,flag;.//定义标志位
void main()
{
TMOD=0x20; //方式控制字
SCON=0x50; //
TH1=0xfd; //设置波特率为9600
TL1=0xfd;
TR1=1;//开定时器1
// REN=1; //
EA=1; //总中断允许
ES=1; //串口中断允许
while(1)
{ P0=table[a-0x30];
if(flag==1)
{
ES=0;
flag=0;
SBUF=a;
while(!TI);
TI=0;
ES=1;
}
}
}
void serial() interrupt 4
{
a=SBUF;
P1=0xf7;
flag=1;
RI=0;
}
篇二:VC实现串口通信项目源码
VC 实现串口通信例程WIN95 界面下的 VC++串口通讯程序在 WIN32 下是不建议对端口进行操作的,在 WIN32 中所有的设备都被看成是文件, 串行口也不例外也是作为文件来进行处理的。 这是我的一份 关于串口编程的读书笔记,对于使用 VC 进行编程的同行应该有一定的帮助。 1.打开串口: 在 Window 95 下串行口作为文件处理,使用文件操作对串行口进行处理。使用 CreateFile()打开串口,CreateFile()将返回串口的句柄。 HANDLE CreateFile( LPCTSTR lpFileName, // pointer to name of the file DWORD dwDesiredAccess, // access (read-write) mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes DWORD dwCreationDistribution, // how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to file with attributes to copy ); lpFileName: 指明串口制备,例:COM1,COM2 dwDesiredAccess: 指明串口存取方式,例:GENERIC_READ|GENERIC_WRITE dwShareMode: 指明串口共享方式 lpSecurityAttributes: 指明串口的安全属性结构,NULL 为缺省安全属性 dwCreateionDistribution: 必须为 OPEN_EXISTIN dwFlagAndAttributes: 对串口唯一有意义的是 FILE_FLAG_OVERLAPPED hTemplateFile: 必须为 NULL 2.关闭串口: CloseHandle(hCommDev); 3.设置缓冲区长度: BOOL SetupComm( HANDLE hFile, DWORD dwInQueue, DWORD dwOutQueue ); MPROP 结构: 可使用 GetCommProperties() 取得 COMMPROP 结构,COMMPROP 结构中记载了系// handle of communications device // size of input buffer // size of output buffer统支持的各项设置。 typedef struct _COMMPROP { WORD wPacketLength; WORD wPacketVersion; DWORD dwServiceMask; DWORD dwReserved1; DWORD dwMaxTxQueue; DWORD dwMaxRxQueue; DWORD dwMaxBaud; DWORD dwProvSubType; DWORD dwProvCapabilities; DWORD dwSettableParams; DWORD dwSettableBaud; WORD wSettableData; WORD