简体   繁体   English

与COM的arduino通信中的错误

[英]Error in arduino communication with COM

Arduino code Arduino代码

 #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 Python代码

 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. Python工作正常,但是Arduino给我这个错误的从'int'到'const char *'[-fpermissive]的无效转换,我认为数据类型存在问题,但是我昨天开始学习这种语言。

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*" 屏幕的“文本”功能需要使用char指针作为第一个参数,并且您不能像错误“从'int'到'const char *的无效转换”中所说的那样将int转换为char *

You can look up all the parameter and return types in the arduino documentation: https://www.arduino.cc/en/Serial/Read 您可以在arduino文档中查找所有参数并返回类型: 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. 也许,在您的python上,在将字符串发送到串行之前添加“ \\ 0”,这将作为接收到的字符串的结尾。 I can not remember if python is sending this by default but you can explicitly just send this. 我不记得python是否默认发送此消息,但是您可以显式发送它。

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'. 在arduino上,获取接收到的数据并填充一个字符数组(可能使用计数器或做指针算术,请注意不要超出数组声明的最大大小),然后等待'\\ 0'。 once it gets the \\0, append it as well to the char array, and do a TFT display. 一旦获得\\ 0,也将其附加到char数组,并进行TFT显示。 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. 我没有使用过TFT库,但是我期望在实现中遇到的问题之一是它不会显示任何单词,因为您将继续将每个字符写入0,0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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