0

Im try to communicate serial data between an Arduino and Python. I get this list of Character but im not able to convert them into integers. I didnt got it running through decoding: It works when im sending values from 0 - 255 aka single bytes...

I'm using Python 3.7

y.encode('utf-8').strip()

Python Code

import serial
import numpy as np

barr = [4]

ser = serial.Serial(
    port='COM12',\
    baudrate=56000,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=5)

print("connected to: " + ser.portstr)
count=0

while True:

    y = ser.readline(512)
    print(y)

Python Output:

b'\x9a1\x9aX\x9a\x7f\x9a\xa6\x9c\xce\x9a\xf5\x9a\x1c\x9aC\x9cj\x9a\x91\x9a\xb8\x9a\xdf\x9c\x06\x9a-\x9cT\x9a{\x9c\xa2\x9c\xc9\x9a\xf0\x9a\x17\x9c>\x9af\x9a\x8d\x9a\xb4\x9a\xdb\x9a\x02\ and it goes one....

Serial.write part of Arduino Code:

for (unsigned short int nIndexStep = 0; nIndexStep<g_objRF.getConfiguration()->getFreqSpectrumSteps(); nIndexStep++)
  {

    //Print every step of sweep data onto display
    int stepFreq = g_objRF.getSweepData()->getFrequencyKHZ(nIndexStep);
    int stepAmp  = g_objRF.getSweepData()->getAmplitudeDBM(nIndexStep);
    delay(serialdelay);

    Serial.write(stepFreq);
    delay(serialdelay);
    Serial.write(stepAmp);
    if (stepAmp <= (TopLevel -90))
    {
      stepAmp = TopLevel -90;
    }
    if (stepAmp > TopLevel)
    {
      stepAmp = TopLevel;
    }
    //Serial.print(stepAmp);
    //Serial.print(", ");
    comp2 = stepAmp;
    comp1 = max(comp1, comp2);




    {

    }

  }



      }
      Serial.write("\n");


1 Answers1

0

With Serial.write you can send a byte, a string or a byte array. You cannot send an integer.

If you want to send an integer you'll have to convert it to a byte array first.

There are countless ways to do that. Bit masks, cast pointers, unions... just search the web and pick your favourite.

See https://www.arduino.cc/reference/en/language/functions/communication/serial/write/

Piglet
  • 27,501
  • 3
  • 20
  • 43