Hi, I'm working with the C # programming language and with a communication protocol called Zmodem. I downloaded the .dll file to implement functions that allow communication, but when I try to make my code an exception is thrown and I do not know what it refers to. The ZModemSend function needs the address of the file that will be sent and the time it will need to wait while the communication is established, so I create an openFileDialog to search the file on the computer and get the full address where it is hosted, the address is saved in a String type variable called filenameDIR. When I try to run the program an exception is thrown and says: System.FormatException: 'Format String can be only "G", "g", "X", "x", "F", "f", "D" , "d". '
Someone knows what he is referring to or what is happening. Because the file exists, but it does not let me continue further. I spent a lot of time researching what could be wrong, but I could not find anything. I would thank you a lot.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using ZModem_Protocol;
using ZmodemTest;
using System.Windows.Forms;
namespace ZmodemTest
{
public partial class Form1 : Form
{
SerialPort port;
String filenameDIR;
public Form1()
{
InitializeComponent();
string[] ports = SerialPort.GetPortNames();
ComboBox_PortNames.Items.AddRange(ports);
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void buttonConect_Click(object sender, EventArgs e)
{
if (ComboBox_PortNames.Text != null)
{
port = new SerialPort(ComboBox_PortNames.Text, 115200, Parity.None);
LabelState.Text = "Conected";
}
}
private void buttonLoadFile_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Select a txt file";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//labelDirFile.Text = openFileDialog1.InitialDirectory;
labelNameFile.Text = openFileDialog1.FileName;
filenameDIR = openFileDialog1.FileName;
}
}
private void buttonSend_Click(object sender, EventArgs e)
{
try
{
SerialPort_Fix.SerialPortFixer.Execute(port.PortName);
}
catch (Exception v)
{
Console.WriteLine("SerialPort fixer exception: " + v.Message);
Console.ReadKey();
return;
}
ZModem_Protocol.ZModem zmodem = new ZModem_Protocol.ZModem(port);
FileInfo fileInfo = null;
if (!File.Exists(@filenameDIR)) {
Console.WriteLine("Holaaaa");
}
fileInfo = new FileInfo(@filenameDIR);
//if (!File.Exists(filenameDIR))
//{
// throw new Exception("File not found");
//}
//FileInfo fileinfo = new FileInfo(filenameDIR);
//Console.WriteLine("rz\r");
//String test = "test.txt";
//FileStream file = File.Open(@"test.txt", FileMode.Open);
try
{
zmodem.ZModemSend(filenameDIR, 10);
}
catch (Exception)
{
throw;
}
}
}
}
Inside the library .dll This method is ZModemSend, which is the one used to make the communication.
public bool ZModemSend(string fileName, int timeOutS)
{
byte[] numArray;
FileInfo fileInfo = null;
if (!File.Exists(fileName))
{
throw new Exception("File not found");
}
fileInfo = new FileInfo(fileName);
if (!this.portserie.IsOpen)
{
this.portserie.Open();
}
Queue<byte> nums = new Queue<byte>();
ZModem.BuildZRQINITFrame();
this.SendCommand("rz\r");
this.SendCommand(ZModem.BuildZRQINITFrame());
while (this.portserie.BytesToRead > 0)
{
nums.Enqueue((byte)this.portserie.ReadByte());
}
Console.WriteLine("Answer to ZRQINIT from device:");
while (nums.Count > 0)
{
Console.Write(nums.Dequeue());
}
Console.WriteLine("");
byte[] numArray1 = null;
numArray1 = ZModem.BuildZFILEFrame(fileInfo.Name, (int)fileInfo.Length);
this.SendCommand(numArray1);
while (this.portserie.BytesToRead > 0)
{
nums.Enqueue((byte)this.portserie.ReadByte());
}
Console.WriteLine("Answer to ZFILE from device:");
while (nums.Count > 0)
{
Console.Write(nums.Dequeue());
}
Console.WriteLine("");
using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
BinaryReader binaryReader = new BinaryReader(fileStream);
numArray1 = ZModem.BuildZDATAFrame(0);
this.portserie.Write(numArray1, 0, (int)numArray1.Length);
int num = 0;
Crc16Ccitt crc16Ccitt = new Crc16Ccitt(Crc16Ccitt.InitialCrcValue.Zeros);
while (binaryReader.PeekChar() > 0)
{
if (num == 256)
{
SerialPort serialPort = this.portserie;
byte[] numArray2 = new byte[] { 24, 105 };
serialPort.Write(numArray2, 0, 2);
crc16Ccitt.AddByteToComputeChecksum(105);
byte[] cRC = crc16Ccitt.getCRC();
numArray = ZModem.ZDLEEncoding(cRC[1]);
this.portserie.Write(numArray, 0, (int)numArray.Length);
numArray = ZModem.ZDLEEncoding(cRC[0]);
this.portserie.Write(numArray, 0, (int)numArray.Length);
num = 0;
crc16Ccitt = new Crc16Ccitt(Crc16Ccitt.InitialCrcValue.Zeros);
}
byte num1 = binaryReader.ReadByte();
crc16Ccitt.AddByteToComputeChecksum(num1);
numArray = ZModem.ZDLEEncoding(num1);
this.portserie.Write(numArray, 0, (int)numArray.Length);
num++;
}
SerialPort serialPort1 = this.portserie;
byte[] numArray3 = new byte[] { 24, 104 };
serialPort1.Write(numArray3, 0, 2);
crc16Ccitt.AddByteToComputeChecksum(104);
byte[] cRC1 = crc16Ccitt.getCRC();
numArray = ZModem.ZDLEEncoding(cRC1[1]);
this.portserie.Write(numArray, 0, (int)numArray.Length);
numArray = ZModem.ZDLEEncoding(cRC1[0]);
this.portserie.Write(numArray, 0, (int)numArray.Length);
num = 0;
crc16Ccitt = new Crc16Ccitt(Crc16Ccitt.InitialCrcValue.Zeros);
Thread.Sleep(50);
while (this.portserie.BytesToRead > 0)
{
nums.Enqueue((byte)this.portserie.ReadByte());
}
Console.WriteLine("Answer to ZDATA from device:");
if (nums.Count > 0)
{
Console.WriteLine("transmission Error!");
}
while (nums.Count > 0)
{
if (nums.Peek() != 0)
{
Console.Write(nums.Dequeue());
}
else
{
Console.WriteLine("CRC Error!");
}
}
Console.WriteLine("");
this.SendCommand(ZModem.BuildZEOFFrame((int)fileStream.Position));
while (this.portserie.BytesToRead > 0)
{
nums.Enqueue((byte)this.portserie.ReadByte());
}
Console.WriteLine("Answer to ZEOF from device:");
while (nums.Count > 0)
{
if (nums.Peek() != 8)
{
Console.Write(nums.Dequeue());
}
else
{
nums.Dequeue();
}
}
Console.WriteLine("");
binaryReader.Close();
}
while (this.portserie.BytesToRead > 0)
{
nums.Enqueue((byte)this.portserie.ReadByte());
}
Console.WriteLine("Answer to ZFIN from device:");
while (nums.Count > 0)
{
Console.Write(nums.Dequeue());
}
Console.WriteLine("");
this.SendCommand(ZModem.SessionAbortSeq);
this.portserie.Close();
return false;
}
Check and it turns out that where you throw the exception is in this method when using .ToString (). What I do not understand is if the parameters of the function are correctly placed to effectively return a String.
private static string BuildHexFrame(ZModem.HeaderType type, int arg0,
int arg1, int arg2, int arg3)
{
string[] hexCommonHeader = new string[] {
ZModem.HexCommonHeader, type.ToString("x2"), arg0.ToString("x2"),
arg1.ToString("x2"), arg2.ToString("x2"), arg3.ToString("x2") };
string str = string.Concat(hexCommonHeader);
int num = ZModem.ComputeHeaderCRC((int)type, arg0, arg1, arg2, arg3);
str = string.Concat(str, num.ToString("x4"));
return string.Concat(str, ZModem.EndOfCommand);
}