Click here to Skip to main content
15,909,645 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 over the serial port using C#. I have tried the following but not seeing any data being written to the serial port.

Is my approach correct? Below are my port settings. I am using baud rate 115200, with hardware flow control on , as specified by the modem.

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

C#
StreamReader readtext = new StreamReader("filename.txt");
string contents = readtext.ReadToEnd();
readtext.Close();
readtext.Dispose();
byte[] WriteBuffer=new byte[contents.Length];
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
WriteBuffer = enc.GetBytes(contents);
port.Write(WriteBuffer, 0,contents.Length );
Posted
Updated 21-Nov-10 4:03am
v3
Comments
Sandeep Mewara 21-Nov-10 9:37am    
From next time please use PRF tags to format your code part. It makes the question clear and readable.
Manfred Rudolf Bihy 21-Nov-10 9:44am    
The file reading stuff isn't really the interesting part. Can we have some of the bits that have to do with the construction and initialization of that port thingy.
sach262 21-Nov-10 10:07am    
Hi, apologies for the incorrect format. I have updated the question with the port information
OriginalGriff 21-Nov-10 10:37am    
Answer updated.

1 solution

You can simplify your code quite a bit:
byte[] writeBuffer = File.ReadAllBytes("filename.txt");
port.Write(writeBuffer, 0, writeBuffer.Length);

http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx[^]
But it won't make any difference to data coming out of the serial port.
1) Are you sure no data is being written? Why? How have you checked?
2) If you don't have any data, then:
2a) Is the port connected to a physical serial port, such as COM1? Are you checking COM1, and have you made sure that you can write to it and talk to the modem with a terminal program (hyperterminal or similar)?
2b) Have you configured Handshaking? Is so, turn it off (it is off by default)
2c) Have you enabled RTS and DTR? Try it.

If this doesn't help, edit your question and post the code fragments you use for port create and initialize - we may be able to help further when we have more info.

"Thanks Griff, I can see what is being sent and received from the serial port , using a serial port monitoring tool. Yes I have tested COM9 with hyperterminal etc. I have updated my question with the flow control settings. Note the modem specifies hardware flow control."

Good! So you genuinely have nothing coming out, but you can transmit with Hyperterminal. Try disabling the Handshake - the Modem may specify it but if it ain't there for testing it doesn't matter too much - you can re-enable it when some data is being transferred. It may be cabling between you and the modem - if you don't have the RTS/CTS and DTR/DTE pairs enabling handshake will just disable the port transmit.
 
Share this answer
 
v2
Comments
sach262 21-Nov-10 10:28am    
Thanks Griff, I can see what is being sent and received from the serial port , using a serial port monitoring tool. Yes I have tested COM9 with hyperterminal etc. I have updated my question with the flow control settings. Note the modem specifies hardware flow control.
sach262 21-Nov-10 11:31am    
I tried disabling the handshake, still no luck. 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. So there is something definitely wrong with my code. Still trying to figure it out

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