반응형
마지막으로 주의 할것은 정수, 실수의 크기 이다.
보통 정수는 4바이트 이지만 아두이노 정수는 2바이트ㄷ, 실수는 4바이트 이다.
그렇기 때문에 파이썬 코드에서도 정확하게 맞게 작성해야 한다.
파이썬에서 시리얼 통신을 사용하기 위해서는 설치해야 한다.
pip install pyserial
#숫자 데이터를 읽고 쓰기
import struct
import serial, time
#원도우용으로 시리얼 포트를 연다.
ser = serial.Serial(port = "com3", baudrate=9600, timeout=2)
if (ser.isOpen() == False):
ser.open()
ser.flushInput()
ser.flushOutput()
try:
while True:
if ser.readable():
data = ser.read(10)
if len(data)==10 :
(age, height, weight) = struct.unpack('=hff', data)
print('data: {0:d}, {1:f}, {2:f}'.format(age, height, weight))
time.sleep(1)
except (KeyboardInterrupt, SystemExit):
print("Exit...")
finally:
ser.close()
print("Good by!")
typedef struct _Info
{
int age;
double height;
double weight;
_Info()
{
}
}Info;
Info a;
volatile unsigned long previousMillis;
void setup()
{
Serial.begin(9600);
previousMillis = millis();
// Serial.println(sizeof(int));
// Serial.println(sizeof(float));
// Serial.println(sizeof(double));
a.age =30;
a.height = 1024.0;
a.weight = 768.1;
}
void loop()
{
unsigned long currentMillis = millis();
if ((currentMillis-previousMillis)> 1000*1)
{
Serial.write((char*)&a, sizeof(Info));
previousMillis = currentMillis;
}
return ;
}
'파이썬' 카테고리의 다른 글
파이썬 바이너리파일 쓰기 -#8 (0) | 2021.10.04 |
---|---|
파이썬 바이너리파일 쓰기 -#7 (0) | 2021.10.03 |
파이썬 바이너리파일 쓰기 -#5 (0) | 2021.10.03 |
파이썬 바이너리파일 쓰기 -#4 (0) | 2021.10.03 |
파이썬 바이너리파일 쓰기 -#3 (0) | 2021.10.03 |
댓글