打开串口,和打开文件一样,在Linux下是这样,Windows下没试过,估计也差不多;
发送数据,即写串口,就跟写文件类似;
读取串口,用read就行。
具体的函数和配置参数可以参考一些别人的代码。
我用的新唐芯片,8051内核,跟51差不多,望采纳void UART_Initial (void)
{
P02_Quasi_Mode //Setting UART pin as Quasi mode for transmit
P16_Quasi_Mode //Setting UART pin as Quasi mode for transmit
SCON_1 = 0x50 //UART1 Mode1,REN_1=1,TI_1=1
T3CON = 0x08 //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1
clr_BRCK
RH3= HIBYTE(65536 - (1000000/u32Baudrate)-1) /*16 MHz */
RL3= LOBYTE(65536 - (1000000/u32Baudrate)-1) /*16 MHz */
set_TR3//Trigger Timer3
}
以上是初始化的
void Send_Data_To_UART1(UINT8 c)
{
TI_1 = 0
SBUF_1 = c
while(TI_1==0)
}
这个是发送
void UART_isr (void) interrupt 4 //串行中断服务程序
{
if (RI_1==1)
{ /* if reception occur */
clr_RI_1/* clear reception flag for next reception */
Receive_Date[c] = SBUF_1
if (Receive_Date[0] == First_Date)
{
c++
}
else if(Receive_Date_Size >0 &&Receive_Date_Size <4)
{
c++
}
else if(Receive_Date[c] == Last_Date &&Receive_Date_Size ==4)
{
c = 0
Flag_Receive_One = 1
}
else
{
c = 0
}
}
}
接收
#include <stdio.h>int main(void)
{
FILE *fp
if((fp=fopen("com3","r"))==NULL)
puts("this way doesn't work!/n")
else
puts("thiw way works!/n")
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)