gpt4 book ai didi

c - MicroC rs-485、pic16f887 字符串发送问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:50:02 24 4
gpt4 key购买 nike

如何将字符串“MY STRING”从主图片发送到从图片?

我正在使用 MicroC RS-485 库示例:

http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/rs-485_library.htm

我试图将字符串从主机发送到从机:并设置 dat[7] = "my string";期望 dat[7] 与我的字符串一起出现在从站上,但我得到的是空值...

链接底部的原始代码。

硕士

char dat[10];                          // buffer for receving/sending messages
char i,j;

sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction

// Interrupt routine
void interrupt() {
RS485Master_Receive(dat);
}

void main(){
long cnt = 0;

ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;

PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISD = 0;


UART1_Init(9600); // initialize UART1 module
Delay_ms(100);

RS485Master_Init(); // initialize MCU as Master
dat[0] = 0xAA;
dat[1] = 0xF0;
dat[2] = 0x0F;
dat[4] = 0; // ensure that message received flag is 0
dat[5] = 0; // ensure that error flag is 0
dat[6] = 0;
dat[7] = "MY STRING";

RS485Master_Send(dat,1,160);


RCIE_bit = 1; // enable interrupt on UART1 receive
TXIE_bit = 0; // disable interrupt on UART1 transmit
PEIE_bit = 1; // enable peripheral interrupts
GIE_bit = 1; // enable all interrupts

while (1){
// upon completed valid message receiving
// data[4] is set to 255
cnt++;
if (dat[5]) { // if an error detected, signal it
PORTD = 0xAA; // by setting portd to 0xAA
}
if (dat[4]) { // if message received successfully
cnt = 0;
dat[4] = 0; // clear message received flag
j = dat[3];
for (i = 1; i <= dat[3]; i++) { // show data on PORTB
PORTB = dat[i-1];
} // increment received dat[0]
dat[0] = dat[0]+1; // send back to master
Delay_ms(1);
RS485Master_Send(dat,1,160);

}
if (cnt > 100000) {
PORTD ++;
cnt = 0;
RS485Master_Send(dat,1,160);
if (PORTD > 10) // if sending failed 10 times
RS485Master_Send(dat,1,50); // send message on broadcast address
}
}

}

奴隶:

char dat[9];             // buffer for receving/sending messages
char i,j;

sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction

// Interrupt routine
void interrupt() {
RS485Slave_Receive(dat);
}

void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;

PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISD = 0;
PORTA = 0;
TRISA = 0;


UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize MCU as slave, address 160

dat[4] = 0; // ensure that message received flag is 0
dat[5] = 0; // ensure that message received flag is 0
dat[6] = 0; // ensure that error flag is 0

RCIE_bit = 1; // enable interrupt on UART1 receive
TXIE_bit = 0; // disable interrupt on UART1 transmit
PEIE_bit = 1; // enable peripheral interrupts
GIE_bit = 1; // enable all interrupts

while (1) {
if(dat[7]=="MY STRING"){
RCA0_bit = 1;
}

if (dat[5]) { // if an error detected, signal it by
PORTD = 0xAA; // setting portd to 0xAA
dat[5] = 0;
}
if (dat[4]) { // upon completed valid message receive
dat[4] = 0; // data[4] is set to 0xFF
j = dat[3];
for (i = 1; i <= dat[3];i++){
PORTB = dat[i-1];
}
dat[0] = dat[0]+1; // increment received dat[0]
Delay_ms(1);
RS485Slave_Send(dat,1); // and send it back to master
}
}
}

enter image description here

最佳答案

char dat[9]; 定义了一个 9 字节的数组,所以 dat[7] = "MY STRING"; 只会存储截断的地址,而不是实际的地址字符串。 memcpy 可用于将字符串从一个缓冲区复制到另一个缓冲区。

参见:http://www.cplusplus.com/reference/cstring/memcpy/

dat[7]=="MY STRING" 将不起作用,因为您将字符串文字 "MY STRING" 的 2 字节地址与存储的单个字节进行比较在 dat[7]。如果您需要比较字符串的差异,可以使用 strcmp

参见:http://www.cplusplus.com/reference/cstring/strcmp/

关于c - MicroC rs-485、pic16f887 字符串发送问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com