用于艾德堡HP系列数显式推拉力计的串口读取
#include <fstream>
#include "stdafx.h"
#include "conio.h"
using namespace std;
#using <System.dll>
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::Runtime::InteropServices;
public ref class PortChat
{
private:
static bool _continue;
static SerialPort^ _serialPort;
public:
static void Main()
{
String^ name;
String^ message;
StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));
// Create a new SerialPort object with default settings.
_serialPort = gcnew SerialPort();
// Allow the user to set the appropriate properties.
_serialPort->PortName = SetPortName(_serialPort->PortName);
_serialPort->BaudRate = SetPortBaudRate(_serialPort->BaudRate);
_serialPort->Parity = SetPortParity(_serialPort->Parity);
_serialPort->DataBits = SetPortDataBits(_serialPort->DataBits);
_serialPort->StopBits = SetPortStopBits(_serialPort->StopBits);
_serialPort->Handshake = SetPortHandshake(_serialPort->Handshake);
// Set the read/write timeouts
_serialPort->ReadTimeout = 500;
_serialPort->WriteTimeout = 500;
_serialPort->Open();
_continue = true;
readThread->Start();
Console::WriteLine("Type Q or q to exit");
while (_continue)
{
message = Console::ReadLine();
char ch = _getch();
if (ch == 'q' || ch == 'Q')
{
_continue = false;
}
//else
//{
// _serialPort->WriteLine(
// String::Format("<{0}>: {1}", name, message));
//}
}
readThread->Join();
_serialPort->Close();
};
/****************
C++调用C#的dll时候,需要响应的类型转换
将 unsigned char[] 转变成 array<unsigned char>^
********************/
static cli::array<unsigned char>^ GetBYTECLI(unsigned char *pszName, int nStrLen)
{
cli::array<unsigned char>^ SerialWrite = gcnew cli::array<unsigned char>(nStrLen);
for (int i = 0; i<nStrLen; i++)
{
SerialWrite[i] = pszName[i];
}
return SerialWrite;
}
static void Read()
{
while (_continue)
{
Byte Writedata[] = { 0x01,0x03,0x00,0x00,0x00,0x0D,0x84,0x0F };//向测力计发送询问指令
cli::array<unsigned char>^ SerialWrite = GetBYTECLI(Writedata, 8);
_serialPort->Write(SerialWrite, 0, 8);
try
{
Byte Serialdata[40];
for (size_t i = 0; i < 31; i++)
{
Serialdata[i] = _serialPort->ReadByte();
}
unsigned long SerialOutput = Serialdata[3] << 24 | Serialdata[4] << 16
| Serialdata[5] << 8 | Serialdata[6] << 0;//数据在第4-7位
float d = *(float*)&SerialOutput;//浮点表示法;
Console::WriteLine("{0}\t", d);
}
catch (TimeoutException ^) {}
}
}
static String^ SetPortName(String^ defaultPortName)
{
String^ portName;
portName = "com5";//串口号在这里
return portName;
}
static Int32 SetPortBaudRate(Int32 defaultPortBaudRate)
{
String^ baudRate;
baudRate = "9600";//波特率在这里
return Int32::Parse(baudRate);
}
static Parity SetPortParity(Parity defaultPortParity)
{
String^ parity;
parity = "None";
return (Parity)Enum::Parse(Parity::typeid, parity);
}
static Int32 SetPortDataBits(Int32 defaultPortDataBits)
{
String^ dataBits;
dataBits = "8";
return Int32::Parse(dataBits);
}
static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
String^ stopBits;
stopBits = "One";
return (StopBits)Enum::Parse(StopBits::typeid, stopBits);
}
static Handshake SetPortHandshake(Handshake defaultPortHandshake)
{
String^ handshake;
handshake = "None";
return (Handshake)Enum::Parse(Handshake::typeid, handshake);
}
};
int main()
{
PortChat::Main();
}