串口错误值 [英] Serial port wrong value

查看:94
本文介绍了串口错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的串口有一个大问题,我做了一个应用程序,通过串口接收数据,在c#的智能设备上使用紧凑的框架。

我收到错误数据的问题。我应该得到值6.但我得到63 ascii =?

为什么。这是我做的代码

  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 = < span class =code-string> \\Folfer \\recupe.txt
;
StreamWriter StrWer = 默认(StreamWriter);
StrWer = new StreamWriter(strPath, true );
StrWer.WriteLine(data2);
StrWer.Close();
}





请帮助



我尝试过:



 静态  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 = 默认(StreamWriter);
StrWer = new StreamWriter(strPath, true );
StrWer.WriteLine(data2);
StrWer.Close();
}





[更新帮助改善问题]

放置鼠标在这里你的会员名称就在下面,向右走你必须看到绿色'改善问题'。

解决方案

你有很多不同的原因可能会收到错误的字符。

您对硬件设置的了解很少,所以我只能就如何找到错误提供一般性建议。



最常见的是你没有正确配置端口。

确定你已经正确设置了波特率,数据位,奇偶校验和停止位。 / b>

某些设备(例如条形码扫描仪)可能使用7个数据位甚至奇偶校验。

请查看与之通信的设备的文档。



如果您有可能,可以将智能设备连接到运行HyperTerminal或其他东西的PC,然后发送从PC到设备的数据。

通过这种方式,您可以在智能设备上以受控方式调试软件。



另一个好处调试工具是一个小硬件嗅探器。将其插入设备之间,并通过PC和终端程序收听通信。

请参阅 RS232间谍电缆 [ ^ ]例如。



如果你在Windows PC上工作,PortMon是检查低级别通信的优秀工具。这是一个免费工具,可以从这里下载:适用于Windows的Portmon [


[更新]

好​​的,所以你通过IrDA进行沟通。好吧,至少那不包括有线电视的问题。



如果我理解正确你就有3台设备。



1. XP笔记本电脑(?),名为XP

2.智能设备,称为SD

3.电子卡,称为EC



(所有这些都有一个IrDA端口。)



使用下面的矩阵测试连接3的结果设备不同的组合。

我发现写下你尝试过的东西很有帮助,因为在诡计之后很容易迷失并且不止一次尝试相同的组合。



如果您需要,可以扩展矩阵并添加您测试过的硬件和软件组合。



XP SD EC
XP x
SD x
EC x




如果你能在XP-SD之间进行通信并获得良好的结果和XP-EC,这很奇怪但不是SD - EC。

特别是如果您使用相同的端口设置,例如波特率和奇偶校验。


建议:

- 使用char数组存储从串行接收的数据。您不能期望收到的数据是什么。永远不会收到字符串然后转换为 char

- 调试序列号链接,使用终端仿真器用计算机替换其中一个设备。它可以让你缩小研究范围。



[更新]

暂时停止你的程序更改。

- 首先你需要确定什么是RS232参数。

而不是你的程序,在接收方使用终端模拟器。喜欢Putty
下载PuTTY - 适用于Windows的免费SSH和telnet客户端 [ ^ ]

或其他终端模拟器,如 RealTerm:Serial / TCP Terminal download | SourceForge.net [ ^ ]允许显示收到的内容你看到六合一的字符,数据的代码即使不是可打印的字符。



顺便问一下:另一边发送数据的东西是什么?

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

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:

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'.

解决方案

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.


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 ?


这篇关于串口错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆