C#读取串口数据问题,并且写入数据库,求详细代码

2025-01-26 15:46:04
推荐回答(1个)
回答1:

private void button1_Click(object sender, EventArgs e)
{
sp.Close();
sp.PortName = "COM1";

sp.BaudRate = 4800;
sp.Open();

}
private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
bool stop = false;
while(!stop)
try
{
string message = sp.ReadLine();
message = message.Substring( 0, 10) //这么写是从串口一条一条的读入,然后截取每一行从头到第10个字节,因为你那个有空格,调试时不对的改下10就好了,串口读入数据百度很多,我就不写了
}