티스토리 뷰
파이썬으로 시리얼통신을 구현할 때 필요했던 부분을 메모
1. 4바이트 헥사문자열을 10진수 아스키 문자열로 변환하기
2. 아스키 문자열의 길이가 10자리가 되지 않는다면 0(0x30)으로 채우기
3. 패킷데이터 변경이 필요한 버퍼는 bytearray로 사용하기
hexstr = "1a2b3c4d"
# Hex String to Int
dec = int(hexstr, 16)
print(dec)
# 10자리 문자열로 변환 (10자리가 안되면 0으로 채우기)
decstr = format(dec, '010')
print(decstr)
data = bytes(decstr, encoding="utf-8") # 변경할 수 없는 배열
print(data)
sendbuffer = bytearray([0x02, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0d, 0x0a, 0x03]) # 변경할 수 있는 배열
print(sendbuffer)
sendbuffer[1:11] = data
print(bytes(sendbuffer))
결과
439041101
0439041101
b'0439041101'
bytearray(b'\x020000000000\r\n\x03')
b'\x020439041101\r\n\x03'
반응형
'개발 > Python' 카테고리의 다른 글
python bytearray to hex string (0) | 2022.12.07 |
---|
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- AlertDialog
- viewDidAppear()
- Nginx #NodeJS #웹서버 #포트포워딩 #웹호스팅 #백엔드개발 #웹개발 #웹프로젝트 #서버관리 #웹애플리케이션
- Bad mode in Synchronous Abort handler detected
- Naver Cloud Platform
- sms api
- Dart Flutter
- setTimeout
- wsl usb mount
- whose view is not in the window hierarchy
- USB Mass Storage
- usbipd attach
- clearTimeout
- action bar
- ProgressBar
- 루미니
- 어바니엘
- Window Status Bar
- SHORT URL
- sms
- raspberrypi serial
- wsl lsblk
- viewDidLoad()
- 다트 플러터
- Shell Script
- artik710
- 롯데캐슬
- Android Bar Color
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함