C Opening USB serial device in C

Hello all. I need some help. I'm trying to open the serial port to a usb modem device using the libc read() function. No matter what I do, I get a permission error. Does anyone have any ideas of how I can successfully open this device?
 
The fact that it is USB makes no difference; in the end, it is a normal /dev/ttyXXX device, with a somewhat strange name (probably with a "U" in it). You can handle it exactly like any other serial port.

If you get a permission problem, the source is most likely that ... drum roll ... you don't have permission. Most likely you are a normal user, logged in with a normal user name, not as root. Most likely, the serial device is owned by root or operator, and its permissions are probably something like "rw-------", meaning only the owner can use it.

To debug this, here is a nasty (brutal, illegal, immoral ...) suggestion: First check that the serial device really exists, and who its owner and permissions area. Then log in as root, and then run your program. If it suddenly starts working (or at least, the permission error goes away), that was the problem. In that case, there are several possible solutions, with different levels of nasty-ness. Really bad idea: Just start logging in as root and doing all the work as root. This is just a bad habit to get into, and any small mistake (like "rm *") and your system is wiped out. You can also quickly manually change the permissions of the device to be "rw-rw-rw-" (with the command chmod a+rw /dev/tty...), or you can change the ownership of the device (with chown adam:bob /dev/tty..., assuming you are user adam in group bob). The problem with those two approaches is that everytime the machine reboots, or the USB device is destroyed or recreated, it will go back to being owned by root and having normal permissions. There is a way to fix that problem (by editing some config file, I think it is devd.conf), but I have really bad internet connection right now, and can't get to my FreeBSD machine right now, so can't offer help on fixing that.
 
One thing that come to mind is port locking.
You start programatically manulipulating the serials on your development box and they become locked.
You try new code on them but they are locked. You may need to do some rebooting in between code attempts to clear the port.
Keep a eye on the /dev/cuaU0.x and you will see what I am talking about.
I know serial/usb cellular modems lock the ports.

For Example from my /dev:
cuaU0.2
cuaU0.2.init
cuaU0.2.lock
This MC7700 internal modem has no sim and no comms. Something is still locking the port.
 
As it turns out, the issue was port locking. I rebooted the system a few hours after my posting & the issues with opening the port disappeared. It's funny how I didn't come across this info in any of my google searches. I really would've expected this to be written down somewhere.
Anyway, I came back to write about what happened when I say the two comments above. No, permissions weren't the issue, because I also ran the program as root. Now that this has been cleared up, I can get back to the task at hand -trying to communicate with the devices. They're Grid+ fan controllers from NZXT. Obviously, there's no official support from the company outside of Windows support. There're scripts for it written by Linux users, but I didn't want to go with scripts (for my own reasons); and they would've had to be ported anyway, so why not just write the app in the languages that I'm most comfortable with. Now, it's just a matter of using the correct protocol to initialize the devices. Anyway, thanks a lot guys!
 
Rather than a reboot. Which seems a bit brutal, and time consuming. I don't suppose you can treat it like a serial port in the fashion of sending a them a hangup: ATA ^A ^H. Been a l-o-n-g time. But just thought I'd suggest it. In case it helped. :)

--Chris
 
Back
Top