简体   繁体   中英

Error in arduino communication with COM

Arduino code

 #include <TFT.h> #include <SPI.h> #define cs 10 #define dc 9 #define rst 8 TFT TFTscreen = TFT(cs, dc, rst); int led = 13; void setup() { TFTscreen.begin(); Serial.begin(9600); pinMode(led, OUTPUT); } void loop() { TFTscreen.background(0, 0, 0); TFTscreen.setTextSize(1); if (Serial.available() > 0) { digitalWrite(led, HIGH); TFTscreen.text(Serial.read(), 0, 0); } } 

Python code

 import os import sys import serial import datetime ser = serial.Serial('COM4', 9600) print(ser.name) print(datetime.datetime.now()) date_string = str(datetime.datetime.now()) date_bytes = date_string.encode('ascii') ser.write(date_bytes) print('OK') ser.close 

Python is working normal, but Arduino give me this error invalid conversion from 'int' to 'const char*' [-fpermissive], I think problem with Type of Data, but I began learn this language yesterday.

The problem is here I think:

TFTscreen.text(Serial.read(), 0, 0);

You call the "read" function which returns an integer. The "text" function of your screen wants a char pointer instead as the first parameter, and you can't cast an int to char* like said in the error "invalid conversion from 'int' to 'const char*"

You can look up all the parameter and return types in the arduino documentation: https://www.arduino.cc/en/Serial/Read

maybe, on your python add a '\\0' before sending the string to the serial, this will serve as the end of received string. I can not remember if python is sending this by default but you can explicitly just send this.

on the arduino, fetch the received data and fill a character array (maybe use a counter or do pointer aritchmetic, be careful of going past the array's declared maximum size), and then wait for the '\\0'. once it gets the \\0, append it as well to the char array, and do a TFT display. then repeat the process again, reset the counter then wait for the incoming byte stream.

I haven't used that TFT library but one of the problems that I anticipate in your implementation is that it will not display any words because you will keep on writing every character to 0,0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM