0

I'm getting an error when trying to run some code on my raspberry pi. Here is the source code:

/*
SimpleReceive
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/

#include "VirtualWire.h"
#include "wiringSerial.h"
#include <wiringPi.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

const char message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
unsigned char messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
int fd;

wiringPiSetup();
fd = serialOpen("/dev/ttyAMA0", 9600);

void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}

void setup()
{
serialOpen("/dev/ttyAMA0", 9600);
serialPrintf(fd, "Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
send("Hello there");
//delay(1000);
if (vw_get_message(message, &messageLength)) // Non-blocking
{
serialPrintf(fd, "Received: ");
for (int i = 0; i < messageLength; i++)
{
serialPrintf(fd, message[i]);
}
serialPuts(fd, "test");
}
}

I'm getting this error:

receiver.c:37:6: error: initializer element is not constant
 fd = serialOpen("/dev/ttyAMA0", 9600);

Any ideas what they mean?

Thanks!

ajnauleau
  • 109
  • 1
  • What instructions are you using trying to "run some code"? – joan Jan 24 '20 at 21:28
  • You are still trying to run Arduino code on the Pi. This code is nonsense. – Milliways Jan 24 '20 at 21:50
  • 2
    WiringPi is also deprecated http://wiringpi.com/wiringpi-deprecated/ – CoderMike Jan 24 '20 at 23:19
  • @Milliways If by "nonsense" you mean more-or-less valid standard C. Albeit unformatted. I think this is an error that is peculiar to C, and not C++, which if the OP is used to arduino code, that uses a C++ compiler. @ajnauleau That's a compiler error. Did you search online for what it means? It has to do with the way messageLength is declared; ideally you should move it into loop(),if that is the only place it is used. If not it gets slightly more complicated. – goldilocks Jan 25 '20 at 12:38
  • @goldilocks I mean "nonsense" literally i.e. the code makes no sense. It is not a valid c (or c++) program, because it lacks a main().

    It fails the precompiler, by attempting to use a function as initialiser. It never actually gets to the c compiler because of this error, but if it did would produce a string of error messages.

    It is attempting to call an Arduino library.

    – Milliways Jan 25 '20 at 22:05
  • That a piece of code doesn't have a main() doesn't make it invalid (though the global wiringPi() call probably does). If the OP expects that to run as a standalone program we are very far from home -- but I read "some code" "getting an error", and the code provided does show the cause of the problem, which is a perfectly sane and valid way of asking a question. I doubt that's the OP's only problem, but it's the one he's digging into here. – goldilocks Jan 26 '20 at 11:49
  • @ajnaulen If you were expecting to write a loop() based Arduino sketch and run it as a normal C or C++ program, you need to sit down and read some basic tutorials on those languages outside of an Arduino context. – goldilocks Jan 26 '20 at 12:02

0 Answers0