from machine import Pin, I2C,PWM,UART
import ssd1306
from time import sleep_ms,sleep
'''
import serial
import time
# Configure the serial connection (adjust the port and baudrate as needed)
ser = serial.Serial('COM1', 9600, timeout=1) # Replace 'COM3' with your port
ser2 = serial.Serial('COM2', 9600, timeout=1)
def send_lin_frame(id, data):
frame = bytearray([id]) + data # Create the LIN frame
ser.write(frame) # Send the frame
def ser2_send_lin_frame(id, data):
frame = bytearray([id]) + data # Create the LIN frame
ser2.write(frame) # Send the frame
def ser2_receive_data():
while True:
if ser2.in_waiting > 0:
response = ser2.read(8) # Read the expected frame size
print("Received:", response)
def receive_data():
while True:
if ser.in_waiting > 0:
response = ser.read(8) # Read the expected frame size
print("Received:", response)
# try:
# while True:
# data = bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06])
# send_lin_frame(0x01, data) # Send LIN frame
# print("Frame sent!")
# time.sleep(1) # Wait for a second
# # receive_data() # Check for incoming data
# ser2_receive_data()
# except KeyboardInterrupt:
# ser.close()
# ser2.close() # Close the serial connection
# ESP32 Pin assignment
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# pwm = PWM(19,freq = 500)
# uart1 = UART(0, baudrate=9600, tx=41, rx=40)
# uart1.write('hello') # write 5 bytes
# uart1.read(5)
# Enable = Pin(13,Pin.OUT,Pin.PULL_DOWN)
# Dir = Pin(12,Pin.OUT,value=0)
# Step = Pin(14,Pin.OUT)
# Ms1 = Pin(25,Pin.OUT,value = 1)
# Ms2 = Pin(26,Pin.OUT,value =0)
# Ms3 = Pin(27,Pin.OUT,value = 0)
# oled_width = 128
# oled_height = 64
# oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# oled.text('Stepper motor angle!', 0, 10)
# oled.show()
# angle = 1
# for i in range(400):
# Step.value(0)
# sleep_ms(10)
# Step.value(1)
# angle = (i+1)*0.9
# oled.text('Stepper motor angle!', 0, 10)
# oled.text( str(angle), 55, 40)
# oled.show()
# oled.fill(0)
# complete_angle = angle
# sleep(10)
# Dir = Pin(12,Pin.OUT,value=1)
# for i in range(400+2):
# Step.value(0)
# sleep_ms(10)
# Step.value(1)
# oled.text('Stepper motor angle!', 0, 10)
# angle =complete_angle -(i-1)*0.9
# oled.text( str(angle) , 55, 40)
# oled.show()
# oled.fill(0)
# while 1:
# for i in range(1023):
# pwm.duty(i)
# sleep_ms(100)
# sleep_ms(100)
# for i in range(1023,1):
# pwm.duty(i)
# sleep_ms(100)
# while 1:
# for i in range(-45,45):
# servo.angle(i)
# sleep_ms(100)
# sleep_ms(100)
# for i in range(45,-45):
# servo.angle(i)
# sleep_ms(100)
# from machine import UART
# uart1 = UART(1, baudrate=9600, tx=17, rx=16)
# uart1.write('hello') # write 5 bytes
# print(uart1.read(5) )
'''
from machine import UART, Pin
import time
# Initialize UART1 with TX on pin 33 and RX on pin 32
uart1 = UART(1, baudrate=9600, tx=17, rx=18)
uart2 = UART(2, baudrate=9600, tx=5, rx=16)
# Function to send a message
def send_message(message):
uart1.write(message) # Send the message
print("Sent to UART2:", message)
# Function to receive a message
def receive_message():
if uart1.any(): # Check if there is any data available
received = uart1.read() # Read 5 bytes
if received:
print("Received to uart1:", received.decode('utf-8')) # Decode bytes to string
def send_message2(message):
uart2.write(message) # Send the message
print("Sent to uart1:", message)
# Function to receive a message
def receive_message2():
if uart2.any(): # Check if there is any data available
received = uart1.read() # Read 5 bytes
if received:
print("Received to UART2:", received.decode('utf-8')) # Decode bytes to string
# Main loop
while True:
send_message('Are you there?') # Send a message
time.sleep(1) # Wait for a second
receive_message2() # Check for incoming messages
# using UART 2
send_message2('I got it') # Send a message
time.sleep(1) # Wait for a second
receive_message() # Check for incoming messages