質問

I am working on a project that uses non standard C Libraries to display the output on an LCD screen. I have the code working great but I have run into a problem.

What my intended purpose of this program is to do is take a command line string of text and convert it to the ASCII decimal values then display them on the screen. The way that you output text to the screen is by making a call to the serialPutchar function so to display the letter H I would write it as serialPutchar(fd, 'H'); I want to be able to get the values from variables and output the letters in the variable.

The problem is that when I write it as serialPutchar(fd, "%c", H); or try serialPutchar(fd, "%d", x); I get the following error:

testing.c: In function âmainâ:
testing.c:22:3: warning: passing argument 1 of âserialPutcharâ makes integer from pointer without a cast [enabled by default]
/usr/local/include/wiringSerial.h:30:14: note: expected âintâ but argument is of type âchar *â
testing.c:22:3: error: too many arguments to function âserialPutcharâ
/usr/local/include/wiringSerial.h:30:14: note: declared here

I'm guessing it cant be used in that fashion like you would use printf so is there an alternative to this or maybe I just have a simple error I am not spotting. I am including a link to the documentation for the wiringSerial library. Also from my error output I am getting the strange characters around the error testing.c In function main: and several other lines. Is there a way to prevent this? Link to library HERE Below is my working code to output HELLO:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <wiringPi.h>
#include <wiringSerial.h>

int main (int argc, char *argv[])
{
  int fd ;
  if ((fd = serialOpen ("/dev/ttyAMA0", 9600)) < 0)
  {
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

  if (wiringPiSetup () == -1)
  {
    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
    return 1 ;
  }
    int H = 1;
         serialPutchar(fd, 'H');
         serialPutchar(fd, 'E');
         serialPutchar(fd, 'L');
         serialPutchar (fd, 'L');
         serialPutchar (fd, 'O');
  }

:::UPDATE:::

Here is the working code that fit my description:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <wiringPi.h>
#include <wiringSerial.h>

int main (int argc, char *argv[])
{
  int fd ;
  if ((fd = serialOpen ("/dev/ttyAMA0", 9600)) < 0)
  {
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

  if (wiringPiSetup () == -1)
  {
    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
    return 1 ;
  }
        for (int i=1; i<argc; i++){
         serialPrintf (fd, "%s",  argv[i]);
        }
  }
役に立ちましたか?

解決

putChar takes a char as its second argument. Not a string, not a format string with arguments, just a char.

If you have a char in a variable x, just do:

 serialPutchar(fd, x);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top