232转USB线有两种,一种是优越者的数据线,一种是普通的数据线,
之前代码用的是serialport进行串口通信,发现普通的数据线接收不到数据,只能用优越者的数据线可以接收到数据,
本人改用mscomm32进行串口通信时,这两种线都可以接收到数据,
serialport串口通信时,对数据线有要求吗, 还是本人的代码有问题,大家帮本人看一下
下面只是做了一个例子,代码不太完美
public partial class Form1 : Form
{
public SerialPort Sp;
public Form1()
{
InitializeComponent();
Sp = new SerialPort();
}
private void axMSComm1_OnComm(object sender, EventArgs e)
{
axMSComm1.Height = 12;
object ret;
ret = axMSComm1.Input;
}
private void mscomm_btn_Click(object sender, EventArgs e) //用mscomm通信
{
if( axMSComm1.PortOpen==false)
{
axMSComm1.CommPort = 9;
axMSComm1.Settings = “9600,n,8,1”;
axMSComm1.InBufferSize = 1024;
axMSComm1.OutBufferSize = 1024;
axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;
axMSComm1.InputLen = 0;
axMSComm1.PortOpen = true;
}
byte[] TT=new byte[6];
TT[0]=1;
TT[1]=3;
TT[2]=0;
TT[3]=0;
TT[4]=241;
TT[5]=216;
//{1,3,0,0,241,216}
axMSComm1.Output = TT;
}
private void serialBTN_Click(object sender, EventArgs e) //用serialport通信
{
byte[] TT = new byte[6];
TT[0] = 1;
TT[1] = 3;
TT[2] = 0;
TT[3] = 0;
TT[4] = 241;
TT[5] = 216;
if (Sp.IsOpen == false)
{
Sp.PortName = “COM9”;
Sp.BaudRate = 9600;
Sp.Parity = Parity.None;
Sp.DataBits = 8;
Sp.StopBits = StopBits.One;
Sp.Open();
Sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Sp_DataReceived);
}
Sp.DiscardInBuffer();
Sp.Write(TT, 0, TT.Length);
}
public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int lenTemp = 0;
int dataLen;
byte[] input = new byte[70];
if (Sp.IsOpen == false)
return;
input = new byte[70];
lenTemp = Sp.Read(input, 0, 70);
Sp.DiscardInBuffer();
}
}
20
2、建议将接收到的数据保存成全局变量,在DataReceived累加进去,Length=70时再处理