Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code below returns the error:
System.FormatException Error: The input string was not in a correct format.

What I have tried:

C#
SerialPort sp = new SerialPort();
sp.PortName = "COM3";
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Open();
byte[] bytestosend = { 0x1B, byte.Parse("?"), 0x1B, 0xA2, byte.Parse("748"), 0x0D, 0x1B, 0xA3, byte.Parse("1200,00"), 0x0D, 0x1B, 0xA4, byte.Parse("11/10/2019"), 0x0D, 0x1B, 0xA1, byte.Parse("CHICOBEL"), 0x0D, 0x1B, 0x1B, 0xB0, 0x0D };

sp.Write(bytestosend, 0, bytestosend.Length);
Posted
Updated 15-Oct-19 2:46am
Comments
ZurdoDev 11-Oct-19 7:47am    
Which line causes the error?
phil.o 11-Oct-19 7:53am    
What are you expecting from snippets like byte.Parse("?") or byte.Parse("11/10/2019") to produce?
byte corresponds to an unsigned 8-bits value, you cannot fit anything in there.

C#
byte.Parse("1200,00")
byte.Parse("CHICOBEL")

Byte.Parse Method (System) | Microsoft Docs[^] only accepts integer values in the string.
 
Share this answer
 
Comments
TheRealSteveJudge 11-Oct-19 8:02am    
5*, but also the allowed range of 0 to 255 must be respected.
Richard MacCutchan 11-Oct-19 8:14am    
Thank you for noting that.
Each of these expressions causes an exception:
byte.Parse("?")
byte.Parse("748")
byte.Parse("1200,00")
byte.Parse("11/10/2019")
byte.Parse("CHICOBEL")

e.g. byte.Parse("748") results in 'Value was either too large or too small for an unsigned byte.

e.g. byte.Parse("CHICOBEL") results in 'Input string was not in a correct format.'

Each argument must be a number which fits into the unsigned byte type.

'?' is not a number
'1200,00' is a number, but too big
'11/10/2019' is not a number
'CHICOBEL' is not a number
'748' is a number, but too big

As Byte Struct in C# - GeeksforGeeks[^] says a byte can hold a number between 0 and 255.

In order to split any data type to bytes you can use the BitConverter class:
BitConverter Class (System) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Gilcecler Carneiro 11-Oct-19 8:07am    
in java i make a for
in C # does the write command not print byte by byte?

besides hex commands I need to send byte by byte the values ​​"1200.00" "748" etc

//java
stValue = "748"; //Banco

btCmds[1] = (byte)0xA2;

btCmds[0] = 0x1B;

for(wdCnt=0;wdCnt < stValue.length() ;wdCnt++ )

{ btCmds[wdCnt+2] = stValue.getBytes()[wdCnt]; }

btCmds[wdCnt+2] = 0x0D;

szCmds = wdCnt+3;

for(int Count = 0;Count < szCmds; Count ++) //Envia Dados

serialPort.writeByte(btCmds[Count]);



stValue = "123456,00"; //Valor

obReg.Valor = stValue;

btCmds[1] = (byte)0xA3;

btCmds[0] = 0x1B;

for(wdCnt=0;wdCnt < stValue.length() ;wdCnt++ )

{ btCmds[wdCnt+2] = stValue.getBytes()[wdCnt]; }

btCmds[wdCnt+2] = 0x0D;

szCmds = wdCnt+3;

for(int Count = 0;Count < szCmds; Count ++) //Envia Dados

serialPort.writeByte(btCmds[Count]);
TheRealSteveJudge 11-Oct-19 8:15am    
If you read the Java code thoroughly you will find that a "getBytes" method is used.
This converts e.g. '123456,00' into a byte array.
You must do the same by using the BitConverter Class

https://docs.microsoft.com/en-us/dotnet/api/system.bitconverter?view=netframework-4.8
Gilcecler Carneiro 11-Oct-19 9:37am    
@TheRealSteveJudge - very important your remark, helped me a lot, thanks :)
Gilcecler Carneiro 11-Oct-19 8:55am    
hummmm, should i do the same loop save in matrix? I'll try
is correct ?

didn't entry in for


C#
SerialPort sp = new SerialPort();
                sp.PortName = "COM3";
                sp.BaudRate = 9600;
                sp.Parity = Parity.None;
                sp.DataBits = 8;
                sp.StopBits = StopBits.One;
                sp.Open();
                
                byte[] btCmds = new byte[200];

                btCmds[1] = 0xA3;
                btCmds[0] = 0x1B;

                stValue = "123456,00"; //Valor
                MessageBox.Show("tamanho " + stValue.Length);

                for (wdCnt = 0; wdCnt < stValue.Length; wdCnt++)
                {
                    stValuep=stValue.Substring(wdCnt);
                    MessageBox.Show("substring  " + stValuep);
                    
                        
                    btCmds[wdCnt + 2] = byte.Parse(stValuep); 
                }

                               
                btCmds[wdCnt + 2] = 0x0D;

                szCmds = wdCnt + 3;

                //for (int Count = 0; Count < szCmds; Count++)   //Envia Dados

                MessageBox.Show("vetor byte " + btCmds);

                    sp.Write(btCmds, 0, btCmds.Length);
 
Share this answer
 
Comments
Richard MacCutchan 15-Oct-19 9:23am    
No it is not correct, you need to add the length value to the stValue.Substring call:
stValuep=stValue.Substring(wdCnt, 1);
However, that will still fail when it tries to parse the comma. Also it is not clear exactly what format the value needs to be in when it is passed to the serial port device, and that may very well be crucial.
Gilcecler Carneiro 15-Oct-19 15:32pm    
*************************************************************
Printer Manual

Function: Command / Format (HEX):
Send value to print 1Bh A3h 'VALUE' 0Dh
*******************************************************
for example:
I want to report the value of $ 1200.00
I need to send the "."



stValue = "1200.00"; //Valor

for (wdCnt = 0; wdCnt < stValue.Length; wdCnt++)
{
stValuep=stValue.Substring(wdCnt);
MessageBox.Show("substring " + stValuep);
btCmds[wdCnt + 2] = byte.Parse(stValuep);
}

btCmds[wdCnt + 2] = 0x0D;

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