Click here to Skip to main content
15,891,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write a c code to transfer data serially out from my laptop and receive it back on the same laptop.

I eventually want to read from a file (.dat file) and write it back to a new file (.dat file) .

And later make it work in real time that is : Through some external source i get my data (which is stored in a dat file previously) and directly stream my data to loopback without having the need to store it in a file and read from it.

What I have tried:

I have a usb to serial cable and I've connected the transmitter and receiver pins together for loopback. Do i need any more connection?

My code seems to transmit data fine (how can i be sure of that? ).
But is not reading it.

The flow of my program :
1) open port (read write): fd = open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY)
2) Set attributes of termios
3) Write data to Tx . (Data predefined in the code itself)
4) Read data :
while(1)
{ bytes_read =read(fd,&read_buffer,10);
if (bytes_read == -1)
switch(errno) {
case EAGAIN : continue;
default : goto quit;
}
}
The program is stuck in this loop.

Thank You.
Posted
Updated 8-Jun-16 19:54pm

Please see, for example, this manual: Serial Programming Guide for POSIX Operating Systems.

—SA
 
Share this answer
 
"Do i need any more connection?"
Depends on how you have configured the port (and sometimes the hardware): for a "proper" loopback, it's a good idea to connect RTS to CTS (7 to 8 on 9pin serial connector), and DTR to DSR and DCD (1, 4, 6 on 9pin) to bypass any hardware handshaking, and to set RTS and DTR high.
"The program is stuck in this loop"
That may be because you are insisting it reads 10 bytes at a time - so if you only send 9, it will never get past the Read call.
I'd read byte-by-byte (especially in the beginning) and buffer up responses into a complete "command" rather than assuming it will be complete and correct at all times.
But...since you specify O_NDELAY when you open the port, if sufficient data isn't available (i.e all ten bytes) it will return EAGAIN immediately. Since Serial is generally slow (9600 baud is around 960 characters per second) and processors are fast, it's unlikely that all (or even any) of the data has looped back immediately after the write operation.

Start by checking your loopback works with a terminal program, then check your code by sending and receiving character by character.
 
Share this answer
 
Comments
Archit Mathur 9-Jun-16 2:04am    
"Start by checking your loopback works with a terminal program ".
Can you please elaborate on how to do this?

Thank You.
OriginalGriff 9-Jun-16 2:39am    
Open a terminal program - it looks like you are on linux, so probably one of these:
http://www.tecmint.com/linux-terminal-emulators/
and type into it. If your hardware is connected and working, you should see the characters coming back. Unplug the loopback and they should stop. It's just a basic test that your hardware is all set up and working before you try to add the complication of your code to the mixture.
I can't recommend one, I don't use linux! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900