I am new to C and am trying to write a program that syncs files on my computer to a USB device. It currently works my me cd'ing to the directory that the device mounts to and typing "myprog init" which creates a .myprog file. The idea then is that when a USB device is connected my program checks for the .myprog file, if it finds it then it syncs. Problem is that I can't figure out how to detect when a new USB device is connected.
I am writing the program for Linux (I'm using Ubuntu 9.04) and using GCC.
Thanks for any help :)
Look into udev documentation for this (writing udev rules).
Also have a look at this stackoverflow thread.
Related
I am trying to understand how can I write tasks for VxWorks. I have VxWorks on a board that is mounted on a development board.
I can access the board through Putty and a serial terminal connected to the UART of the system. How can I write other tasks (maybe to communicate with other UARTs or other Serial Interfaces) ?
And how do I know where to find the documentation describing how I can access those serial interfaces? (for example write/read methods, which file to include etc)*
Thank you
VxWorks is cross development environment, so you need a licensed development system on Windows or Linux desktop to create applications.
Then you load the compiled code on the target reference board as process/RTP or LKM/DKM. How you get it there varies by board: FTP, TFTP, removable USB or SD card....
At the C shell on booted system you can start a task in the kernel context with any public symbol. But if your working with deployed system, it shouldn't have the C shell, that's a serious security risk :)
Try..
-> sp printf, "\nHello VxWorks World!\n"
I'm writing a kernel module which will be kind of a driver for my Chinese Arduino.
At one moment I had to disable existing arduino driver and make sure that arduino is not determined by system. I did it with rmmod command. Unplugged and plugged in my arduino. But it still determined in /dev/ as ttyUSB0, same as it was before.
Does it mean that I have more drivers on my system which I don't know about? Or every hardware plugged in usb port will be determined as a file regardless of driver existence? 0_0
Also, of course I have an arduino ide installed on my machine. Maybe there is a built-in driver? If so, how can I disable it without removing ide?
Thank you for the answers.
Does it mean that I have more drivers on my system which I don't know
about?
Kernel may be configured to use a particular driver while user space still might change that.
The manual way: rmmod your driver, unplug Arduino, check system log with dmesg. Plug in Arduino, check dmesg again. Automated way: use usb-devices script from usbutils package (apt-get install usbutils) to list usb devices and the drivers associated with them.
After you find the associated driver, most likely ch341, rmmod it, or add to /etc/modprobe.d/blacklist to disable loading of this module permanently.
I'm working with a USB device in Linux and have written a library to control this device.
Without going in to TOO many details, the device uses a standard UART protocol, so all I have to do is open a serial connection with open, configure the relevant parameters like baud rate, stop bit, parity, etc, etc, and start bit-banging registers.
The library works fine, however, it its hard coded to assume that this device is /dev/ttyUSB0. That is, this is what I pass to open. If open fails, I exit.
What I would like to do is detect that this device is present, regardless if it's /dev/ttyUSB0, /dev/ttyUSB1, etc. And even detect if there are multiple of these devices connected.
I can write code to poll certain registers on the device that will return serial number, product ID, etc, so I can detect that what is on the other end of the USB is indeed my device... but how can I find a list of connected USB devices, again, in native C?
OR is there a more elegant way of doing this, such as interfacing with it's kernel module, or something? I can provide the USB driver it actually uses, but I'm sort of lost when looking through the code.
Thanks for any insight.
The elegant method is to use udev to create a descriptive symlink for your device when it is connected. Add a line something like this to /etc/udev/rules.d
SUBSYSTEM=="tty",ENV{ID_MODEL}=="My_FlowMeter_Model",ENV{ID_USB_INTERFACE_NUM}=="00",SYMLINK+="flowmeter",RUN+="/bin/su pi -c /home/pi/record-flowmeter.sh
That's a very slightly modified version of an actual udev rule my research group uses to collect data from USB devices connected to battery-powered Raspberry Pi boxes. It also runs a script automatically, which has commands like
stty -F /dev/flowmeter 500000 -ixon -echo -icanon
If you want to know the "real" device filename, you could do readlink /dev/flowmeter. But for most uses you can just use the link: fd = open("/dev/flowmeter"); (or pass it as an argument to your program)
Naturally you should replace flowmeter with a short name for your own device, as well as updating the ID_MODEL based on the output from lsusb.
Multiple devices are a bit more complicated, but there are plenty of examples of udev rules out there.
On Linux, the information you are looking for is in the /sys filesystem, specifically under /sys/bus/usb/devices. From there you will need to search the filesystem to find your device.
For example, I just plugged a USB-serial dongle into my Linux (kernel version 2.6.35) and the device appeared under /sys/bus/usb/devices/2.1-8. Here, I am able to find that this is my device by vendorId:deviceId by checking the files idVendor and idProduct. Here, there is a directory named 2.1-8:1:0 which contains a directory named ttyUSB0.
Obviously, to find your device you will need code (or a shell script using find) to scan the directory tree, looking for the right entries.
How can I communicate with an mbed LPC1768 using C without using a terminal emulator like minicom or teraterm?
For example if I send an integer from my C code than that led should be turned on.
How can I do this?
Without reading user manual or datasheet you can't do any thing to your board. So just go through it then follow below link for demo application LPC1768 board
1.NXP LPC1768 ARM Cortex-M3 Red Suite Demo
2.LPC1768 ARM Cortex-M3 CrossWorks Demo
3.mbed
4.At github
If you are using linux or osx check out the following answer:
How do I read data from serial port in Linux using C?
You can check your /dev folder and look for a file that is created when you plug your mBed. The file name would be something like /dev/ttyS0. That file represents the communication port, you can read/write that file to receive/send data over serial port.
If you want a cross platform solution try RxTx library for Java.
You can use this library
what it does, is that it initialize UART port which is connected to the debugger on your board and use it as serial input output. so with any hyper terminal of your choice, you can write code to do something if you send certain string of characters or just use original printf when something happens. and it will be printed on your PC screen.
I want to create a USB driver, so my own C application to be able to get into my flash drive and take information from the imported flash drive.
OS: Windows
I start my app contains my USB driver and I plug-in my flash and its shows in Explorer that this drive is plugged and it is accessible, while that's happening, my app is checking that file data.txt EXIST if that file exist, program run next function, if not program, my program runs down/exit/!
If you are looking for a programmable USB dongle, I suggest inspecting this SO thread: Programmable USB dongles
The Windows Driver Kit should simplify your work or atleast help you at the start.