Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Guys

I am sending a compiled python file (.pyo extension) over the serial port to my Telit modem. The modem has a built in python engine.

I am writing a C# application to facilitate the downloading of compiled files to the modem. I can successfully send the file to the modem over COM9 using the "send text file" method in hyper terminal but I cannot do this from C#.

Does the "send text file" method convert the file to raw binary data or a byte array when sending over the serial port??

Using the serial port class I have followed this approach:

//open the file using file stream
FileStream fileStream = new FileStream(@"c:\test.pyo", FileMode.Open, FileAccess.Read, FileShare.Read))

//store the open file as binary
BinaryReader binary = new BinaryReader(fileStream , Encoding.GetEncoding(28591));

//at this point I write to the port
port.Write(binary.ReadBytes((int) fileStream.Length), 0, (int) fileStream.Length);
Is this the right approach for sending a text file?
Posted
Updated 7-Nov-10 7:12am
v2

Hello ;
you can try that for reading the file :

public byte[] ReadByteArrayFromFile(string fileName)
{
byte[] buff = null;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int)numBytes);
return buff;
}


or :
C#
using System.IO;

private static void SendTextFile(  SerialPort port, string FileName)
{ port.Write(File.OpenText(FileName).ReadToEnd()); }

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);
}



and for more detials try that links :
http://www.dreamincode.net/forums/topic/35775-serial-port-communication-in-c%23/[^]

regards..
 
Share this answer
 
Comments
sach262 7-Nov-10 17:07pm    
Thank you

The modem specifies that the file when sent in hyperterminal, you must enable the options: send line ends with line feeds and Append line feeds to incoming line ends.

How do you think this would effect the binary data stream?
Change the Encoding of your SerialPort.,,
 
Share this answer
 
Comments
sach262 9-Nov-10 4:12am    
Thank you

I tried this as you suggested but this did not work. I am sending a text file, and the modem requires the file to have line ends that end with line feeds. Since I am sending a text file i need to use ascii transfer mode and not binary transfer, dont you think??
Mopane04 18-Nov-10 11:11am    
Hi Rajesh,

Did you ever get this resolved? I have the same issue, namely sending "RAW ASCII file transfer". If so, could you please share how you got it to work?

When using the "SendTextFile" option above, the modem sometimes never responds.

Regards
sach262 20-Nov-10 17:24pm    
Hi Mopane, no I still am struggling with this. Any ideas???
i dont know anythng about .net.
 
Share this answer
 
Comments
[no name] 13-May-13 7:20am    
Good to know. But why would you post the fact that you do not know anything about .NET as a solution to this 3 year old question?

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