msp430f149连接蓝牙模块发送数据,但是用串口调试精灵接收不到数据,程序如下,求大神指导~

msp430f149连接蓝牙模块发送数据,但是用串口调试精灵接收不到数据,程序如下,求大神指导~,第1张

//创建一个串口通讯

SerialPort CurrentPort = null

CurrentPort = new SerialPort()

CurrentPort.ReadBufferSize = 128

CurrentPort.PortName = comName //端口号

CurrentPort.BaudRate = bandRate//比特率

CurrentPort.Parity =parity//奇偶校验

CurrentPort.StopBits = stop//停止位

CurrentPort.DataBits = databit//数据

CurrentPort.ReadTimeout = 1000//读超时,即在1000内未读到数据就引起超时异常

//绑定数据接收事件,因为发送是被动的,所以你无法主动去获取别人发送的代码,只能通过这个事件来处理

CurrentPort.DataReceived += Sp_DataReceived

CurrentPort.Open()

定义一个变量 byte[] receiveStr

//绑定的事件处理函数

private static void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

{

SerialPort sp = sender as SerialPort

if (sp == null)

return

byte[] readBuffer = new byte[sp.ReadBufferSize]

sp.Read(readBuffer, 0, readBuffer.Length)

//赋值

receiveStr=readBuffer//当然你可以通过转换将byte[]转换为字符串。

}

//你要求的按钮事件可以这么写

private void button1_Click(object sender, EventArgs e)

{

if(receiveStr!=null)

{

变量 xxx=receiveStr

}

}

#include "msp430x14x.h"

#define uchar unsigned char

uchar DA

TA[]="hello world my name!"

void main()

{

// Stop watchdog timer to prevent time out reset

uchar i,j

WDTCTL = WDTPW + WDTHOLD

//

使用外部高频晶体振荡器

BCSCTL1&=~XT2OFF

do{

IFG1&=~OFIFG

for(j=0XFFj>0j--)

}while((IFG1&OFIFG))

BCSCTL2|=SELM_2

P3DIR|=BIT4+BIT7

P3SEL|=BIT4+BIT7

ME1 |= UTXE0 + URXE0

//

使能

USART0

收发

UCTL0 |= CHAR

// 8-bit

数据,一位停止位

UTCTL0 |= SSEL0

//

选择时钟,

UCLK = ACLK

32768

UBR00 = 0x03

// 32k/9600

UBR10 = 0x00

//

UMCTL0 = 0x4a

// Modulation

UCTL0 &= ~SWRST

//

初始化

UART0

状态机,

一般要设置好串口之后才复位

for(i=0i<=20i++)

{

TXBUF0=DA

TA[i]

while((UTCTL0&0X01)==0)//

等待数据发送完毕

}

while(1)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12112931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存