Click here to Skip to main content
15,843,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I am trying to send a text file to a modem using c sharp.

In the modem manual it specifies: use Send Text file with ASCII Setup: Send line ends with line feeds and Append line feeds to incoming line ends in HyperTerminal Properties enabled.

How do you think these properties affect the text file to be sent??

Here is my code I am using to send the text file.

port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.Encoding = System.Text.Encoding.GetEncoding(1252);
port.RtsEnable = true;
port.Handshake = Handshake.RequestToSend;

StreamReader readtext = new StreamReader("filename.txt");
string contents = readtext.ReadToEnd();
byte[] WriteBuffer=new byte[contents.Length];
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
WriteBuffer = enc.GetBytes(contents);
port.Write(WriteBuffer, 0,contents.Length );

Updated 21-Nov-10 14:11pm

1 solution

What it means is the end of a line is represented by a line feed (ASCII 0x0A), however, that looks like it's talking about the HyperTerminal program. Some programs prefer a line feed, others a carriage return (ASCII 0x0D) and some want both or either, but that's usually a "feature" of the program, not the modem.

Actually a serial port and a modem just encode the bytes you send and unless you interrupt to go to a command mode, you can pretty much send anything and it will make it across, so you can just send the file in most cases, CR, LF and all. The only time there might be some confusion is when you mix Linux/Unix and Windows, since they handle end-of-line differently.

If you are writing software only to send, you should handle EOL as expected by the receiving end. If you are writing both TX and RX, then choose what you want to do and be consistent. If both TX and RX are using the same operating system, you probably don't have to do anything.
Share this answer
sach262 21-Nov-10 6:45am    
Thanks for clearing that up for me Walt. All i am doing is sending a compiled python file (as a text file) to my modem via the serial port in C#. So you saying I dont have to worry about appending CR and LF to each line in the text file before I send it via the serial port? Can I just read the file into a stream object and send it using ASCII encoding??
Dr.Walt Fair, PE 21-Nov-10 10:31am    
If you send it as a byte stream and the operating system is the same on both ends it should make it OK, but what software are you using on the receiving end? Make sure that won't strip CR/LF's or something.
sach262 21-Nov-10 13:43pm    
the receiving end is a telit modem. so first I send a special AT command telling it i am ready to send a file. Then I send the file after that
Dr.Walt Fair, PE 21-Nov-10 13:54pm    
OK, I think you are confusing hardware and software functions. The modem sends whatever bytes you send to it and receives whatever bytes it gets. Under most circumstances, the only exceptions are if it receives a command to go to command mode (to get AT commands, etc.)

So what is the software on the other end expecting? If it is expecting a sequence of strings with CR and/or LF's on each one, that's what you should send. If the receiving software just copies the received bytes directly to a disk file, you don't need to do anything, except indicate when you are finished.
sach262 21-Nov-10 19:06pm    
I have just tried the "send ascii file" option with a terminal software called "Advanced serial port terminal" and the modem receives the file perfectly. I also used a serial port monitoring tool while doing this and i noticed that each line end is indeed sent with CR and LF. Now I am manually trying to do the sending in c sharp, but it does not work. Please see my code above, I am not indicating when I am finished, maybe that is the problem.

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