W55RP20 UDP 전송률 테스트 예제를 참고로 해서 UDP로 테스트를 해보자
W55RP20에서 UDP 처리는 메인함수에서 UDP 로 데이터가 전송되면 처리하는 함수 Process_udps()를 주기적으로 호출해서 처리하면 된다.
int32_t Process_udps(uint8_t sn, uint8_t* buf, uint16_t port)
{
int32_t ret;
uint16_t size, sentsize;
uint8_t destip[4];
uint16_t destport;
switch(getSn_SR(sn))
{
case SOCK_UDP :
if((size = getSn_RX_RSR(sn)) > 0)
{
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
//printf("%s->%d\n",buf,ret);
if(ret <= 0)
{
#ifdef _LOOPBACK_DEBUG_
printf("%d: recvfrom error. %ld\r\n",sn,ret);
#endif
return ret;
}
ProcessCommand(buf, ret);
}
break;
case SOCK_CLOSED:
if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
return ret;
break;
default :
break;
}
return 1;
}
int main() {
int cnt = 0;
stdio_init_all();
vreg_set_voltage(VREG_VSEL);
sleep_ms(10);
#ifdef RUN_FROM_CRYSTAL
set_sys_clock_khz(12000, true);
#else
// Run system at TMDS bit clock
set_sys_clock_khz(DVI_TIMING.bit_clk_khz, true);
#endif
//setup_default_uart();
gpio_init(LED1_PIN);
gpio_set_dir(LED1_PIN, GPIO_OUT);
gpio_init(LED2_PIN);
gpio_set_dir(LED2_PIN, GPIO_OUT);
Led2On();
printf("Configuring DVI..\n");
InitEthernet();
printf("Start rendering\n");
while (1)
{
//process_tcps(0, g_tcp_buf, 5000);
Process_udps(0, g_tcp_buf, 5000);
}
}
UDP 데이터가 전송되면 전송된 데이터를 처리하는 함수
void ProcessCommand(unsigned char *buf, unsigned int Size)
{
char *ptr;
int size = 0;
int i;
unsigned int pixel_data = 0;
/*
for(int i=0;i<Size;i++)
{
printf("%c", buf[i]);
}*/
if(gCmdStatus == 1)
{
}
else
{
if(strstr((const char*)buf, "CMD_LED_ON"))
{
Led1On();
printf("Led On Command\r\n");
}
else if(strstr((const char*)buf, "CMD_LED_OFF"))
{
Led1Off();
printf("Led Off Command\r\n");
}
else
{
}
}
}
테스트 코드는 간단하게 Python 코드로 작성해 보자
UDP클라이언트로 동작하며 서버인 W55RP20 보드에 접속해 LED를 On/Off 하는 간단한 프로그램이다.
GUI는 QT(PySide6)를 이용해 LED 이미지를 표시 하도록 했다.
# -*- coding: utf-8 -*-
import sys
import socket
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QLineEdit, QFileDialog,QMessageBox
from PySide6.QtGui import QIcon
class UDPClient(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("UDP Client")
self.setGeometry(100, 100, 400, 300)
self.layout = QVBoxLayout()
# IP 주소 입력 필드
self.ip_input = QLineEdit("192.168.1.6")
self.ip_input.setPlaceholderText("Enter server IP address (e.g. 127.0.0.1)")
self.layout.addWidget(self.ip_input)
# 포트 번호 입력 필드
self.port_input = QLineEdit("5000")
self.port_input.setPlaceholderText("Enter server port (e.g. 12345)")
self.layout.addWidget(self.port_input)
self.message_input = QLineEdit()
self.message_input.setPlaceholderText("Enter message")
self.layout.addWidget(self.message_input)
self.send_button = QPushButton("Send Message")
self.send_button.clicked.connect(self.send_message)
self.layout.addWidget(self.send_button)
#이미지 버튼
self.button = QPushButton()
self.button.setIcon(QIcon("led_off.png")) #LED_OFF
self.button.setIconSize(self.button.size() * 0.8)
#self.button.setFixedSize(100, 100)
self.button.clicked.connect(self.change_image)
self.button.setStyleSheet("background-color: white; border: none;")
self.layout.addWidget(self.button)
self.setLayout(self.layout)
#이미지 변경
self.image1 = "led_on.png"
self.image2 = "led_off.png"
self.current_image = self.image2
# LED ON
self.ledon_button = QPushButton("LED ON")
self.ledon_button.clicked.connect(self.send_led_on)
self.layout.addWidget(self.ledon_button)
# LED OFF
self.ledoff_button = QPushButton("LED OFF")
self.ledoff_button.clicked.connect(self.send_led_off)
self.layout.addWidget(self.ledoff_button)
self.setLayout(self.layout)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send_message(self):
message = self.message_input.text()
ip_address = self.ip_input.text()
port = self.port_input.text()
if not ip_address or not port:
self.ip_input.setText("Please enter a valid IP address and port")
return
if not message:
return
try:
port = int(port)
self.sock.sendto(message.encode(), (ip_address, port))
self.message_input.clear()
except ValueError:
self.port_input.setText("Please enter a valid port number")
def change_image(self):
if self.current_image == self.image1:
self.current_image = self.image2
self.send_led_off()
else:
self.current_image = self.image1
self.send_led_on()
self.button.setIcon(QIcon(self.current_image))
def send_led_on(self):
ip_address = self.ip_input.text()
port = self.port_input.text()
if not ip_address or not port:
self.ip_input.setText("Please enter a valid IP address and port")
return
try:
send_msg = "CMD_LED_ON"
send_msg = send_msg.encode('utf-8')
port = int(port)
self.sock.sendto(send_msg, (ip_address, port))
except ValueError:
self.port_input.setText("Please enter a valid port number")
def send_led_off(self):
ip_address = self.ip_input.text()
port = self.port_input.text()
if not ip_address or not port:
self.ip_input.setText("Please enter a valid IP address and port")
return
try:
send_msg = "CMD_LED_OFF"
send_msg = send_msg.encode('utf-8')
port = int(port)
self.sock.sendto(send_msg, (ip_address, port))
except ValueError:
self.port_input.setText("Please enter a valid port number")
if __name__ == "__main__":
app = QApplication(sys.argv)
client = UDPClient()
client.show()
sys.exit(app.exec())
W55RP20 UDP서버에 접속해서 LED를 On/Off 하는 테스트 프로그램
테스트 결과 W55RP20 SSM 보드에 접속해서 UDP로 간단하게 LED제어 할수 있다.
https://youtube.com/shorts/T8M17j99KAU?feature=share
반응형