GAURAV KUMAR

Friday, July 26, 2013

Communicate with an unknown usb serial device in linux

Hi all,
Recently I was trying to work with a 2D barcode scanner which was told to work as serial data device. When I inserted it in my linux system, there was nothing like a serial port or something in my "/dev" list. When I searched with the model number, I found nothing relevant to make it work. Usually it is suspected that the driver is not present in the system. I was really frustrated at that time since there was no lead and it had to be done soon. The material came with no driver. When I saw the syslog, it showed some path related to the device inserted. There I found information about the manufacturer and googled it which led me to a website where I found windows7 driver of this device. It felt like I was halfway there. I quickly downloaded the driver, installed and run it. And yes, I was able to view serial data after scanning 2D barcodes. But this wasn't it. It had to work on a linux system too.

As I tried to know more about the problem, I learned that there is a way. Writing a udev rule to control the usb device sounded like a cool thing to do. But the thing is, I had never written a udev rule and I was anxious to make the device work. So I kept searching around a bit and found something that said an unknown usb serial device can be recognized by the system if we load the kernel device driver with the product id and vendor id of the device. Finding product id and vendor id was easy. It is printed by "lsusb" command. So I gave it a try.

modprobe usbserial vendor=0x1eab product=0x0d02

I saw "dmesg" and there it was. the device was recognized as a serial device and found as /dev/ttyUSB0 .

Lesson: An unknown serial usb device doesn't need any extra driver but the usual usbserial driver can be used to register it in the linux kernel.

Saturday, February 16, 2013

multicast in lwip using LM3S6965 micro-controller


Hi all,

recently I was working on sending multicast packets to multiple LM3S6965 micro-controllers. I was using lwip library and my experience with lwip is not much.
So, naturally I googled for some examples to get the start. But unfortunately I could not find any complete working example. So, I did a bit of trial and error.
After struggling a while, I was able to send and receive multicast packets. So, I thought I would write a working example for this. I did this using raw sockets and without RTOS.

A few things to do first:-
1. enable igmp in lwipopts.h file by adding this line :-
#define LWIP_IGMP 1
2. enable udp options and disable NETCONN and SOCKET options from lwipopts.h file
#define LWIP_UDP 1
#define LWIP_NETCONN 0
#define LWIP_SOCKET 0

3.if you are using DHCP options then set netif flags in dhcp.c file.
Replace line: netif->flags |= NETIF_FLAG_DHCP;
With line: netif->flags = NETIF_FLAG_DHCP | NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_IGMP;

Now you are all set. Write the code to start receiving and send a sample packet.


void UDP_Multicast_init(void)
{
struct ip_addr ipgroup,test_ip;
struct udp_pcb *g_udppcb;
char msg[] = "gaurav";
struct pbuf* p;
p = pbuf_alloc(PBUF_TRANSPORT,sizeof(msg),PBUF_RAM);
memcpy (p->payload, msg, sizeof(msg));

IP4_ADDR(&ipgroup, 238, 0, 0, 3 ); //MultiCasting Ipaddress.
#if LWIP_IGMP
iret = igmp_joingroup(IP_ADDR_ANY,(struct ip_addr *)(&ipgroup));
UARTprintf("ret of igmp_joingroup: %d \n\r",iret);
#endif
g_udppcb =( struct udp_pcb*) udp_new();
udp_bind(g_udppcb, IP_ADDR_ANY, LOCAL_PORT); //to receive multicast
udp_recv(g_udppcb, NET_UDP_rec,NULL);// (void *)0); //NET_UDP_rec is the callback function that will be called every time you receive multicast
udp_sendto(g_udppcb,p,&ipgroup,LOCAL_PORT); //send a multicast packet
}

Note that you don't need udp_connect to send or receive multicast.

It worked for me. I use lwip 1.3.2