【请教高手】如何用VC向单片机串行传输数据啊,急,分不够再加 小弟第一次接触VC与单片机的问题啊,如题,请各位大侠帮忙啊,急啊~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以前有兄弟贴过哦!http://www.gjwtech.com.共同进步~~ 多谢一楼,看来那些文章后现在心里总算有点头绪了,但是,我还有一点不确定的就是:是不是VC只是负责与PC机的串口通讯,至于具体如何写入单片机是由什么控制的呢?单片机与PC机串口连接后,PC机串口的数据就会自动写入单片机么? 你用API不就可以了,CREATEFILE,READFILE,WRITEFILE什么的,98/NT/2000都可以给你个例子extern "C" bool PASCAL Link(){ m_hCardInterFace = CreateFile("COM1", // 建立和COM1通讯连接 GENERIC_WRITE, NULL,NULL, OPEN_EXISTING, NULL, NULL); if(m_hCardInterFace!=INVALID_HANDLE_VALUE) { m_CommTimeouts.ReadIntervalTimeout = 1000;//读操作两个字节之间的时间(单位:毫秒) m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;//读操作的时间(单位:毫秒) m_CommTimeouts.ReadTotalTimeoutConstant = 1000; m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000; m_CommTimeouts.WriteTotalTimeoutConstant = 1000; SetCommTimeouts(m_hCardInterFace, &m_CommTimeouts); return true; } return false;}extern "C" void PASCAL Unlink() //断开打印机{ CloseHandle(m_hCardInterFace);}bool SendCommand(CString CommandWord) //向打印机发出命令,并且返回命令状态{ DWORD t1,dwRead; COMSTAT t2; CString Command=0x1b; Command+=CommandWord; Command+=0x0d; WriteFile( m_hCardInterFace, Command.GetBuffer (Command.GetLength ()), Command.GetLength (), &dwRead, NULL); if(!ClearCommError(m_hCardInterFace,&t1,&t2)) { switch(t1) { case CE_OOP: TRACE("G CE_OOP\n"); return false; break; case CE_PTO://超时错误 TRACE("G CE_PTO\n"); return false; break; case CE_IOE: TRACE("G CE_IOE\n"); return false; break; } } return true;} 串行通讯 用串行控件 能简单点 单片机?什么系列的 ? 要通过电平转换的 MAX232 关于vc方面的http://www.gjwtech.com这个网站应该很详细了!单片机方面就象上面的兄弟说得那样,要看单片机的代码怎么写的,也就是和vc的协议问题!硬件方面肯定要电平转换的,那个应该很简单的! //这是一个单片机C51串口接收(中断)和发送例程,可以用来测试51单片机的中断接收 //和查询发送,另外我觉得发送没有必要用中断,因为程序的开销是一样的 //程序编写: 龚建伟 [email protected] //技术主页:http://www.gjwtech.com //您有这方面的问题可以和我讨论 #include <reg51.h>#include <string.h>#define INBUF_LEN 4 //数据长度unsigned char inbuf1[INBUF_LEN];unsigned char checksum,count3;bit read_flag=0;void init_serialcomm(void){ SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload PCON |= 0x80; //SMOD=1; TH1 = 0xF4; //Baud:4800 fosc=11.0592MHz IE |= 0x90; //Enable Serial Interrupt TR1 = 1; // timer 1 run // TI=1; }//向串口发送一个字符 void send_char_com(unsigned char ch) { SBUF=ch; while(TI==0); TI=0;}//向串口发送一个字符串,strlen为该字符串长度 void send_string_com(unsigned char *str,unsigned int strlen){ unsigned int k=0; do { send_char_com(*(str + k)); k++; } while(k < strlen);}//串口接收中断函数 void serial () interrupt 4 using 3 { if(RI) { unsigned char ch; RI = 0; ch=SBUF; if(ch>127) { count3=0; inbuf1[count3]=ch; checksum= ch-128; } else { count3++; inbuf1[count3]=ch; checksum ^= ch; if( (count3==(INBUF_LEN-1)) && (!checksum) ) { read_flag=1; //如果串口接收的数据达到INBUF_LEN个,且校验没错, //就置位取数标志 } } }}main(){ init_serialcomm(); //初始化串口 while(1) { if(read_flag) //如果取数标志已置位,就将读到的数从串口发出 { read_flag=0; //取数标志清0 send_string_com(inbuf1,INBUF_LEN); } }} COM中的类型库是什么? StretchBlt函数操作位图的问题 对bitMapInfo.bmiColors[j]赋值进入死循环。 问个函数:如何根据窗口句柄得到窗口的指针?? 请教:在DLL中调用ADO操作数据库连接关闭出错。 mfc bug 关于A2W的转换问题 哪里有免费vc下载 有关动态连接库以及函数指针的奇怪现象 各位高手帮帮忙!!!!!!! ------急--------急---------急--------- VC怎么做ToolTip? control + c 把文本复制到剪贴板,取回的问题
http://www.gjwtech.com.
共同进步~~
给你个例子
extern "C" bool PASCAL Link()
{
m_hCardInterFace =
CreateFile("COM1", // 建立和COM1通讯连接
GENERIC_WRITE,
NULL,NULL,
OPEN_EXISTING,
NULL,
NULL);
if(m_hCardInterFace!=INVALID_HANDLE_VALUE)
{
m_CommTimeouts.ReadIntervalTimeout = 1000;//读操作两个字节之间的时间(单位:毫秒)
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;//读操作的时间(单位:毫秒)
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(m_hCardInterFace, &m_CommTimeouts);
return true;
}
return false;
}
extern "C" void PASCAL Unlink() //断开打印机
{
CloseHandle(m_hCardInterFace);
}
bool SendCommand(CString CommandWord) //向打印机发出命令,并且返回命令状态
{
DWORD t1,dwRead;
COMSTAT t2; CString Command=0x1b;
Command+=CommandWord;
Command+=0x0d;
WriteFile(
m_hCardInterFace,
Command.GetBuffer (Command.GetLength ()),
Command.GetLength (),
&dwRead,
NULL);
if(!ClearCommError(m_hCardInterFace,&t1,&t2))
{
switch(t1)
{
case CE_OOP:
TRACE("G CE_OOP\n");
return false;
break;
case CE_PTO://超时错误
TRACE("G CE_PTO\n");
return false;
break;
case CE_IOE:
TRACE("G CE_IOE\n");
return false;
break;
}
}
return true;
}
//和查询发送,另外我觉得发送没有必要用中断,因为程序的开销是一样的
//程序编写: 龚建伟 [email protected]
//技术主页:http://www.gjwtech.com
//您有这方面的问题可以和我讨论
#include <reg51.h>
#include <string.h>#define INBUF_LEN 4 //数据长度unsigned char inbuf1[INBUF_LEN];
unsigned char checksum,count3;
bit read_flag=0;void init_serialcomm(void)
{
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; //SMOD=1;
TH1 = 0xF4; //Baud:4800 fosc=11.0592MHz
IE |= 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
// TI=1;
}//向串口发送一个字符
void send_char_com(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}//向串口发送一个字符串,strlen为该字符串长度
void send_string_com(unsigned char *str,unsigned int strlen)
{
unsigned int k=0;
do
{
send_char_com(*(str + k));
k++;
} while(k < strlen);
}
//串口接收中断函数
void serial () interrupt 4 using 3
{
if(RI)
{
unsigned char ch;
RI = 0;
ch=SBUF;
if(ch>127)
{
count3=0;
inbuf1[count3]=ch;
checksum= ch-128;
}
else
{
count3++;
inbuf1[count3]=ch;
checksum ^= ch;
if( (count3==(INBUF_LEN-1)) && (!checksum) )
{
read_flag=1; //如果串口接收的数据达到INBUF_LEN个,且校验没错,
//就置位取数标志
}
}
}
}
main()
{
init_serialcomm(); //初始化串口
while(1)
{
if(read_flag) //如果取数标志已置位,就将读到的数从串口发出
{
read_flag=0; //取数标志清0
send_string_com(inbuf1,INBUF_LEN);
}
}}