Pyserial Transfer From One Port to Another Port

I have a flight computer and ground station which is based on arduino. Flight computer transmits sensors data to ground station with rf communication modules. So, I'm trying to write a program for getting data's from ground station then send to another computer with serial communication. Actually I did the sending part with user input. This is the program:

import struct
import time

import serial

array1 = bytearray(78)
array1[0:4] = bytes.fromhex('FFFF5452')
array1[76:78] = bytes.fromhex('0d0a')

altitude = float(input("altitude: "))
gps_altitude = float(input("gps altitude: "))
latitude = float(input("latitude: "))
longitude = float(input("longitude: "))
payload_altitude = float(input("payload altitude: "))
payload_latitude = float(input("payload latitude: "))
payload_longitude = float(input("payload longitude: "))

array1[6:34] = struct.pack('<7f', altitude, gps_altitude, latitude, longitude, payload_altitude, payload_latitude, payload_longitude)

with serial.Serial('COM16', 19200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE) as ser:
    while True:
        array[75] = sum(array[4:75]) % 256
        ser.write(array1)
        array1[5] += 1
        time.sleep(3)

This is working for sending user inpt to other pc via com port.

I couldn't get sensor data from arduino instead of user inputs

this is the code for the ground station where i am trying to get the data (arduino)

struct Signal {
  byte langt[4];
  byte longt[4];

} data;


byte i;
void setup() {
  Serial3.begin(9600);
  Serial.begin(9600);
}


void loop() {
  while (Serial3.available()) {
    receive(&data);
    Serial.print("langt:");
    Serial.println((*(float *)(data.langt)), 6);
    Serial.print("longt  :");
    Serial.println((*(float *)(data.longt)), 6);
  }
}


bool receive(Signal *table) {
  return (Serial3.readBytes((uint8_t *)table, sizeof(Signal)) == sizeof(Signal));
}

How can I edit the above python program to transfer data coming from arduino?

transferring multi-byte data requires knowing the start and end of the packet of information.

the receiver typically monitors the input for a start "sequence", discarding bytes until found and even then, the actual data my "look" like the start sequence and a checksum is needed to verify that a valid packet has been received.

encoding the data in ascii is less efficient, but easier to handle because a common "end" character is a newline, '\n', or carriage return '\r' and on arduino, Serial.readBytesUntil() would conveniently read the "line" representing the packet of data

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.