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

I am using the following method in my C# application to send a text file to a modem.

C#
private static void SendBinaryFile(  SerialPort port, string FileName)
{
  using (FileStream fs = File.OpenRead(FileName))
    port.Write((new BinaryReader(fs)).ReadBytes((int)fs.Length), 0, (int)fs.Length);
}


According to modem this must be done with hardware flow control , so I have done this by setting the following:

MIDL
port.DtrEnable=true;
port.RtsEnable = true;
port.Handshake = Handshake.RequestToSend;


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 binary data stream?
Posted
Updated 7-Nov-10 18:39pm
v2

It seems this modem is favoring unix because windows needs CR+LF to insert an actual new line. With HyperTerminal you would read the data as text and that goes much better when a new line is shown properly. So, I don't think it will have effect on binary data transmission, but an actual test would of course be best. Simply send some binary data back and forward and verify it's the same.

Good luck!
 
Share this answer
 
Comments
sach262 7-Nov-10 17:04pm    
Thank you for your response.

The modem receives and acknowledges the file perfectly when I send it with hyperterminal and enable the above options: send line ends with line feeds and Append line feeds to incoming line ends.

Do you think maybe I should try send it as a byte array perhaps and not use the binary reader?
E.F. Nijboer 8-Nov-10 8:10am    
But the option you mention isn't for you because it only applies when sending text in ascii mode. You are sending binary data so you wouldn't set this option and simply send the data as binary. If there would be any trouble of data not being sent correctly, you could always use base64 encode before sending and base64 decode on receiving.
(officially you would need 7 bits, which is base 128, but since base 64 encoding/decoding can be found fairly easy you could use that. It would also eliminate CR LF characters and therefor completely safe)
sach262 8-Nov-10 9:40am    
Thanks Nijboer.

Your statement about UNIX now makes sense. UNIX text files and windows text files are different as you implied in your previous post. Since the modem manual specifies send line ends with line feeds, the text file needs to be in the UNIX format. I used a little utility to convert the windows text file into a Unix text file and then tried sending it using the binary reader approach, but this still resulted in error.

My apologies, since I am sending a text file I need to send the file in ascii mode. Therefore I assume my approach using the binary reader is wrong. How do I send the UNIX file in ascii mode?
E.F. Nijboer 9-Nov-10 10:53am    
Have a look at the ASCIIEncoding class for proper encoding and decoding.
http://msdn.microsoft.com/en-us/library/ds4kkd55.aspx
http://msdn.microsoft.com/en-us/library/744y86tc.aspx
There is a Problem while sending the binary data from the serial port to Hardware,
if you want to send binary data through the serial port you have to change the Encoding of your Serial port

Use the below code
SerialPort port = new SerialPort();
port.Encoding  = System.Text.Encoding.GetEncoding(1252);
 
Share this answer
 
Comments
sach262 8-Nov-10 5:11am    
Thank you for your response Rajesh. My port encoding was already set to Encoding.GetEncoding("iso-8859-1"), but I changed it as you suggested. Still I can see the data being sent to the modem, but the modem gives an error.

Dont you think the send line ends with line feeds properties etc that I mentioned above have an effect?
Rajesh Anuhya 8-Nov-10 6:08am    
Are Tried the same command from Hyperterminal / Dock light ???
sach262 8-Nov-10 6:34am    
yes from hyperterminal i use the send text file with those properties enabled, and it works correctly

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