Attaching USB Serial Adapters – FTDI/CP2102/PL2303/CH340G

12 篇文章 0 订阅
6 篇文章 0 订阅

TL-WR703N: Attaching USB Serial Adapters – FTDI/CP2102/PL2303/CH340G

Although the TL-WR703N has a built in UART, as I have shown in my previous article, it’s rather difficult to access, and is useful as a serial console in OpenWrt. For the support of your own apps, it is far more convenient to simply plug in a cheap USB to serial adapter — this can be done without even opening the case. Since the TL-WR703N only has one USB port, if you are using EXTROOT, both the serial adapter and the USB flash drive can be attached to a USB hub:usbserialThe TL-WR703N can easily supply the required current, so an unpowered hub may be used. To add support for USB to serial hardware, use opkg to install the appropriate modules. Basic USB to serial support:

opkg install kmod-usb-serial

FTDI (FT232) support:

opkg install kmod-usb-serial-ftdi

Silicon Laboratories CP210x (CP2102) support:

opkg install kmod-usb-serial-cp210x

Nanjin QinHeng Electronics CH341 (CH340G):

opkg install kmod-usb-serial-ch341

Prolific PL2303 support:

opkg install kmod-usb-serial-pl2303

Additionally, OpenWrt has packages for a slew of other USB serial adapters that I’m not familiar with. If you are using Attitude Adjustment (12.09) and its official repository, here are the other supported adapters:

kmod-usb-serial-ark3116 kmod-usb-serial-belkin -- Belkin
kmod-usb-serial-cypress-m8
kmod-usb-serial-ipw
kmod-usb-serial-keyspan
kmod-usb-serial-mct -- Magic Control Technology
kmod-usb-serial-mos7720
kmod-usb-serial-motorola-phone
kmod-usb-serial-oti6858
kmod-usb-serial-qualcomm
kmod-usb-serial-sierrawireless
kmod-usb-serial-ti-usb
kmod-usb-serial-visor -- Handspring Visor/Palm m50x/Sony Clie

After you install the appropriate kernel modules, your USB to serial converter will show up as/dev/ttyUSBx

[ 489.990000] usb 1-1.3: new full-speed USB device number 5 using ehci-platform
[ 490.100000] ch341 1-1.3:1.0: ch341-uart converter detected
[ 490.120000] usb 1-1.3: ch341-uart converter now attached to ttyUSB0

Sure, I can help you with that! Here's an example of how to implement TCP/IP socket communication in C: Step 1: Set up the server ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #define PORT 8080 int main(int argc, char const *argv[]) { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); char buffer[1024] = {0}; char *hello = "Hello from server"; // Creating socket file descriptor if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Forcefully attaching socket to the port 8080 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { perror("setsockopt"); exit(EXIT_FAILURE); } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons( PORT ); // Forcefully attaching socket to the port 8080 if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) { perror("bind failed"); exit(EXIT_FAILURE); } if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) { perror("accept"); exit(EXIT_FAILURE); } // Send a message to the client send(new_socket, hello, strlen(hello), 0); printf("Hello message sent\n"); // Close the socket close(server_fd); return 0; } ``` Step 2: Set up the client ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #define PORT 8080 int main(int argc, char const *argv[]) { int sock = 0, valread; struct sockaddr_in serv_addr; char *hello = "Hello from client"; char buffer[1024] = {0}; // Creating socket file descriptor if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("\n Socket creation error \n"); return -1; } memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Convert IPv4 and IPv6 addresses from text to binary form if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { printf("\nInvalid address/ Address not supported \n"); return -1; } if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf("\nConnection Failed \n"); return -1; } send(sock, hello, strlen(hello), 0); printf("Hello message sent\n"); valread = read(sock, buffer, 1024); printf("%s\n", buffer); return 0; } ``` Note: This code is for educational purposes only and should not be used in production without proper testing and modification. Do test it on local-host. Hope this helps!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值