Unity C# Socket 한글 깨짐

C#/problemsC# 2023. 11. 3. 12:14


    private void ReceiveData()
    {
        byte[] buffer = new byte[1024];
        while (isRunning)
        {
            int bytesRead = this.socketClient.GetStream().Read(buffer, 0, buffer.Length);
            if (bytesRead > 0)
            {
                byte[] receivedData = new byte[bytesRead];
                Array.Copy(buffer, receivedData, bytesRead);
                Debug.Log("Received Binary Data: " + BitConverter.ToString(receivedData));

                // 바이너리 데이터를 문자열로 변환하여 확인 (주의: 한글 문자열이 깨져있을 수 있음)
                string decodedString = Encoding.GetEncoding(949).GetString(receivedData);

                print(decodedString);
                ProcessReceivedMessage(decodedString);
            }
        }
    }

바이너리 값으로 받아서 949 or UTF-8 방식으로 직접 변환

:

Serial

Python 2023. 4. 19. 15:36
import serial
import threading
import time
import parkingControl as Main
class Serial:
    def __init__(self, port, baudrate):
        self.port = port
        self.baudrate = baudrate
        self.serial_port = None
        self.thread_recv = None
        self.is_recv = False
   
    def connect(self):
        try:
            self.serial_port = serial.Serial(self.port, self.baudrate, timeout=0)
            self.is_recv = True
            self.thread_recv = threading.Thread(target=self.recv)
            self.thread_recv.daemon = False
            self.thread_recv.start()
            print("Connected!!")
        except Exception as e:
            print(f"Error connecting to serial port: {e}")
   
    def recv(self):
        while self.is_recv:
            data = self.serial_port.readline().decode().strip()
            if data:
                Main.on_receive_serial(data)

    def disconnect(self):
        self.is_recv = False
        if self.thread_recv:
            self.thread_recv.join()
        if self.serial_port:
            self.serial_port.close()
        print(f"diconnect {self.port}")

    def send(self, data):
        self.serial_port.write(data.encode())

if __name__ == "__main__":
    s = Serial("COM7", 115200)
    s.connect()
    while True:
        s.send("Hello, world!")
        time.sleep(10)

'Python' 카테고리의 다른 글

uft-8  (0) 2022.10.31
aes256 암호화 복호화  (0) 2021.01.27
:

Rasp Terminal 자동 실행 및 script 자동 실행

Raspberry Pi 2023. 2. 3. 15:58

https://diggingfun.tistory.com/233

 

라즈베리파이 부팅 후 터미널 자동실행

라즈베리파이의 터미널을 자주 쓰는데 매번 부팅이 완료된 후 터미널을 두 개 열어서 크기를 늘이고... 아..정말 너무 번거롭다는 생각이 들어서 찾아봤다. www.raspberrypi.org/forums/viewtopic.php?t=65607 H

diggingfun.tistory.com

 

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
최 하단, @lxterminal -e python /home/pi/path/AAA.py

:

rasp python2.7 downgrade

Raspberry Pi/rasberry_pi_problems 2022. 11. 21. 15:47

sudo apt autoremove python

sudo apt install python2.7

 

alias python=python2.7

 

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py 

sudo python2.7 get-pip.py

 

이런거 하지말고 그냥 구버전 받기
20200205버전 받기 얌전히

:

uft-8

Python 2022. 10. 31. 14:06
# -*- coding: utf-8 -*-

'Python' 카테고리의 다른 글

Serial  (0) 2023.04.19
aes256 암호화 복호화  (0) 2021.01.27
:

pip 설치 문제 Faltal error in launcher: Unable to create process using

Python/problems_python 2022. 4. 5. 15:34

경로 문제

pip install XXX
라고 쓰지말고

python -m pip install XXX

:

C# 10430 분배법칙

Algorithmus 2021. 10. 25. 10:19

https://www.acmicpc.net/problem/10430

 

using System;
using System.Threading;
using System.Collections.Generic;

namespace NetHappy
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            str = Console.ReadLine();
            string[] result = str.Split(new char[] { ' ' });
            double a = (double)Convert.ToInt32(result[0]);
            double b = (double)Convert.ToInt32(result[1]);
            double c = (double)Convert.ToInt32(result[2]);

            Console.WriteLine((a + b) % c);
            Console.WriteLine(((a % c) + (b % c)) % c);
            Console.WriteLine((a * b) % c);
            Console.WriteLine(((a % c) * (b % c)) % c);
        }
    }
}

'Algorithmus' 카테고리의 다른 글

20200811 임시, 추가 변수없이 두 정수값 교체  (0) 2020.08.11
[설탕 배달]06-12  (0) 2019.06.12
AStar algorithm 4방향(캐릭터 이동)  (0) 2019.05.21
AStar algorithm  (0) 2019.05.20
[Palindrome Number]05-14  (0) 2019.05.14
:

int to Hex byte

C# 2021. 10. 19. 17:28

using System;
using System.Threading;
using System.Collections.Generic;


namespace NetHappy
{
    class Program
    {
        static Dictionary<byte, int> dicCmdDatalength = new Dictionary<byte, int>();
        static void Main(string[] args)
        {
            dicCmdDatalength.Add(0xf4, 16);
            int[] wheelSpeed = { -1, -1, 1000, 1000 };
            byte[] value = InputData(wheelSpeed, 0xf4);

            string strValue = BitConverter.ToString(value);
            Console.WriteLine(strValue);
        }

        static byte[] InputData(int[] data, byte cmd)
        {
            int length = dicCmdDatalength[cmd];
            byte[] result = new byte[length + 5];//+ 5 = STX, STX, CMD, DataLength, ETX
            byte[] value= new byte[4];
            result[0] = 0x55;
            result[1] = 0x55;
            result[2] = cmd;
            result[3] = Convert.ToByte(length);

            int k = 4;
            for(int i = 0; i < 4; i++)
            {
                value = BitConverter.GetBytes(data[i]);
                for(int j = 0; j < 4; j++)
                {
                    result[k] = value[j];
                    k++;
                }
            }
            result[result.Length - 1] = 0xAA;

            return result;
        }
    }
}

: