0

I am experimenting reading (and eventually writing) serial ports in C. I want to be able to connect to a port on debian and read in some data but I need a port that is writing (speaking). I am new to linux programming.

What port, that will definitely be present and talking in debian, can I connect to to read some data in?

Can you also suggest a port I can eventually use to write to aswell?

I've tried connecting to /dev/ttyUSB1 that this example uses but that port doesn't exist.

Community
  • 1
  • 1
sazr
  • 24,984
  • 66
  • 194
  • 362
  • 1
    It's impossible to understand what are you asking. – Iharob Al Asimi Apr 29 '15 at 23:39
  • 2
    Open `/dev/random` and read from that. Also, you don't read from ports *per se*, you read from sockets. Fortunately, sockets and "normal" files are both the same in POSIX, so you can read from `/dev/random` as if it was a socket that received data from a network. – Paul Manta Apr 29 '15 at 23:43
  • 2
    @PaulManta: Make that `/dev/urandom`. It uses a pseudorandom number generator that is mixed when entropy is available, but never blocks; whereas `/dev/random` will block if you run out of entropy. (Entropy is basically a measure of *"unused random bits"* collected from various random-ish sources like clock jitter and such timings, within the kernel.) – Nominal Animal Apr 29 '15 at 23:54

1 Answers1

0

I would suggest either open /dev/random (or /dev/urandom) as Paul suggests or create your own socket and read/write to that. Don't just pick an arbitrary socket and hope it has information that no other process needed.

If this is your first time working with sockets I would also suggest that you try playing around with things in a language like python simply because you don't need to recompile to see where you went wrong and the warnings are often more readable (take a look at https://docs.python.org/2/howto/sockets.html)

As a side note: If you have access to an arduino you might like to try connecting to that socket (usually something like ser = serial.Serial('/dev/ttyACM0', 9600) in python).

ilent2
  • 5,171
  • 3
  • 21
  • 30
  • Thanks that connected successfully and I read data in. The data is coming in as binary (I imaging thats whats being sent or maybe my setup isn't configured to receive ascii). Do you know of ports that are writing ascii? – sazr Apr 30 '15 at 00:08
  • `/dev/urandom/` will output random bytes (8-bit), if you want ASCII (7-bit) you can take the modulo 2 of the output `output % 2` however your character set might not provide symbols for all ASCII characters. – ilent2 Apr 30 '15 at 00:18
  • Oops, not modulo 2, divide by 2. – ilent2 Apr 30 '15 at 00:18