我正在从串行端口Rs232读取。我面临一个非常奇怪的问题。 传入信息的总字节为37字节。在我的计算机上,每次我在串行端口中中断时,该信息正好为37字节。
示例abcdefghijklmnopqrstuvwxyz1234567890!直到3
但是在另一台PC中,我的信息被分隔为8个字节
示例abcdefgh,在下一个Interrup ijklmnop中,在下一个Interrup qrstuvwx中,依此类推
这是我的串口代码:
public static string Rs232Weight = "0";//Αυτό είναι για να παίρνει την τιμή η ζυγαριά
public static readonly object lockWeight = new object();
private static SerialPort mySerialPort;
private void button1_Click(object sender, EventArgs e)
{
try
{
mySerialPort = new SerialPort(textBox1.Text);
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.Open();
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
catch
{
}
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
try
{
SerialPort sp = (SerialPort)sender;
lock (lockWeight)
{
string tmp = sp.ReadExisting();
ExportFile(Convert.ToString(tmp) + "|" + Convert.ToString(tmp.Length));
}
}
catch //(Exception ex)
{
// Dialogs.SingleMessage(Convert.ToString(ex), "Προειδοποίηση");
}
}