大家好。
我在XP下用VS2005 C#编写了串口程序,用了自带的SerialPort类,
程序在本机上是可以与单片机正常通信的,但在程序在其他机器上就不可以和单片机正常通信了。
是否这个SerialPort类需要什么环境支持的?
别的机器也是使用XP,安装了.Net 2.0 , 并且在别的机器上用串口通信软件是可以与单片机通信的,
想请问我的程序问题出在什么地方?
谢谢

解决方案 »

  1.   

    没不同。别的机器上用sscomm32.exe或超级终端检查一下,确认硬件连接是否正常。
      

  2.   

    是不是串口设置的问题 比如本机你设置cm1 其他机器cm1 已经被占用了 我的电脑 设备管理器里去看看串口的设置
      

  3.   

    我的接收代码
    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                int recCount = 0;
                byte recSum = 0x00;
                int recByetLen = this.serialPort1.BytesToRead;
                byte[] recByte = new byte[recByetLen];
                this.serialPort1.Read(recByte, 0, recByetLen);
                if ((recByte[0] == 0x41) && (recByte[1] == 0x54) && (recByte[2] == 0x53) && (recByte[3] == 0x54) &&
                                (recByte[4] == 0x45) && (recByte[5] == 0x4B))
                {
                    
                    for (recCount = 6; recCount < recByetLen - 2; recCount++)
                    {
                        recSum += recByte[recCount];
                    }
                    if (recSum == recByte[recByetLen - 1])
                    {                    
                        if (recByte[11] == 0x00)
                        {
                            MessageBox.Show("设置成功");
                        }
                        else if (recByte[11] == 0x01)
                        {
                            MessageBox.Show("设置错误");
                        }
                        str = Encoding.UTF8.GetString(recByte);
                        
                        
                    }
                    this.timer1.Enabled = false;
                    ShowInfo(str);
                }如果定时器1放在校验和里面就不会被停止,放在外面就可以停止。
    但为什么在我电脑上就正常,在其他电脑不正常~
      

  4.   

     private void button5_Click(object sender, EventArgs e)
            {
                if (this.serialPort1.IsOpen)
                {
                    //byte[] bytes = new byte[14];
                    bytes[0] = 0x61;
                    bytes[1] = 0x74;
                    bytes[2] = 0x73;
                    bytes[3] = 0x74;
                    bytes[4] = 0x65;
                    bytes[5] = 0x6B;
                    bytes[6] = 0x00;
                    bytes[7] = 0x04;
                    bytes[8] = 0xcc;
                    bytes[9] = 0x20;
                    bytes[10] = 0xff;
                    bytes[11] = 0xff;
                    bytes[12] = 0x00;
                    bytes[13] = 0xEA;
                    this.serialPort1.Write(bytes, 0, 14);
                    whichButtonClick = 5;
                    this.timer1.Enabled = true;
                    this.button1.Enabled = false;
                    this.button2.Enabled = false;
                    this.button3.Enabled = false;
                    this.button4.Enabled = false;
                    this.button5.Enabled = false;
                }
                else
                {
                    MessageBox.Show("请先打开串口");
                }
            }发送的代码
      

  5.   

    串口设置???
    你的serialPort1_DataReceived数据是不一定能接受完整的。
    因为这个是有数据就触发,一次不知道会接受到几个字节,所以你考虑数据接受的完整性