Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi there, i have a big problem with a serial port, i did an application that receive data through serial port with compact framwork on smart device with c#.
the problem that i received wrong data. i should get the value 6. but i got 63 on ascii = "?"
why. here it is the code that i did
C#
static void th_sr_f()
       {
           SerialPort mySerialPort = new SerialPort("COM8",2400, Parity.None, 8, StopBits.One);
           mySerialPort.Open();
           mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);



             }

       private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
       {

          string data2 = "";
           SerialPort sp = (SerialPort)sender;
           string indata = sp.ReadExisting();

           char[] charValues = indata.ToCharArray();
           foreach (char _eachChar in charValues)
           {
               int value = Convert.ToInt32(_eachChar);
               data2 += String.Format("{0:X}", value + " ");

           }

           string strPath = "\\Folfer\\recupe.txt";
           StreamWriter StrWer = default(StreamWriter);
           StrWer = new StreamWriter(strPath, true);
           StrWer.WriteLine(data2);
           StrWer.Close();
       }


please help

What I have tried:

C#
static void th_sr_f()
        {
            SerialPort mySerialPort = new SerialPort("COM8",2400, Parity.None, 8, StopBits.One);
            mySerialPort.Open();
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        
       
  
              }


   private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {

           string data2 = "";
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();

            char[] charValues = indata.ToCharArray();
            foreach (char _eachChar in charValues)
            {
                int value = Convert.ToInt32(_eachChar);
                data2 += String.Format("{0:X}", value + " ");
             
            }


                string strPath = "\\Folfer\\recupe.txt";
            StreamWriter StrWer = default(StreamWriter);
            StrWer = new StreamWriter(strPath, true);
            StrWer.WriteLine(data2);
            StrWer.Close();
        }


[Update for help improve the question]
Put your mouse here your member name is just under, go right you must see the green 'improve question'.
Posted
Updated 19-May-16 2:41am
v4
Comments
Sergey Alexandrovich Kryukov 16-May-16 16:45pm    
The question makes little to no sense, because many things depend not on your code, but on what you have on the other end of your RS-232 cable, and we have no information on that.
—SA

There are quite a few different reasons why you might receive the wrong character.
You are telling very little about your hardware setup, so I can only give general advice for how you can find the error.

The most common is that you haven't configured the port correctly.
Make dead sure you have set the baud rate, data bits, parity and stop bits correctly.
Some devices, for example a barcode scanner, might use 7 data bits and even parity.
So check the documentation for the device you are communicating with.

If you have the possibility, you could connect your smart device to a PC where you run HyperTerminal or something, and then send data from the PC to your device.
This way you can debug your software on the smart device in a controlled way.

Another good debug tool is a little hardware sniffer. Plug that in between your devices and listen to the communication with a PC and a terminal program.
See An RS232 "spy" cable[^] for an example.

If you work on a Windows PC, PortMon is an exellent tool for checking the low level communication. It's a free tool and can be downloaded from here: Portmon for Windows[^]
I doubt you can use this on your smart device, though.

[UPDATE]
Ok, so you are communicating over IrDA. Well at least that excludes a problem with the cable.

If I understand you correctly you have 3 devices.

1. An XP laptop(?), called XP
2. A smart device, called SD
3. An electronic card, called EC

(All of them have an IrDA port.)

Use the matrix below and test the result of connecting the 3 devices the different combinations.
I find it helpful to write down what you have tried, because after a wile it is easy to get lost and try the same combination more than once.

If you need you can expand the matrix and add combinations of hardware and software you have tested.

XPSDEC
XPx
SDx
ECx


It is weird if you can communicate between the XP - SD and get a good result and the XP - EC but not the SD - EC.
Especially if you are using the same port settings, such as baudrate and parity.
 
Share this answer
 
v2
Comments
Jammes_Ca 17-May-16 4:20am    
Hi, i will explain more, there is an electronic card with an IR receiver and sender.
so when i send an specefic data to this ir, a red led be "on", after the sender of this electronic card send me the number "6".
i did 2 program
the first is on wondows xp that has an ir sensor , did a program that receive data from the receiver of electronic card, and i receive the number 6 corectly.

the second program is the same program who run on xp but it run on windows mobile. but i receive teh number 63 which mean "?"

so i think the problem due to the OS or we have change some configuration to get the number 6 on windows mobile.
have you an idea ?
George Jonsson 17-May-16 4:36am    
Sorry. I cannot give more advice without having the actual hardware in front of me.

What happens if you use the Windows XP PC and try to communicate with the electronic card?
Patrice T 17-May-16 18:57pm    
Use Improve question to update your question.
Jammes_Ca 17-May-16 6:12am    
the program that run on xp receive a correct data witch equal to 6 but in the windows mobile i receive 63,
George Jonsson 17-May-16 19:58pm    
I have updated the question with some more trouble shooting help.
Not much more I can do without having the things in front of me.
Advice:
- use a char array to store the data received from serial. You can't have expectations about what is the data you are receiving. Never receive in a string and then transform to char.
- To debug the serial link, replace 1 of the devices with a computer using a terminal emulator. it will allow you to narrow the research.

[Update]
stop changes in your program for now.
- First you need to make sure of what is the RS232 parameters.
Instead of your program, use a terminal emulator on receiving side.Like Putty Download PuTTY - a free SSH and telnet client for Windows[^]
or another terminal emulator like RealTerm: Serial/TCP Terminal download | SourceForge.net[^] which allow the display of received chars in hexa you see, the codes of the data even if not printable chars.

By the way: What the thing on the other side sending data ?
 
Share this answer
 
v3
Comments
Jammes_Ca 18-May-16 16:00pm    
thank you too, could you tell me what i change here it is my new code

<pre lang="c#">

private void button2_Click(object sender, EventArgs e)
{


//first data
Thread.Sleep(70);
serialPort1.Write(new byte[] { 65 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 18 }, 0, 1);
Thread.Sleep(70);


// here i should get the nubmer 6 from the receiver


serialPort1.Parity = Parity.Odd; ////////
// second data

serialPort1.Write(new byte[] { 2 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 91 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 69 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 3 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 3 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 66 }, 0, 1);
Thread.Sleep(70);
// serialPort1.Parity = Parity.None; ////////

// here i should get 6 from the receier




//third data

serialPort1.Write(new byte[] { 65 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 17 }, 0, 1);
Thread.Sleep(70);

// here i should get 2 83 71 0 1 3 209 41

// last data

serialPort1.Write(new byte[] { 6 }, 0, 1);
//serialPort1.Parity = Parity.None; ////////
}



private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (serialPort1.BytesToRead >5)
{
data = serialPort1.ReadExisting();

char[] charValues = data.ToCharArray();

foreach (char _eachChar in charValues)
{
value = (_eachChar);
data2 += String.Format("{0:X}", value + " ");

}

this.Invoke(new EventHandler(display_data_event));// fonction temps réel
data2 = "";
}
}

private void display_data_event(object sender, EventArgs e)
{
textBox2.Text = data;
textBox4.Text = data2;
}

</pre>

i received missed data and i have an other problem, is that the number 83 and the number 3 are = 63

i should get this data
6 6 2 83 71 0 1 3 209 41. but me i receive 2 63 71 0 1 63 209 41

could you help me please
Patrice T 18-May-16 17:06pm    
Use Improve question to update your question.
Jammes_Ca 18-May-16 17:09pm    
how do i do to Improve question ?
Patrice T 18-May-16 17:46pm    
Improve question button is on bottom of your question, put the mouse on your user name on bottom of question, it is on right
Jammes_Ca 19-May-16 3:19am    
i did what you told me but i didn't find the improve 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