using System; using System.Text; using System.Windows; using SLAB_HID_TO_SMBUS; namespace MMCP2112 { public partial class MainWindow : Window { const ushort vid = 4292; //10C4(CP2112デフォルトVID) const ushort pid = 60048; //EA90(CP2112デフォルトPID) IntPtr connectedDevice; byte slaveAddr; //ArduinoのI2Cスレーブアドレス(8) byte sendSize; //送信したバイト数 public MainWindow() { InitializeComponent(); } //初期設定 private void Window_Loaded(object sender, RoutedEventArgs e) { uint numDevices = 0; CP2112_DLL.HidSmbus_GetNumDevices(ref numDevices, vid, pid); // 1台だけ接続されていることをチェック if (numDevices < 1) { MessageBox.Show("MM-CP2112が接続されていません。\n", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (numDevices > 1) { MessageBox.Show("MM-CP2112が複数接続されています。接続は1台までです。\n", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } // デバイスポインター取得 CP2112_DLL.HidSmbus_Open(ref connectedDevice, 0, vid, pid); int rtnval; rtnval = CP2112_DLL.HidSmbus_WriteLatch(connectedDevice, 0x0, 0x10); if (rtnval != CP2112_DLL.HID_SMBUS_SUCCESS) { MessageBox.Show("接続エラー\r\nHidSmbus_WriteLatch ERR " + rtnval.ToString("X4") + "\n", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } slaveAddr = 0x10; //I2Cスレーブアドレス(8)は1ビット左シフトする sendSize = 0; } //終了設定 private void Window_Closed(object sender, EventArgs e) { CP2112_DLL.HidSmbus_Close(connectedDevice); } //送信 private void SendButtonClick(object sender, RoutedEventArgs e) { ASCIIEncoding ascii = new ASCIIEncoding(); byte[] sendData = ascii.GetBytes(SenderBox.Text); sendSize = (byte)sendData.Length; if (sendSize < 1) return; else if (sendSize > 32) //ArduinoのI2Cバッファは32バイト sendSize = 32; int rtnval = CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, slaveAddr, sendData, sendSize); if (rtnval != CP2112_DLL.HID_SMBUS_SUCCESS) { MessageBox.Show("I2C接続エラー:HidSmbus_WriteRequest(" + rtnval.ToString("X4") + ")", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } SenderBox.Text = ""; } //受信 private void ReceiveButtonClick(object sender, RoutedEventArgs e) { char[] strData = new char[32]; byte[] receiveData = new byte[62]; byte status = 0; byte totalBytesRead = 0; if (sendSize == 0) //DATA無し return; int rtnval = CP2112_DLL.HidSmbus_ReadRequest(connectedDevice, slaveAddr, sendSize); if (rtnval != CP2112_DLL.HID_SMBUS_SUCCESS) { MessageBox.Show("I2C接続エラー:HidSmbus_AddressReadRequest(" + rtnval.ToString("X4") + ")", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } rtnval = CP2112_DLL.HidSmbus_ForceReadResponse(connectedDevice, sendSize); if (rtnval != CP2112_DLL.HID_SMBUS_SUCCESS) { MessageBox.Show("I2C接続エラー:HidSmbus_ForceReadResponse(" + rtnval.ToString("X4") + ")", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } int p = 0; do { byte bytesRead = 0; rtnval = CP2112_DLL.HidSmbus_GetReadResponse(connectedDevice, ref status, receiveData, 62, ref bytesRead); if (rtnval != CP2112_DLL.HID_SMBUS_SUCCESS) { MessageBox.Show("I2C接続エラー:HidSmbus_GetReadResponse(" + rtnval.ToString("X4") + ")", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); return; } for (ushort i = 0; i < bytesRead; i++) { strData[p++] = Convert.ToChar(receiveData[i]); } totalBytesRead += bytesRead; } while (totalBytesRead < sendSize); string s = new string(strData); ReceiverBox.Text = s.Substring(0, sendSize); ; sendSize = 0; } } }