Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing commands to serial
#8
I am learning Python new and i want send to date and time but i have some problem.
When i write to input method something, i can send to my message.But the message been date and time on windows ,cannot send to arduino. Screen output become like that b' ' . I need to year,month,day,hour,minute and second data.

import serial
import time
import datetime
 	

ser = serial.Serial('COM3', 9600, timeout=0)
ser.reset_input_buffer()
time.sleep(1.5)

now=datetime.datetime.now()

#str_now=now.strftime("%Y.%m.%d.%H.%M.%S")+'\r\n'

year = now.strftime("%Y")+'\r\n'
#print("year:", year)

#month = now.strftime("%m")+'\r\n'
#print("month:", month)

#day = now.strftime("%d")+'\r\n'
#print("day:", day)

#hour = now.strftime("%H")+'\r\n'
#print("hour:", hour)

#minute = now.strftime("%M")+'\r\n'
#print("minute:", minute)

#second = now.strftime("%S")+'\r\n'
#print("second:", second)

#var = input("Enter something: ")+'\r\n'

ser.write(year.encode())

while 1:
    try:
        print (ser.readline())
        time.sleep(1.5)
    except ser.SerialTimeoutException:
        print('Data could not be read')
Arduino code:
int incomingByte = 0;
 
void setup(){
// Open serial connection.
Serial.begin(9600);
 
}
 
void loop(){
if (Serial.available() > 0) {
 
 // read the incoming byte:
 incomingByte = Serial.read();
 
 // say what you got:
 Serial.print("I got: "); // ASCII printable characters
 Serial.println(incomingByte,DEC);
}
 
}
Output:

[Image: 1pcuo0.jpg]

How to change python code and send to date and time datas
Reply


Messages In This Thread
Writing commands to serial - by python_beginner - Jan-30-2018, 02:41 PM
RE: Writing commands to serial - by sparkz_alot - Jan-30-2018, 04:04 PM
RE: Writing commands to serial - by DeaD_EyE - Jan-30-2018, 04:25 PM
RE: Writing commands to serial - by cvh - Jun-23-2020, 08:59 PM
RE: Writing commands to serial - by sparkz_alot - Jan-30-2018, 08:29 PM
RE: Writing commands to serial - by python_beginner - Feb-01-2018, 01:19 PM
RE: Writing commands to serial - by dalwood - Feb-01-2018, 03:48 PM
RE: Writing commands to serial - by DeaD_EyE - Feb-02-2018, 10:10 AM
RE: Writing commands to serial - by jambuna35 - Feb-03-2019, 09:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,112 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Trouble writing over serial EngineerNeil 1 2,635 Apr-07-2019, 08:17 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020