Home > Net >  How do in c # multiple Form Windows share a serial port to send data?
How do in c # multiple Form Windows share a serial port to send data?

Time:11-19

I want to set up a PC control of single chip microcomputer, 485 communication between single-chip microcomputer,

On the left is three function module and data real-time display page, click on the corresponding module, on the right side of the panel container will replace me set Form2, Form3 corresponding interface; Such as centrifuge interface here, after I set the start, through a serial port sent to the next bit machine,
I had a few problems when sharing the serial port:


 
Public static SerialPort serialPort1=new SerialPort ();//this is the main form of Form1

Public static IModbusMaster master;//this is the centrifuge Form in the Form


Before running time is always an error, only when a form is not an error, but also can normal to send data and single chip microcomputer communication,

CodePudding user response:

Get a singleton class, class provides a serial port switch method, a serial port object set into a singleton

CodePudding user response:

Inside the main form of the serial port of the create operation class, Form2, Form3 increase constructor, a new constructor with parameters for a serial port, the serial port operation in the constructor object saved to the local local variables inside
The main form in the generated Form2, Form3 passed on operation object through the constructor of the serial port, such Form2, Form3 can call directly to the serial port operation

CodePudding user response:


Serial problems like is on my side, by in form1 instantiate a serialport, this problem should be solved, the but I prepare to use the modbus protocol in Form3 writing coil, the place of the master error

 
SlaveAddress=byte. Parse (" 1 ");//from the station address
StartAddress=ushort. Parse (" 3 ");//address
Master. WriteSingleCoil (slaveAddress startAddress, false);//this place master error


error reason: the object reference not set to an instance of an object

 
//about this function used elsewhere
Public static IModbusMaster master;
//source definition about the function
The namespace. The Modbus Device
{
Public interface IModbusMaster: IDisposable
{
ModbusTransport Transport {get; }

Bool [] ReadCoils (byte slaveAddress, ushort startAddress, ushort numberOfPoints);
TaskUshort [] ReadHoldingRegisters (byte slaveAddress, ushort startAddress, ushort numberOfPoints);
TaskUshort [] ReadInputRegisters (byte slaveAddress, ushort startAddress, ushort numberOfPoints);
TaskBool [] ReadInputs (byte slaveAddress, ushort startAddress, ushort numberOfPoints);
TaskUshort [] ReadWriteMultipleRegisters (byte slaveAddress, ushort startReadAddress, ushort numberOfPointsToRead, ushort startWriteAddress, ushort [] writeData);
TaskVoid WriteMultipleCoils (byte slaveAddress, ushort startAddress, bool [] data);
Task WriteMultipleCoilsAsync (byte slaveAddress, ushort startAddress, bool [] data);
Void WriteMultipleRegisters (byte slaveAddress, ushort startAddress, ushort [] data);
Task WriteMultipleRegistersAsync (byte slaveAddress, ushort startAddress, ushort [] data);
Void WriteSingleCoil (byte slaveAddress, ushort coilAddress, Boolean value);
Task WriteSingleCoilAsync (byte slaveAddress, ushort coilAddress, Boolean value);
Void WriteSingleRegister (byte slaveAddress, ushort registerAddress, ushort value);
Task WriteSingleRegisterAsync (byte slaveAddress, ushort registerAddress, ushort value);
}
}

CodePudding user response:

Where you the communication interface IModbusMaster is created, apply it to the global singleton, where the access is the same,

CodePudding user response:

I was created in Form3 window, I try your method, although I dish check data, more feeling should be effective,

CodePudding user response:

A serial port object set into a singleton
  •  Tags:  
  • C#
  • Related